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

Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.custom import MySerializerWithJSON
Florent Chehab's avatar
Florent Chehab committed
6
from backend_app.fields import JSONField
7
8
9
from backend_app.permissions import get_viewset_permissions
from base_app.models import User
from rest_framework import mixins, serializers, viewsets
10
11
12


class PendingModeration(models.Model):
Florent Chehab's avatar
Florent Chehab committed
13
14
15
16
    """
    Model to hold models that are pending moderation.
    """

17
18
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.CharField(max_length=100)  # 100 should be ok
19
    referenced_object = GenericForeignKey("content_type", "object_id")
20

21
22
    updated_by = models.ForeignKey(User, on_delete=models.CASCADE)
    updated_on = models.DateTimeField(null=True)
23

Florent Chehab's avatar
Florent Chehab committed
24
    # Object pending moderation
Florent Chehab's avatar
Florent Chehab committed
25
    new_object = JSONField(default=dict)
26
27

    class Meta:
28
        unique_together = ("content_type", "object_id")
29
30


Florent Chehab's avatar
Florent Chehab committed
31
class PendingModerationSerializer(MySerializerWithJSON):
Florent Chehab's avatar
Florent Chehab committed
32
33
34
35
    """
    Serializer for the Pending Moderation Model
    """

Florent Chehab's avatar
Florent Chehab committed
36
37
38
39
    content_type = serializers.CharField(read_only=True)
    object_id = serializers.CharField(read_only=True)

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

    new_object = serializers.JSONField(read_only=True)

44
45
    class Meta:
        model = PendingModeration
46
        fields = "__all__"
47
48
49


class PendingModerationViewSet(viewsets.ModelViewSet):
Florent Chehab's avatar
Florent Chehab committed
50
51
52
53
    """
    Viewset for the pending moderation model.
    """

54
55
    # Since PendingModerationViewSet doesn't inherit from MyModelViewset
    # We need to link here the correct permissions
56
    permission_classes = get_viewset_permissions("PendingModerationViewSet")
57
58
    queryset = PendingModeration.objects.all()  # pylint: disable=E1101
    serializer_class = PendingModerationSerializer
59
60


61
class PendingModerationObjViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
62
63
64
65
    """
    Viewset to retrieve the pending moderation data for an object
    """

66
67
68
    # Since PendingModerationObjViewSet doesn't inherit from MyModelViewset
    # We need to link here the correct permissions
    permission_classes = get_viewset_permissions("PendingModerationObjViewSet")
69
70
71
72
73
74
75
76
    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
        )