diff --git a/README.md b/README.md index ecb694b6..f1751259 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,11 @@ and `to_dict`. can pass the parameter `skip_save=True` when calling update (useful when you want to avoid calling save signals). + +### log_entries + Returns a list of processed LogEntry objects that contain the history of + the object + ### OrderableModel This model inherits from BaseModel. It adds the `display_order` field to allow diff --git a/base/models.py b/base/models.py index 48b43a71..c718105f 100644 --- a/base/models.py +++ b/base/models.py @@ -7,11 +7,13 @@ import json # django +from django.conf import settings +from django.contrib.admin.models import LogEntry +from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.db import models from django.utils import timezone from django.utils.translation import ugettext_lazy as _ -from django.conf import settings # base from base import utils @@ -157,6 +159,31 @@ def get_full_url(self): site = Site.objects.get_current().domain return 'http://{site}{path}'.format(site=site, path=absolute_url) + def log_entries(self): + """The 'history' of this object.""" + # First check if the user can see this history. + model = self.__class__ + + content_type = ContentType.objects.get_for_model( + model, + for_concrete_model=False + ) + + log_entries = list(LogEntry.objects.filter( + object_id=self.id, + content_type=content_type, + ).select_related().order_by('action_time')) + + for log_entry in log_entries: + parsed_message = log_entry.get_change_message() + + try: + log_entry.parsed_message = json.loads(parsed_message) + except Exception: + log_entry.parsed_message = parsed_message + + return log_entries + class OrderableModel(BaseModel): display_order = models.PositiveSmallIntegerField(