From f5aed059bfad48b205e2bb35c5f9d2d1baed3342 Mon Sep 17 00:00:00 2001 From: Jan-Jelle Kester Date: Wed, 11 Dec 2013 17:14:07 +0100 Subject: [PATCH] Add changes_dict and changes_str utility methods to LogEntry model --- src/auditlog/models.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/auditlog/models.py b/src/auditlog/models.py index 7e3e4a1..f984b67 100644 --- a/src/auditlog/models.py +++ b/src/auditlog/models.py @@ -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):