pendingModeration.py 1.84 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
9
from backend_app.fields import JSONField
from backend_app.utils import get_model_config, get_viewset_permissions
from rest_framework import 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.
    """

Florent Chehab's avatar
Florent Chehab committed
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
Florent Chehab's avatar
Florent Chehab committed
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
    """

Florent Chehab's avatar
Florent Chehab committed
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)
Florent Chehab's avatar
Florent Chehab committed
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