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
5
from backend.fields import JSONField
6
from django.contrib.auth.models import User
Florent Chehab's avatar
Florent Chehab committed
7
from backend.utils import get_viewset_permissions, get_model_config
8
from backend.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 16 17
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.CharField(max_length=100)  # 100 should be ok
    referenced_object = GenericForeignKey('content_type', 'object_id')

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 24 25 26

    class Meta:
        unique_together = ('content_type', 'object_id')


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

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

    new_object = serializers.JSONField(read_only=True)

37 38 39 40 41 42
    class Meta:
        model = PendingModeration
        fields = '__all__'


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