From 79bd0d7d2bc2474f391a7e0a1270a5eff8d19891 Mon Sep 17 00:00:00 2001 From: Anders <6058745+ddabble@users.noreply.github.com> Date: Mon, 21 Nov 2022 04:53:06 +0100 Subject: [PATCH] Added history tracking to ManyToManyFields ...that exist on models that already have history tracking. Note that the many-to-many selection for these fields *are* correctly saved, they're just currently not correctly displayed in the objects' history page in Django admin (the *current* M2M selection is always shown); see https://github.com/jazzband/django-simple-history/issues/1063. --- ...icalcontentbox_extra_change_permissions.py | 62 ++++++++++++++++++ contentbox/models.py | 2 +- .../0006_historicalquestion_categories.py | 61 ++++++++++++++++++ faq/models.py | 2 +- ...toricalinheritancegroup_own_permissions.py | 64 +++++++++++++++++++ groups/models.py | 3 +- 6 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 contentbox/migrations/0015_historicalcontentbox_extra_change_permissions.py create mode 100644 faq/migrations/0006_historicalquestion_categories.py create mode 100644 groups/migrations/0013_historicalinheritancegroup_own_permissions.py diff --git a/contentbox/migrations/0015_historicalcontentbox_extra_change_permissions.py b/contentbox/migrations/0015_historicalcontentbox_extra_change_permissions.py new file mode 100644 index 000000000..39376a760 --- /dev/null +++ b/contentbox/migrations/0015_historicalcontentbox_extra_change_permissions.py @@ -0,0 +1,62 @@ +# Generated by Django 4.1.3 on 2022-11-21 03:42 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("auth", "0012_alter_user_first_name_max_length"), + ("contentbox", "0014_history_date_db_index"), + ] + + operations = [ + migrations.CreateModel( + name="HistoricalContentBox_extra_change_permissions", + fields=[ + ( + "id", + models.BigIntegerField( + auto_created=True, blank=True, db_index=True, verbose_name="ID" + ), + ), + ("m2m_history_id", models.AutoField(primary_key=True, serialize=False)), + ( + "contentbox", + models.ForeignKey( + blank=True, + db_constraint=False, + db_tablespace="", + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="+", + to="contentbox.contentbox", + ), + ), + ( + "history", + models.ForeignKey( + db_constraint=False, + on_delete=django.db.models.deletion.DO_NOTHING, + to="contentbox.historicalcontentbox", + ), + ), + ( + "permission", + models.ForeignKey( + blank=True, + db_constraint=False, + db_tablespace="", + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="+", + to="auth.permission", + ), + ), + ], + options={ + "verbose_name": "HistoricalContentBox_extra_change_permissions", + }, + ), + ] diff --git a/contentbox/models.py b/contentbox/models.py index 00f702ae6..67979b6ae 100644 --- a/contentbox/models.py +++ b/contentbox/models.py @@ -28,7 +28,7 @@ class ContentBox(models.Model): ) last_modified = models.DateTimeField(auto_now=True, verbose_name=_("last modified")) - history = HistoricalRecords(excluded_fields=['last_modified']) + history = HistoricalRecords(m2m_fields=[extra_change_permissions], excluded_fields=['last_modified']) class Meta: permissions = ( diff --git a/faq/migrations/0006_historicalquestion_categories.py b/faq/migrations/0006_historicalquestion_categories.py new file mode 100644 index 000000000..cdce863ab --- /dev/null +++ b/faq/migrations/0006_historicalquestion_categories.py @@ -0,0 +1,61 @@ +# Generated by Django 4.1.3 on 2022-11-21 03:42 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("faq", "0005_history_date_db_index"), + ] + + operations = [ + migrations.CreateModel( + name="HistoricalQuestion_categories", + fields=[ + ( + "id", + models.BigIntegerField( + auto_created=True, blank=True, db_index=True, verbose_name="ID" + ), + ), + ("m2m_history_id", models.AutoField(primary_key=True, serialize=False)), + ( + "category", + models.ForeignKey( + blank=True, + db_constraint=False, + db_tablespace="", + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="+", + to="faq.category", + ), + ), + ( + "history", + models.ForeignKey( + db_constraint=False, + on_delete=django.db.models.deletion.DO_NOTHING, + to="faq.historicalquestion", + ), + ), + ( + "question", + models.ForeignKey( + blank=True, + db_constraint=False, + db_tablespace="", + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="+", + to="faq.question", + ), + ), + ], + options={ + "verbose_name": "HistoricalQuestion_categories", + }, + ), + ] diff --git a/faq/models.py b/faq/models.py index 2f896b1a8..5c3f8bf45 100644 --- a/faq/models.py +++ b/faq/models.py @@ -41,7 +41,7 @@ class Question(models.Model): ) last_modified = models.DateTimeField(auto_now=True, verbose_name=_("last modified")) - history = HistoricalRecords(excluded_fields=['last_modified']) + history = HistoricalRecords(m2m_fields=[categories], excluded_fields=['last_modified']) class Meta: verbose_name = _("question") diff --git a/groups/migrations/0013_historicalinheritancegroup_own_permissions.py b/groups/migrations/0013_historicalinheritancegroup_own_permissions.py new file mode 100644 index 000000000..cc2a3166a --- /dev/null +++ b/groups/migrations/0013_historicalinheritancegroup_own_permissions.py @@ -0,0 +1,64 @@ +# Generated by Django 4.1.3 on 2022-11-21 03:42 + +from django.db import migrations, models +import django.db.models.deletion +import simple_history.models + + +class Migration(migrations.Migration): + + dependencies = [ + ("auth", "0012_alter_user_first_name_max_length"), + ("groups", "0012_history_date_db_index"), + ] + + operations = [ + migrations.CreateModel( + name="HistoricalInheritanceGroup_own_permissions", + fields=[ + ( + "id", + models.BigIntegerField( + auto_created=True, blank=True, db_index=True, verbose_name="ID" + ), + ), + ("m2m_history_id", models.AutoField(primary_key=True, serialize=False)), + ( + "history", + models.ForeignKey( + db_constraint=False, + on_delete=django.db.models.deletion.DO_NOTHING, + to="groups.historicalinheritancegroup", + ), + ), + ( + "inheritancegroup", + models.ForeignKey( + blank=True, + db_constraint=False, + db_tablespace="", + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="+", + to="groups.inheritancegroup", + ), + ), + ( + "permission", + models.ForeignKey( + blank=True, + db_constraint=False, + db_tablespace="", + null=True, + on_delete=django.db.models.deletion.DO_NOTHING, + related_name="+", + to="auth.permission", + ), + ), + ], + options={ + "verbose_name": "HistoricalInheritanceGroup_own_permissions", + }, + bases=(simple_history.models.HistoricalChanges, models.Model), + ), + ] diff --git a/groups/models.py b/groups/models.py index 7f42525e9..fa8641f4c 100644 --- a/groups/models.py +++ b/groups/models.py @@ -38,7 +38,8 @@ class InheritanceGroup(Group): ) last_modified = models.DateTimeField(auto_now=True, verbose_name=_("last modified")) - history = HistoricalRecords(excluded_fields=['last_modified']) + # TODO: Add `parents` to `m2m_fields` when https://github.com/jazzband/django-simple-history/issues/1126 is resolved + history = HistoricalRecords(m2m_fields=[own_permissions], excluded_fields=['last_modified']) @property def inherited_permissions(self):