Source code for superdjango.contrib.history.models

# Imports

import difflib
from django.conf import settings
from django.db import models
from django.utils.translation import ugettext_lazy as _
from superdjango.db.history.models import HistoryModel

# Exports

__all__ = (
    "History",
)

# Constants

AUTH_USER_MODEL = settings.AUTH_USER_MODEL

# Models


[docs]class History(HistoryModel): """An entry in the record history timeline.""" field_changes = models.TextField( _("field changes"), blank=True, help_text=_("Fields that changed as part of the history."), null=True ) class Meta: get_latest_by = "added_dt" ordering = ["-added_dt"] verbose_name = _("History Entry") verbose_name_plural = _("History Entries")
[docs] @classmethod def log_field_changes(cls, instance, verb, fields=None): """Log changes to fields to the ``field_changes`` field of the history record.""" if fields is None: return a = list() for change in fields: # Attempt to make text fields or long character fields look good. if len(str(change.new_value)) > 128 or len(str(change.old_value)) > 128: b = list() diff = difflib.Differ() for i in diff.compare(str(change.old_value).splitlines(), str(change.new_value).splitlines()): b.append(i.strip()) a.append(" ".join(b)) else: a.append(str(change)) instance.field_changes = "\n\n".join(a) instance.save(update_fields=["field_changes"])