myModel.py 1.51 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
from django.db import models
from django.contrib.auth.models import User
Florent Chehab's avatar
Florent Chehab committed
3
4
from django.contrib.contenttypes.fields import GenericRelation
from .pendingModeration import PendingModeration
5
6
from shared import OBJ_MODERATION_PERMISSIONS
from shared import DEFAULT_OBJ_MODERATION_LV
Florent Chehab's avatar
Florent Chehab committed
7
8
9
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError

Florent Chehab's avatar
Florent Chehab committed
10
oml = DEFAULT_OBJ_MODERATION_LV
Florent Chehab's avatar
Florent Chehab committed
11
12
13
14
15
16
17
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')
Florent Chehab's avatar
Florent Chehab committed
18
19
20
21
22
23
24
25
26
27
28


class MyModel(models.Model):
    """
        All models in the app deppend of this one.
        It contains the required attributes for managing eventual moderation data.

        All the logic behind moderation is done in myModelSerializer
    """

    moderated_by = models.ForeignKey(
29
        User, null=True, on_delete=models.SET_NULL, related_name='+')
Florent Chehab's avatar
Florent Chehab committed
30
31
32
33
    moderated_on = models.DateTimeField(null=True)

    updated_on = models.DateTimeField(null=True)
    updated_by = models.ForeignKey(
34
        User, null=True, on_delete=models.SET_NULL, related_name='+')
Florent Chehab's avatar
Florent Chehab committed
35
36
    obj_moderation_level = models.SmallIntegerField(
        default=oml, validators=[MinValueValidator(0), validate_obj_model_lv])
Florent Chehab's avatar
Florent Chehab committed
37
    pending_moderation = GenericRelation(PendingModeration)
Florent Chehab's avatar
Florent Chehab committed
38
39
40

    class Meta:
        abstract = True
41

42
43
44
    model_config = {
        "moderation_level": None,
    }