From 7dfe4ea2f6c62ffb662085cb4d62a13b8e902ce1 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 23 Sep 2015 00:04:27 +0200 Subject: Replace close_connection() for newer django versions django.db.close_connection() is deprecated in django1.7 (and removed in 1.8). The new django.db.close_old_connections() does not seem to work like the old one (see http://stackoverflow.com/a/32614137), so we replace it with an own implementation. --- src/lib/Bcfg2/Reporting/Storage/DjangoORM.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/lib/Bcfg2/Reporting/Storage/DjangoORM.py') diff --git a/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py b/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py index c9aa169bf..7acd9e293 100644 --- a/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py +++ b/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py @@ -11,11 +11,11 @@ import Bcfg2.DBSettings from Bcfg2.Compat import md5 from Bcfg2.Reporting.Storage.base import StorageBase, StorageError from Bcfg2.Server.Plugin.exceptions import PluginExecutionError +import django from django.core import management from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.db.models import FieldDoesNotExist from django.core.cache import cache -from django import db #Used by GetCurrentEntry import difflib @@ -383,8 +383,12 @@ class DjangoORM(StorageBase): finally: self.logger.debug("%s: Closing database connection" % self.__class__.__name__) - db.close_connection() + if django.VERSION[0] == 1 and django.VERSION[1] >= 7: + for connection in django.db.connections.all(): + connection.close() + else: + django.db.close_connection() def validate(self): """Validate backend storage. Should be called once when loaded""" -- cgit v1.2.3-1-g7c22 From 7cc349e27836f760e2a14b65e690473bd63bf3ad Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 23 Sep 2015 20:25:49 +0200 Subject: Reporting: Replace _meta.get_all_field_names() for django 1.8 The _meta API was changed with django 1.8 and get_fields() now returns all model fields even fields defined in related models. But while creating the Entries we use this field list for construction and the list should only contain local fields. --- src/lib/Bcfg2/Reporting/Storage/DjangoORM.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/lib/Bcfg2/Reporting/Storage/DjangoORM.py') diff --git a/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py b/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py index 7acd9e293..ac0cde783 100644 --- a/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py +++ b/src/lib/Bcfg2/Reporting/Storage/DjangoORM.py @@ -24,6 +24,16 @@ from Bcfg2.Reporting.models import * from Bcfg2.Reporting.Compat import transaction +def get_all_field_names(model): + if django.VERSION[0] == 1 and django.VERSION[1] >= 8: + return [field.name + for field in model._meta.get_fields() + if field.auto_created == False and + not (field.is_relation and field.related_model is None)] + else: + return model._meta.get_all_field_names() + + class DjangoORM(StorageBase): options = StorageBase.options + [ Bcfg2.Options.Common.repository, @@ -80,7 +90,7 @@ class DjangoORM(StorageBase): for attr in boolean + ["current_exists"]: xforms[attr] = boolean_xform act_dict = dict(state=state) - for fieldname in entrytype._meta.get_all_field_names(): + for fieldname in get_all_field_names(entrytype): if fieldname in ['id', 'hash_key', 'state']: continue try: -- cgit v1.2.3-1-g7c22