Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

add: log_entries method to BaseModel #103

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down