pendingModeration.py 1.62 KB
Newer Older
1
from django.db import models
2
from rest_framework import serializers, viewsets
3
4
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.fields import JSONField
6
from django.contrib.auth.models import User
Florent Chehab's avatar
Florent Chehab committed
7
8
from backend_app.utils import get_viewset_permissions, get_model_config
from backend_app.custom import MySerializerWithJSON
9
10
11


class PendingModeration(models.Model):
Florent Chehab's avatar
Florent Chehab committed
12
    model_config = get_model_config("PendingModeration")
13

14
15
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.CharField(max_length=100)  # 100 should be ok
16
    referenced_object = GenericForeignKey("content_type", "object_id")
17

18
19
    updated_by = models.ForeignKey(User, on_delete=models.CASCADE)
    updated_on = models.DateTimeField(null=True)
20

Florent Chehab's avatar
Florent Chehab committed
21
    new_object = JSONField(default=dict)
22
23

    class Meta:
24
        unique_together = ("content_type", "object_id")
25
26


Florent Chehab's avatar
Florent Chehab committed
27
class PendingModerationSerializer(MySerializerWithJSON):
Florent Chehab's avatar
Florent Chehab committed
28
29
30
31
    content_type = serializers.CharField(read_only=True)
    object_id = serializers.CharField(read_only=True)

    updated_by = serializers.CharField(read_only=True)
32
    updated_on = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
Florent Chehab's avatar
Florent Chehab committed
33
34
35

    new_object = serializers.JSONField(read_only=True)

36
37
    class Meta:
        model = PendingModeration
38
        fields = "__all__"
39
40
41


class PendingModerationViewSet(viewsets.ModelViewSet):
42
    permission_classes = get_viewset_permissions("PendingModerationViewSet")
43
44
    queryset = PendingModeration.objects.all()  # pylint: disable=E1101
    serializer_class = PendingModerationSerializer