From 079d1150771eb6e70d1547665d3b64c42d2ee4ac Mon Sep 17 00:00:00 2001 From: Dabble Date: Mon, 4 Apr 2022 21:18:48 +0200 Subject: [PATCH] Added history tracking to InheritanceGroup `django-simple-history` doesn't currently track changes to many-to-many relationships - which was the main point of this commit (i.e. to track the changes to `own_permissions`) - but once https://github.com/jazzband/django-simple-history/issues/399 is resolved (likely through https://github.com/jazzband/django-simple-history/pull/932), it will. --- groups/admin.py | 2 +- .../0011_historicalinheritancegroup.py | 36 +++++++++++++++++++ groups/models.py | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 groups/migrations/0011_historicalinheritancegroup.py diff --git a/groups/admin.py b/groups/admin.py index 2b3a6624e..355c93aa2 100644 --- a/groups/admin.py +++ b/groups/admin.py @@ -5,7 +5,7 @@ from .models import Committee, InheritanceGroup -class InheritanceGroupAdmin(admin.ModelAdmin): +class InheritanceGroupAdmin(SimpleHistoryAdmin): list_display = ('name', 'last_modified') fieldsets = ( diff --git a/groups/migrations/0011_historicalinheritancegroup.py b/groups/migrations/0011_historicalinheritancegroup.py new file mode 100644 index 000000000..a8150a169 --- /dev/null +++ b/groups/migrations/0011_historicalinheritancegroup.py @@ -0,0 +1,36 @@ +# Generated by Django 4.0.3 on 2022-04-04 19:12 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import simple_history.models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('groups', '0010_alter_committee_image_filename'), + ] + + operations = [ + migrations.CreateModel( + name='HistoricalInheritanceGroup', + fields=[ + ('group_ptr', models.ForeignKey(auto_created=True, blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, parent_link=True, related_name='+', to='auth.group')), + ('id', models.IntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')), + ('name', models.CharField(db_index=True, max_length=150, verbose_name='name')), + ('history_id', models.AutoField(primary_key=True, serialize=False)), + ('history_date', models.DateTimeField()), + ('history_change_reason', models.CharField(max_length=100, null=True)), + ('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)), + ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)), + ], + options={ + 'verbose_name': 'historical inheritance group', + 'ordering': ('-history_date', '-history_id'), + 'get_latest_by': 'history_date', + }, + bases=(simple_history.models.HistoricalChanges, models.Model), + ), + ] diff --git a/groups/models.py b/groups/models.py index 0a3691498..45b79fb76 100644 --- a/groups/models.py +++ b/groups/models.py @@ -37,6 +37,8 @@ class InheritanceGroup(Group): ) last_modified = models.DateTimeField(auto_now=True, verbose_name=_("last modified")) + history = HistoricalRecords(excluded_fields=['last_modified']) + @property def inherited_permissions(self): return set(self.permissions.all()) - set(self.own_permissions.all())