2016-06-22 10:19:00 -04:00
|
|
|
from django.db import models
|
|
|
|
|
2018-06-21 13:24:26 -04:00
|
|
|
from extras.models import ObjectChange
|
2018-06-22 14:00:23 -04:00
|
|
|
from utilities.utils import serialize_object
|
2018-06-21 13:24:26 -04:00
|
|
|
|
2016-06-22 10:19:00 -04:00
|
|
|
|
2020-01-14 12:01:23 -05:00
|
|
|
__all__ = (
|
|
|
|
'ChangeLoggedModel',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-06-13 15:40:16 -04:00
|
|
|
class ChangeLoggedModel(models.Model):
|
|
|
|
"""
|
|
|
|
An abstract model which adds fields to store the creation and last-updated times for an object. Both fields can be
|
|
|
|
null to facilitate adding these fields to existing instances via a database migration.
|
|
|
|
"""
|
|
|
|
created = models.DateField(
|
|
|
|
auto_now_add=True,
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
|
|
|
last_updated = models.DateTimeField(
|
|
|
|
auto_now=True,
|
|
|
|
blank=True,
|
|
|
|
null=True
|
|
|
|
)
|
2016-06-22 10:19:00 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2018-06-21 13:24:26 -04:00
|
|
|
|
2019-08-26 16:52:05 -04:00
|
|
|
def to_objectchange(self, action):
|
2018-06-21 13:24:26 -04:00
|
|
|
"""
|
2019-08-26 16:52:05 -04:00
|
|
|
Return a new ObjectChange representing a change made to this object. This will typically be called automatically
|
2018-06-21 13:24:26 -04:00
|
|
|
by extras.middleware.ChangeLoggingMiddleware.
|
|
|
|
"""
|
2019-08-26 16:52:05 -04:00
|
|
|
return ObjectChange(
|
2018-06-21 13:24:26 -04:00
|
|
|
changed_object=self,
|
2019-08-26 16:52:05 -04:00
|
|
|
object_repr=str(self),
|
2018-06-21 13:24:26 -04:00
|
|
|
action=action,
|
2018-06-22 14:00:23 -04:00
|
|
|
object_data=serialize_object(self)
|
2019-08-26 16:52:05 -04:00
|
|
|
)
|