essentialModule.py 13.2 KB
Newer Older
1
from django.contrib.contenttypes.fields import GenericRelation
Florent Chehab's avatar
Florent Chehab committed
2
from django.contrib.contenttypes.models import ContentType
3 4 5
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
6
from django.utils import timezone
7
from rest_framework import serializers
Florent Chehab's avatar
Florent Chehab committed
8

9
from backend_app.models.pendingModeration import PendingModeration
10 11
from backend_app.permissions.moderation import is_moderation_required
from backend_app.permissions.utils import Request as FakeRequest
12
from backend_app.settings.defaults import (
13 14 15
    DEFAULT_OBJ_MODERATION_LV,
    OBJ_MODERATION_PERMISSIONS,
)
16 17
from backend_app.utils import get_user_level
from base_app.models import User
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
from .base import BaseModel, BaseModelSerializer, BaseModelViewSet

POSSIBLE_OBJ_MODER_LV = [
    OBJ_MODERATION_PERMISSIONS[key] for key in OBJ_MODERATION_PERMISSIONS
]


def validate_obj_model_lv(value):
    if value not in POSSIBLE_OBJ_MODER_LV:
        raise ValidationError("obj_moderation_level not recognized")


#
#
#
#
#
#
#      Module
#
#
#
#
#
#
#
#


class EssentialModule(BaseModel):
    """
49
        All models in the app depend of this one.
50
        It contains the required attributes for managing optional data moderation.
Florent Chehab's avatar
Florent Chehab committed
51

52 53
        All the logic behind moderation is done in EssentialModuleSerializer
    """
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    # store the update author
    updated_by = models.ForeignKey(
        User, null=True, on_delete=models.SET_NULL, related_name="+"
    )
    # store the update date (model can be updated without moderation)
    updated_on = models.DateTimeField(null=True)

    # store the moderator
    moderated_by = models.ForeignKey(
        User, null=True, on_delete=models.SET_NULL, related_name="+"
    )
    # store the moderation date
    moderated_on = models.DateTimeField(null=True)

    # Store the object moderation level by default
    obj_moderation_level = models.SmallIntegerField(
        default=DEFAULT_OBJ_MODERATION_LV,
        validators=[MinValueValidator(0), validate_obj_model_lv],
    )
    # Add the link to pending moderation
    pending_moderation = GenericRelation(PendingModeration)

    # A bit of optimization: we store if there is something pending moderation
    has_pending_moderation = models.BooleanField(default=False)

    class Meta:
        abstract = True


#
#
#
#
#
#
#      Serializer
#
#
#
#
#
#
#
#


CLEANED_ESSENTIAL_MODULE_MODEL_DATA = {
102 103 104 105
    "moderated_by": None,
    "moderated_on": None,
    "updated_by": None,
    "updated_on": None,
Florent Chehab's avatar
Florent Chehab committed
106 107 108
}


Florent Chehab's avatar
Florent Chehab committed
109 110 111 112
def override_data(old_data: dict, new_data: dict) -> dict:
    """Update the data in old_data with the one in new_data
    """

Florent Chehab's avatar
Florent Chehab committed
113
    for key in new_data:
Florent Chehab's avatar
Florent Chehab committed
114 115
        if key in old_data:
            old_data[key] = new_data[key]
Florent Chehab's avatar
Florent Chehab committed
116 117 118
    return old_data


119 120
class EssentialModuleSerializer(BaseModelSerializer):
    """Serializer to go along the EssentialModule Model. This serializer handles backend data moderation checks and tricks.
Florent Chehab's avatar
Florent Chehab committed
121 122 123 124 125

    Raises:
        ValidationError -- If you are trying to moderate something you don't have rights to
    """

126 127
    check_obj_permissions_for_edit = False

Florent Chehab's avatar
Florent Chehab committed
128 129
    ######
    # Basic fields serializers
130
    updated_by = serializers.SerializerMethodField()
