myModel.py 1.5 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
Florent Chehab's avatar
Florent Chehab committed
5
6
7
8
9
10
11
12
13
14
15
16
from backend.permissions import OBJ_MODERATION_PERMISSIONS
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError

oml = OBJ_MODERATION_PERMISSIONS["authenticated_user"]
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
17
18
19
20
21
22
23
24
25
26
27


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(
28
        User, null=True, on_delete=models.SET_NULL, related_name='+')
Florent Chehab's avatar
Florent Chehab committed
29
30
31
32
    moderated_on = models.DateTimeField(null=True)

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

    class Meta:
        abstract = True
40

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