Records_audit: oprava zacykleni pri prazdne databazi

manage.py migrate padalo, pokud v DataAudited.__init__
bylo volani do DB AuditTable (ci jinych) modelu.
This commit is contained in:
Martin Rejman 2019-04-12 22:16:28 +02:00
parent 950c00d1cb
commit 0a8f0b026f
2 changed files with 48 additions and 13 deletions

View File

@ -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'),
),
]

View File

@ -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]) )