pendingModeration.py 2.38 KB
Newer Older
1
from base_app.models import User
Florent Chehab's avatar
Florent Chehab committed
2 3 4 5
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
6
from backend_app.custom import MySerializerWithJSON
Florent Chehab's avatar
Florent Chehab committed
7 8
from backend_app.fields import JSONField
from backend_app.utils import get_model_config, get_viewset_permissions
9
from rest_framework import serializers, viewsets, mixins
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
    model_config = get_model_config("PendingModeration")
18

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

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

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

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


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

38 39 40 41
    content_type = serializers.CharField(read_only=True)
    object_id = serializers.CharField(read_only=True)

    updated_by = serializers.CharField(read_only=True)
42
    updated_on = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
43 44 45

    new_object = serializers.JSONField(read_only=True)

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


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

56
    permission_classes = get_viewset_permissions("PendingModerationViewSet")
57 58
    queryset = PendingModeration.objects.all()  # pylint: disable=E1101
    serializer_class = PendingModerationSerializer
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74


class PendingModerationObjViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    Viewset to retrieve the pending moderation data for an object
    """

    permission_classes = get_viewset_permissions("PendingModerationObjViewset")
    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
        )