feat(core): If user context not supplied, dont create audithistory for model

this allows tests and direct creation via code if required.

ref: #789 #759
This commit is contained in:
2025-06-06 04:15:13 +09:30
parent a8e2536bd2
commit 7691ad5b0b
9 changed files with 12 additions and 76 deletions

View File

@ -1,21 +1,24 @@
from django.apps import apps
from django.contrib.auth.models import ContentType
from django.core.exceptions import ValidationError
from django.db.models.signals import (
# post_delete,
post_save
)
from django.dispatch import receiver
# @receiver(post_delete, dispatch_uid="audit_history_delete")
@receiver(post_save, dispatch_uid="audit_history_save")
def audit_history(sender, instance, **kwargs):
if getattr(instance, '_audit_enabled', False):
audit_model = apps.get_model( instance._meta.app_label, instance._meta.object_name + 'AuditHistory')
if instance.context.get('user', None) is None:
return
audit_model = apps.get_model(
instance._meta.app_label,
instance._meta.object_name + 'AuditHistory'
)
audit_action = audit_model.Actions.UPDATE
@ -27,14 +30,6 @@ def audit_history(sender, instance, **kwargs):
audit_action = audit_model.Actions.DELETE
if instance.context.get('user', None) is None:
raise ValidationError(
code = 'model_missing_user_context',
message = f'Model {instance._meta.model_name}, is missing user context. ' \
'No audit history can be saved'
)
history = audit_model.objects.create(
organization = instance.organization,

View File

@ -38,13 +38,6 @@ class CenturionAbstractModelTestCases(
'type': bool,
'value': True,
},
'context': {
'type': dict,
'value': {
'logger': None,
'user': None,
}
},
'model_tag': {
'type': str,
},
@ -277,16 +270,10 @@ class CenturionAbstractModelInheritedCases(
})
default_val = model.context['user']
model.context['user'] = user
model_object = model.objects.create(
**kwargs
)
model.context['user'] = default_val
for field, values in many_field.items():
for value in values: