Fakultas Ilmu Komputer UI

serializers.py 1.59 KB
Newer Older
1
2
3
from rest_framework import serializers

from apps.accounts.serializers import AccountSerializer
4
5
6
7
8
9
10
11
12
13
14
15
from apps.accounts.models import AccountHistory, Account
from apps.cases.models import (
    CaseSubjectHistory,
    InvestigationCaseHistory,
    MonitoringCaseHistory,
)
from apps.constants import (
    MODEL_NAME_ACCOUNT,
    MODEL_NAME_CASE_SUBJECT,
    MODEL_NAME_INVESTIGATION_CASE,
    MODEL_NAME_MONITORING_CASE,
)
16
17


18
19
20
21
22
23
24
class LogSerializer(serializers.Serializer):
    author = serializers.SerializerMethodField()
    model_name = serializers.SerializerMethodField()
    revision_id = serializers.UUIDField()
    object_id = serializers.UUIDField()
    action_type = serializers.CharField()
    recorded_at = serializers.DateTimeField()
25
26
27
28
29

    class Meta:
        fields = [
            "revision_id",
            "object_id",
30
            "model_name",
31
            "action_type",
32
            "recorded_at",
33
34
35
            "author",
        ]

36
37
38
39
40
41
42
43
    def get_model_name(self, instance):
        if isinstance(instance, AccountHistory):
            return MODEL_NAME_ACCOUNT
        if isinstance(instance, CaseSubjectHistory):
            return MODEL_NAME_CASE_SUBJECT
        if isinstance(instance, InvestigationCaseHistory):
            return MODEL_NAME_INVESTIGATION_CASE
        if isinstance(instance, MonitoringCaseHistory):
Irwanto's avatar
Irwanto committed
44
            return MODEL_NAME_MONITORING_CASE
45
46
47
48
49
50
51
52

    def get_author(self, instance):
        if isinstance(instance.author, Account):
            author = instance.author
        else:
            author = Account.objects.filter(id=instance.author).first()

        return AccountSerializer(author).data