pendingModeration.py 2.48 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
3
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
4
from rest_framework import serializers
Florent Chehab's avatar
Florent Chehab committed
5
6

from backend_app.fields import JSONField
7
8
9
10
11
from backend_app.models.abstract.base import (
    BaseModel,
    BaseModelSerializer,
    BaseModelViewSet,
)
12
from backend_app.permissions.app_permissions import ReadOnly
13
from base_app.models import User
14
15


16
class PendingModeration(BaseModel):
Florent Chehab's avatar
Florent Chehab committed
17
    """
18
    Model to hold models instances that are pending moderation.
Florent Chehab's avatar
Florent Chehab committed
19
20
    """

21
22
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.CharField(max_length=100)  # 100 should be ok
23
    referenced_object = GenericForeignKey("content_type", "object_id")
24

25
26
    updated_by = models.ForeignKey(User, on_delete=models.CASCADE)
    updated_on = models.DateTimeField(null=True)
27

Florent Chehab's avatar
Florent Chehab committed
28
    # Object pending moderation
29
30
    # No validation of the JSON it's used internally only and the data is
    # validated before arriving here.
Florent Chehab's avatar
Florent Chehab committed
31
    new_object = JSONField(default=dict)
32
33

    class Meta:
34
        unique_together = ("content_type", "object_id")
35
36


37
class PendingModerationSerializer(BaseModelSerializer):
Florent Chehab's avatar
Florent Chehab committed
38
39
40
41
    """
    Serializer for the Pending Moderation Model
    """

Florent Chehab's avatar
Florent Chehab committed
42
43
44
45
    content_type = serializers.CharField(read_only=True)
    object_id = serializers.CharField(read_only=True)

    updated_by = serializers.CharField(read_only=True)
46
    updated_on = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
Florent Chehab's avatar
Florent Chehab committed
47
48
49

    new_object = serializers.JSONField(read_only=True)

50
51
    class Meta:
        model = PendingModeration
52
        fields = "__all__"
53
54


55
class PendingModerationViewSet(BaseModelViewSet):
Florent Chehab's avatar
Florent Chehab committed
56
57
58
59
    """
    Viewset for the pending moderation model.
    """

60
61
    queryset = PendingModeration.objects.all()  # pylint: disable=E1101
    serializer_class = PendingModerationSerializer
62
    end_point_route = "pendingModeration"
63
64


65
class PendingModerationObjViewSet(BaseModelViewSet):
66
67
68
69
70
    """
    Viewset to retrieve the pending moderation data for an object
    """

    serializer_class = PendingModerationSerializer
71
72
73
74
    permission_classes = (ReadOnly,)
    end_point_route = (
        r"pendingModerationObj/(?P<content_type_id>[0-9]+)/(?P<object_pk>[0-9A-Za-z]+)"
    )
75
76
77
78
79
80
81

    def get_queryset(self):
        content_type_id = self.kwargs["content_type_id"]
        object_pk = self.kwargs["object_pk"]
        return PendingModeration.objects.filter(
            content_type=content_type_id, object_id=object_pk
        )