pendingModeration.py 1.6 KB
Newer Older
1
from django.db import models
2
from rest_framework import serializers, viewsets
3
4
5
6
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.postgres.fields import JSONField
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
9
10


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

13
14
15
16
    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')

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

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

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


class PendingModerationSerializer(serializers.ModelSerializer):
Florent Chehab's avatar
Florent Chehab committed
27
28
29
30
31
32
33
34
35
    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)

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


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