import uuid from django.contrib.postgres.fields import ArrayField from django.db import models from auditlog.models import AuditlogHistoryField from auditlog.registry import AuditlogModelRegistry, auditlog m2m_only_auditlog = AuditlogModelRegistry(create=False, update=False, delete=False) @auditlog.register() class SimpleModel(models.Model): """ A simple model with no special things going on. """ text = models.TextField(blank=True) boolean = models.BooleanField(default=False) integer = models.IntegerField(blank=True, null=True) datetime = models.DateTimeField(auto_now=True) history = AuditlogHistoryField() class AltPrimaryKeyModel(models.Model): """ A model with a non-standard primary key. """ key = models.CharField(max_length=100, primary_key=True) text = models.TextField(blank=True) boolean = models.BooleanField(default=False) integer = models.IntegerField(blank=True, null=True) datetime = models.DateTimeField(auto_now=True) history = AuditlogHistoryField(pk_indexable=False) class UUIDPrimaryKeyModel(models.Model): """ A model with a UUID primary key. """ id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) text = models.TextField(blank=True) boolean = models.BooleanField(default=False) integer = models.IntegerField(blank=True, null=True) datetime = models.DateTimeField(auto_now=True) history = AuditlogHistoryField(pk_indexable=False) class ProxyModel(SimpleModel): """ A model that is a proxy for another model. """ class Meta: proxy = True class RelatedModel(models.Model): """ A model with a foreign key. """ related = models.ForeignKey(to="self", on_delete=models.CASCADE) history = AuditlogHistoryField() class ManyRelatedModel(models.Model): """ A model with a many to many relation. """ related = models.ManyToManyField("self") history = AuditlogHistoryField() class FirstManyRelatedModel(models.Model): """ A model with a many to many relation to another model similar. """ related = models.ManyToManyField("OtherManyRelatedModel", related_name="related") history = AuditlogHistoryField() class OtherManyRelatedModel(models.Model): """ A model that 'receives' the other side of the many to many relation from 'FirstManyRelatedModel'. """ history = AuditlogHistoryField() @auditlog.register(include_fields=["label"]) class SimpleIncludeModel(models.Model): """ A simple model used for register's include_fields kwarg """ label = models.CharField(max_length=100) text = models.TextField(blank=True) history = AuditlogHistoryField() class SimpleExcludeModel(models.Model): """ A simple model used for register's exclude_fields kwarg """ label = models.CharField(max_length=100) text = models.TextField(blank=True) history = AuditlogHistoryField() class SimpleMappingModel(models.Model): """ A simple model used for register's mapping_fields kwarg """ sku = models.CharField(max_length=100) vtxt = models.CharField(verbose_name="Version", max_length=100) not_mapped = models.CharField(max_length=100) history = AuditlogHistoryField() class AdditionalDataIncludedModel(models.Model): """ A model where get_additional_data is defined which allows for logging extra information about the model in JSON """ label = models.CharField(max_length=100) text = models.TextField(blank=True) related = models.ForeignKey(to=SimpleModel, on_delete=models.CASCADE) history = AuditlogHistoryField() def get_additional_data(self): """ Returns JSON that captures a snapshot of additional details of the model instance. This method, if defined, is accessed by auditlog manager and added to each logentry instance on creation. """ object_details = { "related_model_id": self.related.id, "related_model_text": self.related.text, } return object_details class DateTimeFieldModel(models.Model): """ A model with a DateTimeField, used to test DateTimeField changes are detected properly. """ label = models.CharField(max_length=100) timestamp = models.DateTimeField() date = models.DateField() time = models.TimeField() naive_dt = models.DateTimeField(null=True, blank=True) history = AuditlogHistoryField() class ChoicesFieldModel(models.Model): """ A model with a CharField restricted to a set of choices. This model is used to test the changes_display_dict method. """ RED = "r" YELLOW = "y" GREEN = "g" STATUS_CHOICES = ( (RED, "Red"), (YELLOW, "Yellow"), (GREEN, "Green"), ) status = models.CharField(max_length=1, choices=STATUS_CHOICES) multiplechoice = models.CharField(max_length=255, choices=STATUS_CHOICES) history = AuditlogHistoryField() class CharfieldTextfieldModel(models.Model): """ A model with a max length CharField and a Textfield. This model is used to test the changes_display_dict method's ability to truncate long text. """ longchar = models.CharField(max_length=255) longtextfield = models.TextField() history = AuditlogHistoryField() class PostgresArrayFieldModel(models.Model): """ Test auditlog with Postgres's ArrayField """ RED = "r" YELLOW = "y" GREEN = "g" STATUS_CHOICES = ( (RED, "Red"), (YELLOW, "Yellow"), (GREEN, "Green"), ) arrayfield = ArrayField( models.CharField(max_length=1, choices=STATUS_CHOICES), size=3 ) history = AuditlogHistoryField() class NoDeleteHistoryModel(models.Model): integer = models.IntegerField(blank=True, null=True) history = AuditlogHistoryField(delete_related=False) auditlog.register(AltPrimaryKeyModel) auditlog.register(UUIDPrimaryKeyModel) auditlog.register(ProxyModel) auditlog.register(RelatedModel) auditlog.register(ManyRelatedModel) auditlog.register(ManyRelatedModel.related.through) m2m_only_auditlog.register( FirstManyRelatedModel, include_fields=["pk", "history"], m2m_fields={"related"} ) auditlog.register(SimpleExcludeModel, exclude_fields=["text"]) auditlog.register(SimpleMappingModel, mapping_fields={"sku": "Product No."}) auditlog.register(AdditionalDataIncludedModel) auditlog.register(DateTimeFieldModel) auditlog.register(ChoicesFieldModel) auditlog.register(CharfieldTextfieldModel) auditlog.register(PostgresArrayFieldModel) auditlog.register(NoDeleteHistoryModel)