diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-01-30 16:12:11 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-07-01 18:02:28 +0200 |
commit | a01045cb7b9523f6d0baa8ef9d537a26a189cdb4 (patch) | |
tree | 3cb18e93011af38dd312798dc4cf11091111e5fe /ishtar_common | |
parent | 6567523c3ad53fee87688fff44b9f0c7afe44387 (diff) | |
download | Ishtar-a01045cb7b9523f6d0baa8ef9d537a26a189cdb4.tar.bz2 Ishtar-a01045cb7b9523f6d0baa8ef9d537a26a189cdb4.zip |
Django simple history -> v2.7.0
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/migrations/0202_auto_20200129_1941.py | 99 | ||||
-rw-r--r-- | ishtar_common/models.py | 50 |
2 files changed, 143 insertions, 6 deletions
diff --git a/ishtar_common/migrations/0202_auto_20200129_1941.py b/ishtar_common/migrations/0202_auto_20200129_1941.py index 0e640188a..02f13f170 100644 --- a/ishtar_common/migrations/0202_auto_20200129_1941.py +++ b/ishtar_common/migrations/0202_auto_20200129_1941.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals from django.db import migrations, models +import django.db.models.deletion +from django.conf import settings class Migration(migrations.Migration): @@ -22,4 +24,101 @@ class Migration(migrations.Migration): name='history_change_reason', field=models.CharField(max_length=100, null=True), ), + migrations.AlterField( + model_name='historicalorganization', + name='history_creator', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to=settings.AUTH_USER_MODEL, + verbose_name='Creator'), + ), + migrations.AlterField( + model_name='historicalorganization', + name='history_modifier', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to=settings.AUTH_USER_MODEL, + verbose_name='Last editor'), + ), + migrations.AlterField( + model_name='historicalorganization', + name='lock_user', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to=settings.AUTH_USER_MODEL, + verbose_name='Locked by'), + ), + migrations.AlterField( + model_name='historicalorganization', + name='organization_type', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to='ishtar_common.OrganizationType', + verbose_name='Type'), + ), + migrations.AlterField( + model_name='historicalorganization', + name='precise_town', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', to='ishtar_common.Town', + verbose_name='Town (precise)'), + ), + migrations.AlterField( + model_name='historicalperson', + name='attached_to', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to='ishtar_common.Organization', + verbose_name='Is attached to'), + ), + migrations.AlterField( + model_name='historicalperson', + name='history_creator', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to=settings.AUTH_USER_MODEL, + verbose_name='Creator'), + ), + migrations.AlterField( + model_name='historicalperson', + name='history_modifier', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to=settings.AUTH_USER_MODEL, + verbose_name='Last editor'), + ), + migrations.AlterField( + model_name='historicalperson', + name='lock_user', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to=settings.AUTH_USER_MODEL, + verbose_name='Locked by'), + ), + migrations.AlterField( + model_name='historicalperson', + name='precise_town', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', to='ishtar_common.Town', + verbose_name='Town (precise)'), + ), + migrations.AlterField( + model_name='historicalperson', + name='title', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name='+', + to='ishtar_common.TitleType', + verbose_name='Title'), + ), ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index d0612d648..3900611b6 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -74,6 +74,8 @@ from ishtar_common.utils import ugettext_lazy as _, ugettext, \ pgettext_lazy from ishtar_common.utils_secretary import IshtarSecretaryRenderer from simple_history.models import HistoricalRecords as BaseHistoricalRecords +from simple_history.signals import post_create_historical_record, \ + pre_create_historical_record from unidecode import unidecode from ishtar_common.alternative_configs import ALTERNATE_CONFIGS, \ @@ -233,13 +235,48 @@ class HistoryModel(models.Model): class HistoricalRecords(BaseHistoricalRecords): - def create_historical_record(self, instance, type): + def _save_historic(self, manager, instance, history_date, history_type, + history_user, history_change_reason, using, attrs): + history_instance = manager.model( + history_date=history_date, + history_type=history_type, + history_user=history_user, + history_change_reason=history_change_reason, + **attrs + ) + + pre_create_historical_record.send( + sender=manager.model, + instance=instance, + history_date=history_date, + history_user=history_user, + history_change_reason=history_change_reason, + history_instance=history_instance, + using=using, + ) + + history_instance.save(using=using) + + post_create_historical_record.send( + sender=manager.model, + instance=instance, + history_instance=history_instance, + history_date=history_date, + history_user=history_user, + history_change_reason=history_change_reason, + using=using, + ) + + def create_historical_record(self, instance, history_type, using=None): try: history_modifier = getattr(instance, 'history_modifier', None) assert history_modifier except (User.DoesNotExist, AssertionError): # on batch removing of users, user could have disappeared return + history_date = getattr(instance, "_history_date", + datetime.datetime.now()) + history_change_reason = getattr(instance, "changeReason", None) force = getattr(instance, "_force_history", False) manager = getattr(instance, self.manager_name) attrs = {} @@ -252,8 +289,9 @@ class HistoricalRecords(BaseHistoricalRecords): if not q_history.count(): if force: delattr(instance, '_force_history') - manager.create(history_type=type, - history_date=datetime.datetime.now(), **attrs) + self._save_historic( + manager, instance, history_date, history_type, history_modifier, + history_change_reason, using, attrs) return old_instance = q_history.all()[0] # multiple saving by the same user in a very short time are generaly @@ -269,12 +307,12 @@ class HistoricalRecords(BaseHistoricalRecords): if force: delattr(instance, '_force_history') - if 'history_date' not in attrs or not attrs['history_date']: - attrs['history_date'] = datetime.datetime.now() # record a new version only if data have been changed for field in instance._meta.fields: if getattr(old_instance, field.attname) != attrs[field.attname]: - manager.create(history_type=type, **attrs) + self._save_historic(manager, instance, history_date, + history_type, history_modifier, + history_change_reason, using, attrs) return |