From 0a8f0b026fe5c07af7d4d32201d7062ee4013f57 Mon Sep 17 00:00:00 2001 From: Martin Rejman Date: Fri, 12 Apr 2019 22:16:28 +0200 Subject: [PATCH] Records_audit: oprava zacykleni pri prazdne databazi manage.py migrate padalo, pokud v DataAudited.__init__ bylo volani do DB AuditTable (ci jinych) modelu. --- .../migrations/0054_auto_20190412_1852.py | 22 +++++++++++ src/records_audit/utils.py | 39 ++++++++++++------- 2 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 src/nalodeni/migrations/0054_auto_20190412_1852.py diff --git a/src/nalodeni/migrations/0054_auto_20190412_1852.py b/src/nalodeni/migrations/0054_auto_20190412_1852.py new file mode 100644 index 0000000..3ee20b5 --- /dev/null +++ b/src/nalodeni/migrations/0054_auto_20190412_1852.py @@ -0,0 +1,22 @@ +# Generated by Django 2.0.3 on 2019-04-12 18:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('nalodeni', '0053_auto_20190303_1207'), + ] + + operations = [ + migrations.AlterModelOptions( + name='newsletter', + options={'ordering': ('-enabled', 'name')}, + ), + migrations.AlterField( + model_name='euro2019interest', + name='want_info', + field=models.BooleanField(default=True, verbose_name='Chci dostávat informace e-mailem'), + ), + ] diff --git a/src/records_audit/utils.py b/src/records_audit/utils.py index 635ea58..781fba0 100644 --- a/src/records_audit/utils.py +++ b/src/records_audit/utils.py @@ -2,6 +2,7 @@ import datetime import django from django.db import models as dj_models +from django.apps import apps from django.utils.translation import ugettext_lazy as _ @@ -82,9 +83,13 @@ class DataAudited: # setup our save method self.save = self.__audited_save__ - # - # check and update the object cache - # + def __cache_setup__(self): + """ Check and update the object cache. + + This calculation CAN NOT be done in __init__ because of + possible circular dependencies in the models. + """ + tbl_name = self._meta.db_table if tbl_name in _table_cache: self.__audit_table = _table_cache[tbl_name] @@ -96,13 +101,14 @@ class DataAudited: 'fields' : {}, } ltc = _table_cache[tbl_name] # local table cache variable + self.__audit_table = ltc # load or create AuditTable rslt = models.AuditTable.objects.filter(name=tbl_name) if len(rslt) == 1: tbl = rslt[0] elif len(rslt) == 0: - print("Creating AuditTable", tbl_name ) + #print("Creating AuditTable", tbl_name ) tbl = models.AuditTable() tbl.name = tbl_name tbl.save() @@ -125,7 +131,7 @@ class DataAudited: # create remaining wanted_af (there can be some new) for fld_name in wanted_af: - print("Creating AuditField", tbl_name, fld_name ) + #print("Creating AuditField", tbl_name, fld_name ) fld = models.AuditField(table=tbl, name=fld_name) fld.save() # create cache record @@ -148,14 +154,18 @@ class DataAudited: # probably already in DataAudited with block, just save self.__save(*args, **kwargs) else: - # do the audit - obj_orig = type(self).objects.get(pk=self.id) - self.__audit_start_ts = datetime.datetime.now() - self.__orig_data = extract_object_data(obj_orig) - self.__save(*args, **kwargs) - self.__exit__(None, None, None) - self.__orig_data = None - self.__audit_start_ts = None + if self.id is None: + # just save + self.__save(*args, **kwargs) + else: + # do the audit + obj_orig = type(self).objects.get(pk=self.id) + self.__audit_start_ts = datetime.datetime.now() + self.__orig_data = extract_object_data(obj_orig) + self.__save(*args, **kwargs) + self.__exit__(None, None, None) + self.__orig_data = None + self.__audit_start_ts = None def audit_context(self, user=None): """ Setup self as the context. @@ -215,6 +225,9 @@ class DataAudited: tnum = 1 + if self.__audit_table is None: + self.__cache_setup__() + print("AuditLog", self.__audit_table['db_table'], 'id', self.id) for k in data_diff: print(' %s: \t %s -> %s' % (k, data_diff[k][0], data_diff[k][1]) )