131
    updated_on = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)
Florent Chehab's avatar
Florent Chehab committed
132

133
    moderated_by = serializers.SerializerMethodField()
Florent Chehab's avatar
Florent Chehab committed
134 135
    moderated_on = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", read_only=True)

136
    has_pending_moderation = serializers.BooleanField(read_only=True)
137

138 139 140
    # Add a content_type_id field to be able to find versions
    content_type_id = serializers.SerializerMethodField()

141 142 143 144 145 146
    def get_updated_by(self, obj):
        return self.get_user_related_field(obj.updated_by)

    def get_moderated_by(self, obj):
        return self.get_user_related_field(obj.moderated_by)

147 148 149 150 151 152
    def get_content_type_id(self, obj):
        """
        Serializer for content type
        """
        return ContentType.objects.get_for_model(self.Meta.model).id

153
    def get_obj_info(self, obj) -> dict:
Florent Chehab's avatar
Florent Chehab committed
154
        """
155
        Serializer for the `obj_info` *dynamic* field. Redefined.
Florent Chehab's avatar
Florent Chehab committed
156
        """
157
        obj_info = super().get_obj_info(obj)
158 159 160
        try:
            user_can_edit = self.context["user_can_edit"]
        except KeyError:
161
            # In case some viewsets don't inherit from BaseModelViewSet and therefore
162 163 164 165
            # don't have the method to produce context["user_can_edit"]
            # Anyway, those Viewsets should be readonly, so we can return false.
            user_can_edit = False

166 167 168 169 170 171 172 173 174 175 176 177
        if user_can_edit and self.check_obj_permissions_for_edit:
            try:
                fake_edit_request = FakeRequest(self.get_user_from_request(), "PUT")
                for permission_class in self.context["permission_classes"]:
                    if not permission_class.has_object_permission(
                        fake_edit_request, None, obj
                    ):
                        user_can_edit = False
                        break
            except KeyError:
                pass

178
        obj_info["user_can_edit"] = user_can_edit
179
        obj_info["user_can_moderate"] = user_can_edit and not is_moderation_required(
180 181 182
            self.Meta.model, obj, self.get_user_from_request()
        )
        return obj_info
183 184

    class Meta:
185
        model = EssentialModule
186 187 188 189 190 191 192 193
        fields = BaseModelSerializer.Meta.fields + (
            "updated_by",
            "updated_on",
            "moderated_by",
            "moderated_on",
            "has_pending_moderation",
            "content_type_id",
        )
194

Florent Chehab's avatar
Florent Chehab committed
195
    def validate(self, attrs):
Florent Chehab's avatar
Florent Chehab committed
196
        """
197
        Validate `BaseModel` fields and enforce certain field at the backend level.
Florent Chehab's avatar
Florent Chehab committed
198

Florent Chehab's avatar
Florent Chehab committed
199
        Checks that the requested moderation level is not higher than the one of the user.
Florent Chehab's avatar
Florent Chehab committed
200
        """
Florent Chehab's avatar
Florent Chehab committed
201 202
        if "obj_moderation_level" in attrs:
            requested_obj_moder_lv = attrs["obj_moderation_level"]
203

204
            try:
205
                user_level = get_user_level(self.get_user_from_request())
206 207 208 209 210 211 212
            except KeyError:
                # if for some reason we don't have the user in the request
                # we set the level to the default one
                # this can occur during testing.
                user_level = DEFAULT_OBJ_MODERATION_LV

            if requested_obj_moder_lv > user_level:
Florent Chehab's avatar
Florent Chehab committed
213
                raise ValidationError(
214 215
                    "You can't request moderation for a higher rank than you."
                )
Florent Chehab's avatar
Florent Chehab committed
216

217
        return attrs
Florent Chehab's avatar
Florent Chehab committed
218

Florent Chehab's avatar
Florent Chehab committed
219 220 221
    def set_model_attrs_for_moderation_and_update(
        self, user, moderated_and_updated: bool
    ):
