Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangquochung1110 committed Apr 2, 2024
1 parent 96c4355 commit e036e36
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 31 deletions.
1 change: 0 additions & 1 deletion auditlog/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.conf import settings


# Register all models when set to True
settings.AUDITLOG_INCLUDE_ALL_MODELS = getattr(
settings, "AUDITLOG_INCLUDE_ALL_MODELS", False
Expand Down
2 changes: 1 addition & 1 deletion auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def changes_display_dict(self):
auditlog = AuditlogRegistry.get_auditlog(model=model)

model_fields = None
if auditlog.contains(model._meta.model):
if auditlog:
model_fields = auditlog.get_model_fields(model._meta.model)

changes_display_dict = {}
Expand Down
12 changes: 7 additions & 5 deletions auditlog/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,18 +401,20 @@ def get_auditlog(cls, model: ModelBase):
Retrieve the auditlog object associated with a given model.
"""
auditlogs = get_default_auditlogs()
return next(
(auditlog for auditlog in auditlogs if auditlog._registry.get(model)), None
)
for auditlog in auditlogs:
if auditlog.contains(model):
return auditlog

@classmethod
def get_model_fields(cls, model: ModelBase):
"""
Wrapper of AuditlogModelRegistry.get_model_fields().
"""
auditlog = cls.get_auditlog(model)
model_fields = auditlog.get_model_fields(model)
return model_fields
if auditlog:
model_fields = auditlog.get_model_fields(model)
return model_fields
return {}


auditlog_registry = AuditlogRegistry()
16 changes: 0 additions & 16 deletions auditlog_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,6 @@ class AutoManyRelatedModel(models.Model):
related = models.ManyToManyField(SimpleModel)


class CreateUpdateOnlyModel(models.Model):
"""
A simple model to test create_only_auditlog and update_only_auditlog.
"""

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(delete_related=True)

def __str__(self):
return self.text


auditlog.register(AltPrimaryKeyModel)
auditlog.register(UUIDPrimaryKeyModel)
auditlog.register(ProxyModel)
Expand Down
14 changes: 6 additions & 8 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
AutoManyRelatedModel,
CharfieldTextfieldModel,
ChoicesFieldModel,
CreateUpdateOnlyModel,
DateTimeFieldModel,
JSONModel,
ManyRelatedModel,
Expand Down Expand Up @@ -2650,15 +2649,18 @@ def test_get_changes_for_missing_model(self):
class CustomAuditlogTest(TestCase):
def setUp(self):
super().setUp()
if auditlog.contains(SimpleModel):
auditlog.unregister(SimpleModel)

def tearDown(self):
for model in create_only_auditlog.get_models():
create_only_auditlog.unregister(model)
for model in update_only_auditlog.get_models():
update_only_auditlog.unregister(model)
auditlog.register(SimpleModel)

def make_object(self):
return CreateUpdateOnlyModel.objects.create(text="I am not difficult.")
return SimpleModel.objects.create(text="I am not difficult.")

def update_object(self, obj):
obj.text = "Changed"
Expand All @@ -2671,9 +2673,7 @@ def test_create_only_auditlog(self):
"auditlog_tests.test_registry.create_only_auditlog",
]
):
create_only_auditlog.register(
CreateUpdateOnlyModel, include_fields=["text"]
)
create_only_auditlog.register(SimpleModel, include_fields=["text"])

# Get the object to work with
obj = self.make_object()
Expand All @@ -2693,9 +2693,7 @@ def test_update_only_auditlog(self):
"auditlog_tests.test_registry.update_only_auditlog",
]
):
update_only_auditlog.register(
CreateUpdateOnlyModel, include_fields=["text"]
)
update_only_auditlog.register(SimpleModel, include_fields=["text"])

# Get the object to work with
obj = self.make_object()
Expand Down

0 comments on commit e036e36

Please sign in to comment.