70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
import datetime
|
|
import django
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
class AuditTable(models.Model):
|
|
""" Translation table for names of audited tables
|
|
"""
|
|
name = models.CharField(_('Table name'), max_length=150)
|
|
|
|
class Meta:
|
|
unique_together = (('name',),)
|
|
|
|
|
|
class AuditField(models.Model):
|
|
""" Audited fields in the table.
|
|
"""
|
|
table = models.ForeignKey(AuditTable, verbose_name=_('Audited table'), on_delete=models.PROTECT)
|
|
name = models.CharField(_('Field name'), max_length=150)
|
|
|
|
class Meta:
|
|
unique_together = (('table','name',),)
|
|
|
|
|
|
class AuditLog(models.Model):
|
|
""" Track changes in records. """
|
|
|
|
ts = models.DateTimeField(_('Timestamp'),
|
|
null=False, editable=False)
|
|
|
|
tnum = models.IntegerField(_('Transaction number'), default = "-1",
|
|
null=False, editable=False)
|
|
""" This number is unique within one AuditContext run. """
|
|
|
|
user_id = models.IntegerField(verbose_name=_('User ID'),
|
|
null=True, editable=False)
|
|
""" The user who performed this action. None, if unknown.
|
|
|
|
The ID stored is the user-id from the get_user_model() table. Due to
|
|
circular reference, when auditing get_user_model() table, this is just
|
|
plain integer, not ForeignKey.
|
|
"""
|
|
|
|
table = models.ForeignKey(AuditTable, verbose_name=_('Audited table'), on_delete=models.PROTECT,
|
|
null=False, editable=False)
|
|
|
|
fld = models.ForeignKey(AuditField, verbose_name=_('Audited field'), on_delete=models.PROTECT,
|
|
null=False, editable=False)
|
|
|
|
rid = models.IntegerField(_('Table record ID'),
|
|
null=False, editable=False)
|
|
|
|
val = models.CharField(_('Field value'), max_length=500, null=True)
|
|
""" The latest value is stored in the actual model table. This value
|
|
is the previous one.
|
|
|
|
The store timestamp is the last Datetime when the value Val was valid.
|
|
"""
|
|
|
|
class Meta:
|
|
ordering = ('ts', 'tnum',)
|
|
""" We order by transaction start timestamp, then by 'tnum', because
|
|
a nested transaction can be saved earlier than the parent object.
|
|
"""
|
|
|
|
|