Florent Chehab's avatar
Florent Chehab committed
222
        """
Florent Chehab's avatar
Florent Chehab committed
223 224 225 226
        Overrides model attributes regarding moderation and update.
        The moderated field is set to the request user. The moderated_on field is reset to now.

        If there was an updated, the updated_by field and updated_on field are also reset.
Florent Chehab's avatar
Florent Chehab committed
227
        """
228
        now = timezone.now()
229
        self.override_validated_data({"moderated_by": user, "moderated_on": now})
230 231

        if moderated_and_updated:
232
            self.override_validated_data({"updated_by": user, "updated_on": now})
233 234

    def clean_validated_data(self):
Florent Chehab's avatar
Florent Chehab committed
235 236 237
        """
        Clear fields related to update and moderation
        """
238
        self.override_validated_data(CLEANED_ESSENTIAL_MODULE_MODEL_DATA)
239

Florent Chehab's avatar
Florent Chehab committed
240
    def override_validated_data(self, new_data: dict):
Florent Chehab's avatar
Florent Chehab committed
241 242 243 244 245 246
        """
        Method used to force specific attributes when saving a model
        """
        for key in new_data:
            self.validated_data[key] = new_data[key]

247
    def do_before_save(self):
Florent Chehab's avatar
Florent Chehab committed
248
        """
249
        Action to perform before saving a model
Florent Chehab's avatar
Florent Chehab committed
250
        """
Florent Chehab's avatar
Florent Chehab committed
251 252
        pass

Florent Chehab's avatar
Florent Chehab committed
253
    def save(self, *args, **kwargs):
Florent Chehab's avatar
Florent Chehab committed
254 255
        """
        Function that handles all the moderation in a smart way.
256 257
        Nothing has to be done to tell that we won't the data to be moderated,
        it is detected automatically.
Florent Chehab's avatar
Florent Chehab committed
258
        """
259 260 261 262

        user = self.context["request"].user
        user_level = get_user_level(user)

263
        self.clean_validated_data()
264
        self.do_before_save()
Florent Chehab's avatar
Florent Chehab committed
265
        ct = ContentType.objects.get_for_model(self.Meta.model)
266

267
        if is_moderation_required(self.Meta.model, self.instance, user, user_level):
268
            if self.instance is None:  # we need to create the main model
Florent Chehab's avatar
Florent Chehab committed
269
                # Store the user for squashing data in versions models
270
                self.validated_data.updated_by = user
Florent Chehab's avatar
Florent Chehab committed
271
                self.instance = super().save(*args, **kwargs)
272

Florent Chehab's avatar
Florent Chehab committed
273 274 275
            data_to_save = dict()
            for key in self.validated_data:
                try:
Florent Chehab's avatar
Florent Chehab committed
276 277
                    # retrieve the submitted data and save the clean json
                    # to make sure it will be savable
Florent Chehab's avatar
Florent Chehab committed
278 279 280 281
                    data_to_save[key] = self.initial_data[key]
                except KeyError:
                    pass

282 283 284
            data_to_save = override_data(
                data_to_save, CLEANED_ESSENTIAL_MODULE_MODEL_DATA
            )
Florent Chehab's avatar
Florent Chehab committed
285

Florent Chehab's avatar
Florent Chehab committed
286
            # Save instance into pending moderation state
Florent Chehab's avatar
Florent Chehab committed
287 288 289 290
            PendingModeration.objects.update_or_create(
                content_type=ct,
                object_id=self.instance.pk,
                defaults={
291
                    "updated_on": timezone.now(),
292
                    "updated_by": user,
293 294 295
                    "new_object": data_to_save,
                },
            )
296 297 298 299

            # Performance optimization, we store the fact that there is an object pending moderation
            self.instance.has_pending_moderation = True
            self.instance.save()
300 301
            return self.instance

