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

from backend_app.fields import JSONField
6
7
8
9
10
from backend_app.models.abstract.base import (
    BaseModel,
    BaseModelSerializer,
    BaseModelViewSet,
)
11
from base_app.models import User
12
from rest_framework import serializers
13
14


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

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

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

Florent Chehab's avatar
Florent Chehab committed
27
    # Object pending moderation
Florent Chehab's avatar
Florent Chehab committed
28
    new_object = JSONField(default=dict)
29
30

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


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

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

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

    new_object = serializers.JSONField(read_only=True)

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


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

57
58
    queryset = PendingModeration.objects.all()  # pylint: disable=E1101
    serializer_class = PendingModerationSerializer
59
60


61
class PendingModerationObjViewSet(BaseModelViewSet):
62
63
64
65
66
67
68
69
70
71
72
73
    """
    Viewset to retrieve the pending moderation data for an object
    """

    serializer_class = PendingModerationSerializer

    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
        )