__is_moderation_required.py 1.47 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from backend_app.utils.__get_user_level import get_user_level
2
from django.conf import settings
3
from shared import OBJ_MODERATION_PERMISSIONS
4
5


6
7
8
9
10
11
12
13
14
######################################
######################################
##
# IF YOU TOUCH THIS FILE, MODIFY IT'S
# JS EQUIVALENT (isModerationRequired)
##
######################################
######################################

15

16
def is_moderation_required(model_config, obj_in_db, user, user_level=None):
17
    model_moderation_level = model_config["moderation_level"]
18

19
20
21
    if user_level is None:
        user_level = get_user_level(user)

22
    key = "enforce_moderation_user_level"
23
24
25
26
    if key in model_config.keys() and model_config[key] is not None:
        if user_level < model_config[key]:
            return True

27
28
29
30
31
32
33
34
35
36
37
    if model_moderation_level == 0:
        return False
    else:
        # At this point we have to check the obj_moderation_level
        if obj_in_db is not None:
            obj_moderation_level = obj_in_db.obj_moderation_level
            if user_level < obj_moderation_level:
                return True

        if model_moderation_level == 1:
            if settings.MODERATION_ACTIVATED:
38
                return not user_level >= OBJ_MODERATION_PERMISSIONS["moderator"]
39
40
41
            else:
                return False
        elif model_moderation_level == 2:
42
            return not user_level >= OBJ_MODERATION_PERMISSIONS["moderator"]
43
        else:
44
            raise Exception("No other moderation level should be defined...")