pendingModeration.py 2.37 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
Florent Chehab's avatar
Florent Chehab committed
29
    new_object = JSONField(default=dict)
30
31

    class Meta:
32
        unique_together = ("content_type", "object_id")
33
34


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

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

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

    new_object = serializers.JSONField(read_only=True)

48
49
    class Meta:
        model = PendingModeration
50
        fields = "__all__"
51
52


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

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


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

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

    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
        )