1 6
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, ContentType, DELETION
2 6
from django.utils.translation import gettext as _
3

4

5 6
class LoggingMethodMixin:
6
    """
7
    Adds methods that log changes made to users' data.
8

9
    To use this, subclass it and ModelViewSet, and override _get_logging_user(). Ensure
10
    that the viewset you're mixing this into has `self.model` and `self.serializer_class`
11
    attributes.
12
    """
13

14 6
    def _get_logging_user(self):
15
        """Return the user of this logged item. Needs overriding in any subclass."""
16 0
        raise NotImplementedError
17

18 6
    def extra_data(self, data):
19
        """Hook to append more data."""
20 0
        return {}
21

22 6
    def log(self, operation, instance):
23 6
        if instance.__class__.__name__ != 'JobEvent':
24 6
            if operation == ADDITION:
25 6
                action_message = _('Created')
26 6
            if operation == CHANGE:
27 6
                action_message = _('Updated')
28 6
            if operation == DELETION:
29 6
                action_message = _('Deleted')
30 6
            LogEntry.objects.log_action(
31
                user_id=self.request.user.id,
32
                content_type_id=ContentType.objects.get_for_model(instance).pk,
33
                object_id=instance.pk,
34
                object_repr=str(instance),
35
                action_flag=operation,
36
                change_message=action_message + ' ' + str(instance))
37

38 6
    def _log_on_create(self, serializer):
39
        """Log the up-to-date serializer.data."""
40 6
        self.log(operation=ADDITION, instance=serializer.instance)
41

42 6
    def _log_on_update(self, serializer):
43
        """Log data from the updated serializer instance."""
44 6
        self.log(operation=CHANGE, instance=serializer.instance)
45

46 6
    def _log_on_destroy(self, instance):
47
        """Log data from the instance before it gets deleted."""
48 6
        self.log(operation=DELETION, instance=instance)
49

50

51 6
class LoggingViewSetMixin(LoggingMethodMixin):
52
    """
53
    A viewset that logs changes made to users' data.
54

55
    To use this, subclass it and ModelViewSet, and override _get_logging_user(). Ensure
56
    that the viewset you're mixing this into has `self.model` and `self.serializer_class`
57
    attributes.
58

59
    If you modify any of the following methods, be sure to call super() or the
60
    corresponding _log_on_X method:
61
    - perform_create
62
    - perform_update
63
    - perform_destroy
64
    """
65

66 6
    def perform_create(self, serializer):
67
        """Create an object and log its data."""
68 6
        super().perform_create(serializer)
69 6
        self._log_on_create(serializer)
70

71 6
    def perform_update(self, serializer):
72
        """Update the instance and log the updated data."""
73 6
        super().perform_update(serializer)
74 6
        self._log_on_update(serializer)
75

76 6
    def perform_destroy(self, instance):
77
        """Delete the instance and log the deletion."""
78 6
        super().perform_destroy(instance)
79 6
        self._log_on_destroy(instance)

Read our documentation on viewing source code .

Loading