Florent Chehab's avatar
Florent Chehab committed
302
        else:  # Moderation is not needed, we need to check whether it's a moderation or an update with no moderation
303

304
            moderated_and_updated = True
305
            if self.instance is None:
Florent Chehab's avatar
Florent Chehab committed
306 307 308
                self.set_model_attrs_for_moderation_and_update(
                    user, moderated_and_updated
                )
309
                instance = super().save(*args, **kwargs)
310
            else:
Florent Chehab's avatar
Florent Chehab committed
311 312
                try:
                    pending_instance = PendingModeration.objects.get(
313
                        content_type=ct, object_id=self.instance.pk
Florent Chehab's avatar
Florent Chehab committed
314
                    )
Florent Chehab's avatar
Florent Chehab committed
315
                    self.clean_validated_data()  # Make quez that it is done...
Florent Chehab's avatar
Florent Chehab committed
316 317 318 319 320 321 322 323
                    # We have to compare the serialized data
                    # So we make sure to compare the same elements
                    key_to_remove = []
                    for key in self.initial_data:
                        if key not in self.validated_data:
                            key_to_remove.append(key)
                    for key in key_to_remove:
                        self.initial_data.pop(key, None)
Florent Chehab's avatar
Florent Chehab committed
324

Florent Chehab's avatar
Florent Chehab committed
325
                    if pending_instance.new_object == self.initial_data:
326
                        moderated_and_updated = False
327 328
                        self.validated_data["updated_by"] = pending_instance.updated_by
                        self.validated_data["updated_on"] = pending_instance.updated_on
Florent Chehab's avatar
Florent Chehab committed
329
                    pending_instance.delete()
330

Florent Chehab's avatar
Florent Chehab committed
331 332
                except PendingModeration.DoesNotExist:
                    pass
333

Florent Chehab's avatar
Florent Chehab committed
334 335 336
                self.set_model_attrs_for_moderation_and_update(
                    user, moderated_and_updated
                )
337 338 339 340 341 342
                instance = super().save(*args, **kwargs)

            # Performance optimization to know if has pending moderation
            instance.has_pending_moderation = False
            instance.save()
            return instance
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377


#
#
#
#
#
#
#      ViewSet
#
#
#
#
#
#
#
#


class EssentialModuleViewSet(BaseModelViewSet):
    """
    Custom default viewset
    """

    serializer_class = EssentialModuleSerializer

    def get_serializer_context(self):
        """
        Override default function.
        Extra context is provided to the serializer class to
        know if a user can edit an element or not.

        This allows to not do this query for all elements and improves
        performances. You can look at the comment below for more information.
        """
378 379 380 381 382 383

        # When generating the API documentation (url: /api-doc) the request would be None
        # and we don't need to do anything special
        if self.request is None:
            return super().get_serializer_context()

384 385 386 387 388 389 390 391 392 393
        fake_edit_request = FakeRequest(self.request.user, "PUT")

        user_can_edit = True
        for permission_class in self.get_permissions():
            # Theoretically speaking we would need to use has_object_permission
            # But for performance purpose, we will consider edition right at the model
            # level. Which is consistent with our design.
            # Beware, that this might provide inconsistent data to the frontend
            # especially if permission_classes impact at the object level such as
            # IsOwner.
394 395 396
            #
            # Set check_obj_permissions_for_edit=True in your serializer
            # if you want a better check at the object level
397 398 399 400 401 402
            if not permission_class.has_permission(fake_edit_request, None):
                user_can_edit = False
                break

        default_context = super().get_serializer_context()
        default_context["user_can_edit"] = user_can_edit
403
        default_context["permission_classes"] = self.get_permissions()
404 405 406 407 408 409 410 411
        return default_context

    def get_queryset(self):
        """
        Extended default rest framework behavior
        to prefetch some table and enhance performances
        """
        return self.queryset.prefetch_related("moderated_by", "updated_by")