Skip to content

Commit

Permalink
Added history tracking to InheritanceGroup
Browse files Browse the repository at this point in the history
`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 jazzband/django-simple-history#399 is resolved
(likely through jazzband/django-simple-history#932), it will.
  • Loading branch information
ddabble committed Apr 6, 2022
1 parent 5156606 commit 079d115
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion groups/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .models import Committee, InheritanceGroup


class InheritanceGroupAdmin(admin.ModelAdmin):
class InheritanceGroupAdmin(SimpleHistoryAdmin):
list_display = ('name', 'last_modified')

fieldsets = (
Expand Down
36 changes: 36 additions & 0 deletions groups/migrations/0011_historicalinheritancegroup.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
2 changes: 2 additions & 0 deletions groups/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 079d115

Please sign in to comment.