from datetime import datetime
+from util import DiffableModel
+
# We have few enough of these, and it's really the only thing we
# need to extend from the user model, so just create a separate
# class.
return self.topic
-class Patch(models.Model):
+class Patch(models.Model, DiffableModel):
name = models.CharField(max_length=500, blank=False, null=False, verbose_name='Description')
topic = models.ForeignKey(Topic, blank=False, null=False)
# that's attached to this message.
lastmail = models.DateTimeField(blank=True, null=True)
+ map_manytomany_for_diff = {
+ 'authors': 'authors_string',
+ 'reviewers': 'reviewers_string',
+ }
# Some accessors
@property
def authors_string(self):
--- /dev/null
+from django.forms.models import model_to_dict
+
+
+class DiffableModel(object):
+ """
+ Make it possible to diff a model.
+ """
+
+ def __init__(self, *args, **kwargs):
+ super(DiffableModel, self).__init__(*args, **kwargs)
+ self.__initial = self._dict
+
+ @property
+ def diff(self):
+ manytomanyfieldnames = [f.name for f in self._meta.many_to_many]
+ d1 = self.__initial
+ d2 = self._dict
+ diffs = dict([(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]])
+ if hasattr(self, 'map_manytomany_for_diff'):
+ for k,v in diffs.items():
+ if k in manytomanyfieldnames and self.map_manytomany_for_diff.has_key(k):
+ # Try to show the display name instead here
+ newvalue = getattr(self, self.map_manytomany_for_diff[k])
+ diffs[k] = (v[0], newvalue)
+ return diffs
+
+ def save(self, *args, **kwargs):
+ super(DiffableModel, self).save(*args, **kwargs)
+ self.__initial = self._dict
+
+ @property
+ def _dict(self):
+ fields = [field.name for field in self._meta.fields]
+ fields.extend([field.name for field in self._meta.many_to_many])
+ return model_to_dict(self, fields=fields)
r = form.save(commit=False)
# Fill out any locked fields here
+ form.save_m2m()
+
+ # Track all changes
+ for field, values in r.diff.items():
+ PatchHistory(patch=patch, by=request.user, what='Changed %s to %s' % (field, values[1])).save()
r.set_modified()
r.save()
- form.save_m2m()
return HttpResponseRedirect('../../%s/' % r.pk)
# Else fall through and render the page again
else: