myModelSerializer.py 5.16 KB
Newer Older
1
from rest_framework import serializers
Florent Chehab's avatar
Florent Chehab committed
2
from rest_framework.validators import ValidationError
3
from django.utils import timezone
4
from .pendingModeration import PendingModeration
5
from django.contrib.contenttypes.models import ContentType
Florent Chehab's avatar
Florent Chehab committed
6
from .myModel import MyModel
Florent Chehab's avatar
Florent Chehab committed
7
from .pendingModeration import PendingModerationSerializer
Florent Chehab's avatar
Florent Chehab committed
8
from backend.utils import get_user_level
9
from backend.permissions import is_moderation_required
10

Florent Chehab's avatar
Florent Chehab committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CLEANED_MY_MODEL_DATA = {
    'moderated_by': None,
    'moderated_on': None,
    'updated_by': None,
    'updated_on': None,
}


def override_data(old_data, new_data):
    for key in new_data:
        old_data[key] = new_data[key]
    return old_data


25
26
27
28
29
class MyModelSerializer(serializers.ModelSerializer):
    moderated_on = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", read_only=True)
    updated_on = serializers.DateTimeField(
        format="%Y-%m-%d %H:%M:%S", read_only=True)
30
31
    moderated_by = serializers.CharField(read_only=True)
    updated_by = serializers.CharField(read_only=True)
32
    pending_moderation = serializers.SerializerMethodField()
Florent Chehab's avatar
Florent Chehab committed
33
    model_config = serializers.SerializerMethodField()
34

Florent Chehab's avatar
Florent Chehab committed
35
    def get_model_config(self, obj=None):
Florent Chehab's avatar
Florent Chehab committed
36
        return self.Meta.model.model_config
37

38
39
40
41
42
    def get_pending_moderation(self, obj):
        if self.context['view'].action != 'list':
            return PendingModerationSerializer(obj.pending_moderation, many=True, read_only=True, context=self.context).data
        return None

43
44
45
    class Meta:
        model = MyModel

Florent Chehab's avatar
Florent Chehab committed
46
47
48
49
    def my_validate(self, attrs):
        return attrs

    def validate(self, attrs):
Florent Chehab's avatar
Florent Chehab committed
50
51
52
53
        """
        TODO unit test this
        """
        self.user = self.context['request'].user
Florent Chehab's avatar
Florent Chehab committed
54
55
56
57
        self.user_level = get_user_level(self.user)

        if "obj_moderation_level" in attrs:
            requested_obj_moder_lv = attrs["obj_moderation_level"]
58

Florent Chehab's avatar
Florent Chehab committed
59
60
61
62
63
64
            if requested_obj_moder_lv > self.user_level:
                raise ValidationError(
                    "You can't request moderation for a higher rank than you.")

        return self.my_validate(attrs)

65
66
    def set_model_attr_no_moder(self, moderated_and_updated):
        now = timezone.now()
Florent Chehab's avatar
Florent Chehab committed
67
        self.override_validated_data({
Florent Chehab's avatar
Florent Chehab committed
68
            'moderated_by': self.user,
Florent Chehab's avatar
Florent Chehab committed
69
70
            'moderated_on': now,
        })
71
72

        if moderated_and_updated:
Florent Chehab's avatar
Florent Chehab committed
73
            self.override_validated_data({
Florent Chehab's avatar
Florent Chehab committed
74
                'updated_by': self.user,
Florent Chehab's avatar
Florent Chehab committed
75
76
                'updated_on': now,
            })
77
78

    def clean_validated_data(self):
Florent Chehab's avatar
Florent Chehab committed
79
        self.override_validated_data(CLEANED_MY_MODEL_DATA)
80

Florent Chehab's avatar
Florent Chehab committed
81
82
83
84
85
86
87
88
89
90
    def override_validated_data(self, new_data):
        """
        Method used to force specific attributes when saving a model
        """
        for key in new_data:
            self.validated_data[key] = new_data[key]

    def my_pre_save(self):
        pass

Florent Chehab's avatar
Florent Chehab committed
91
92
    def save(self, *args, **kwargs):
        return self.my_save(*args, **kwargs)
93

Florent Chehab's avatar
Florent Chehab committed
94
    def my_save(self, *args, **kwargs):
95
        self.clean_validated_data()
Florent Chehab's avatar
Florent Chehab committed
96
        self.my_pre_save()
Florent Chehab's avatar
Florent Chehab committed
97
        ct = ContentType.objects.get_for_model(self.Meta.model)
98

Florent Chehab's avatar
Florent Chehab committed
99
        if is_moderation_required(self.get_model_config()['moderation_level'], self.instance, self.user, self.user_level):
100
101
            if self.instance is None:  # we need to create the main model
                self.instance = super(
Florent Chehab's avatar
Florent Chehab committed
102
                    MyModelSerializer, self).save(*args, **kwargs)
103

Florent Chehab's avatar
Florent Chehab committed
104
105
106
            data_to_save = dict()
            for key in self.validated_data:
                try:
Florent Chehab's avatar
Florent Chehab committed
107
108
                    # retrieve the submitted data and save the clean json
                    # to make sure it will be savable
Florent Chehab's avatar
Florent Chehab committed
109
110
111
112
113
                    data_to_save[key] = self.initial_data[key]
                except KeyError:
                    pass

            data_to_save = override_data(data_to_save, CLEANED_MY_MODEL_DATA)
Florent Chehab's avatar
Florent Chehab committed
114
115
116
117
118
119
120
121
122

            PendingModeration.objects.update_or_create(
                content_type=ct,
                object_id=self.instance.pk,
                defaults={
                    'updated_on': timezone.now(),
                    'updated_by': self.user,
                    'new_object': data_to_save
                })
123
124
125
            return self.instance

        else:
126

127
            moderated_and_updated = True
128
            if self.instance is None:
129
                self.set_model_attr_no_moder(moderated_and_updated)
Florent Chehab's avatar
Florent Chehab committed
130
                return super(MyModelSerializer, self).save(*args, **kwargs)
131
            else:
Florent Chehab's avatar
Florent Chehab committed
132
133
134
135
136
                try:
                    pending_instance = PendingModeration.objects.get(
                        content_type=ct,
                        object_id=self.instance.pk,
                    )
137
                    self.clean_validated_data()  # Make that it is done...
Florent Chehab's avatar
Florent Chehab committed
138
                    if pending_instance.new_object == self.validated_data:
139
                        moderated_and_updated = False
Florent Chehab's avatar
Florent Chehab committed
140
141
142
                        self.validated_data['updated_by'] = pending_instance.updated_by
                        self.validated_data['updated_on'] = pending_instance.updated_on
                    pending_instance.delete()
143

Florent Chehab's avatar
Florent Chehab committed
144
145
                except PendingModeration.DoesNotExist:
                    pass
146

147
                self.set_model_attr_no_moder(moderated_and_updated)
Florent Chehab's avatar
Florent Chehab committed
148
                return super(MyModelSerializer, self).save(*args, **kwargs)