Add changes_dict and changes_str utility methods to LogEntry model

This commit is contained in:
Jan-Jelle Kester 2013-12-11 17:14:07 +01:00
parent 8c541fe416
commit f5aed059bf

View file

@ -99,6 +99,29 @@ class LogEntry(models.Model):
fstring = _("Logged {repr:s}")
return fstring.format(repr=self.object_repr)
@property
def changes_dict(self):
try:
return json.loads(self.changes)
except ValueError:
return {}
@property
def changes_str(self, colon=': ', arrow=u' \u2192 ', separator='; '):
substrings = []
for field, values in self.changes_dict.iteritems():
substring = u'{fieldname:s}{colon:s}{old:s}{arrow:s}{new:s}'.format(
fieldname=field,
colon=colon,
old=values[0],
arrow=arrow,
new=values[1],
)
substrings.append(substring)
return separator.join(substrings)
class AuditlogHistoryField(generic.GenericRelation):