views: fix history tracking to work with current django-reversion

We use django-reversion to track history of admin/maintainer changes to
the site, and part of our extension on top of django-reversion involves
annotating each "revision" with a description of the changes that were
made. In django-reversion 2.0.0+ the pre_revision_commit signal that we
were using to do this annotation is gone in favour of just using
Django's standard pre-save signal. This was a little challenging to
adapt to for our purposes but not impossible.

Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
This commit is contained in:
Paul Eggleton 2018-04-13 12:33:01 +12:00
parent e3afa843f4
commit 375e7587d1

View File

@ -31,7 +31,7 @@ from . import tasks
import settings import settings
from django.dispatch import receiver from django.dispatch import receiver
import reversion import reversion
from django.db.models.signals import pre_save
def edit_layernote_view(request, template_name, slug, pk=None): def edit_layernote_view(request, template_name, slug, pk=None):
layeritem = get_object_or_404(LayerItem, name=slug) layeritem = get_object_or_404(LayerItem, name=slug)
@ -801,25 +801,23 @@ class EditProfileFormView(UpdateView):
return reverse('frontpage') return reverse('frontpage')
@receiver(reversion.pre_revision_commit) @receiver(pre_save, sender=reversion.models.Version)
def annotate_revision(sender, **kwargs): def annotate_revision_version(sender, instance, *args, **kwargs):
ignorefields = ['vcs_last_rev', 'vcs_last_fetch', 'vcs_last_commit', 'updated'] ignorefields = ['vcs_last_rev', 'vcs_last_fetch', 'vcs_last_commit', 'updated']
versions = kwargs.pop('versions')
instances = kwargs.pop('instances')
changelist = [] changelist = []
for ver, inst in zip(versions, instances): objclass = instance.content_type.model_class()
currentVersion = ver.field_dict currentVersion = instance.field_dict
modelmeta = ver.content_type.model_class()._meta
#FIXME modern django-reversion dropped the type field (argh!) #FIXME modern django-reversion dropped the type field (argh!)
#if ver.type == reversion.models.VERSION_DELETE: #if instance.type == reversion.models.VERSION_DELETE:
# changelist.append("Deleted %s: %s" % (modelmeta.verbose_name.lower(), ver.object_repr)) # changelist.append("Deleted %s: %s" % (modelmeta.verbose_name.lower(), instance.object_repr))
#else: #else:
pastver = reversion.get_for_object(inst) pastver = reversion.models.Version.objects.filter(content_type=instance.content_type, object_id=instance.object_id).order_by('-id').first()
if pastver:# and ver.type != reversion.models.VERSION_ADD: if pastver:# and instance.type != reversion.models.VERSION_ADD:
pastVersion = pastver[0].field_dict pastVersion = pastver.field_dict
changes = set(currentVersion.items()) - set(pastVersion.items()) changes = set(currentVersion.items()) - set(pastVersion.items())
changedVars = [var[0] for var in changes] changedVars = [var[0] for var in changes]
fieldchanges = [] fieldchanges = []
modelmeta = objclass._meta
for field in changedVars: for field in changedVars:
if field not in ignorefields: if field not in ignorefields:
modelfield = modelmeta.get_field(field) modelfield = modelmeta.get_field(field)
@ -831,16 +829,24 @@ def annotate_revision(sender, **kwargs):
break break
fieldchanges.append("%s to '%s'" % (modelfield.verbose_name.lower(), newvalue)) fieldchanges.append("%s to '%s'" % (modelfield.verbose_name.lower(), newvalue))
if fieldchanges: if fieldchanges:
changelist.append("Changed %s %s %s" % (modelmeta.verbose_name.lower(), ver.object_repr, ", ".join(fieldchanges))) changelist.append("Changed %s %s %s" % (modelmeta.verbose_name.lower(), instance.object_repr, ", ".join(fieldchanges)))
if changelist:
if not instance.revision.comment or instance.revision.comment == 'No changes':
instance.revision.comment = '\n'.join(changelist)
else: else:
changelist.append("Added %s: %s" % (modelmeta.verbose_name.lower(), ver.object_repr)) instance.revision.comment = instance.revision.comment + '\n' + ('\n'.join(changelist))
comment = '\n'.join(changelist) instance.revision.save()
if not comment:
comment = 'No changes'
revision = kwargs.pop('revision') @receiver(pre_save, sender=reversion.models.Revision)
revision.comment = comment def annotate_revision(sender, instance, *args, **kwargs):
revision.save() if instance.pk is None:
kwargs['revision'] = revision # When you make changes in the admin site the comment gets set to just
# specify the field that was changed, but that's not enough detail.
# For changes elsewhere it'll be blank since we aren't creating a revision
# explicitly. Thus, set the comment to a default value and we'll fill it in
# ourselves using the Version pre-save signal handler above.
instance.comment = 'No changes'
class RecipeDetailView(DetailView): class RecipeDetailView(DetailView):