@ -42,6 +42,11 @@ class Create(
|
||||
|
||||
try:
|
||||
|
||||
if hasattr(self.model, 'context'):
|
||||
|
||||
self.model.context['user'] = self.request.user
|
||||
self.model.context['logging'] = self.get_log()
|
||||
|
||||
response = super().create(request = request, *args, **kwargs)
|
||||
|
||||
if str(response.status_code).startswith('2'):
|
||||
@ -129,6 +134,11 @@ class Destroy(
|
||||
|
||||
try:
|
||||
|
||||
if hasattr(self.model, 'context'):
|
||||
|
||||
self.model.context['user'] = self.request.user
|
||||
self.model.context['logging'] = self.get_log()
|
||||
|
||||
response = super().destroy(request = request, *args, **kwargs)
|
||||
|
||||
except Exception as e:
|
||||
@ -182,6 +192,11 @@ class List(
|
||||
|
||||
try:
|
||||
|
||||
if hasattr(self.model, 'context'):
|
||||
|
||||
self.model.context['user'] = self.request.user
|
||||
self.model.context['logging'] = self.get_log()
|
||||
|
||||
response = super().list(request = request, *args, **kwargs)
|
||||
|
||||
except Exception as e:
|
||||
@ -238,6 +253,11 @@ class Retrieve(
|
||||
|
||||
try:
|
||||
|
||||
if hasattr(self.model, 'context'):
|
||||
|
||||
self.model.context['user'] = self.request.user
|
||||
self.model.context['logging'] = self.get_log()
|
||||
|
||||
response = super().retrieve(request = request, *args, **kwargs)
|
||||
|
||||
except Exception as e:
|
||||
@ -290,6 +310,11 @@ class Update(
|
||||
|
||||
try:
|
||||
|
||||
if hasattr(self.model, 'context'):
|
||||
|
||||
self.model.context['user'] = self.request.user
|
||||
self.model.context['logging'] = self.get_log()
|
||||
|
||||
response = super().partial_update(request = request, *args, **kwargs)
|
||||
|
||||
if str(response.status_code).startswith('2'):
|
||||
@ -360,6 +385,11 @@ class Update(
|
||||
|
||||
try:
|
||||
|
||||
if hasattr(self.model, 'context'):
|
||||
|
||||
self.model.context['user'] = self.request.user
|
||||
self.model.context['logging'] = self.get_log()
|
||||
|
||||
response = super().update(request = request, *args, **kwargs)
|
||||
|
||||
if str(response.status_code).startswith('2'):
|
||||
|
@ -1,5 +1,6 @@
|
||||
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
|
||||
@ -26,6 +27,14 @@ 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,
|
||||
|
Reference in New Issue
Block a user