__is_moderation_required.py 1.5 KB
Newer Older
1
from backend.utils.__get_user_level import get_user_level
2 3 4 5
from django.conf import settings
from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS


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

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

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

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

26 27 28 29 30 31 32 33 34 35 36
    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:
37
                return not user_level >= OBJ_MODERATION_PERMISSIONS["moderator"]
38 39 40
            else:
                return False
        elif model_moderation_level == 2:
41
            return not user_level >= OBJ_MODERATION_PERMISSIONS["moderator"]
42 43 44
        else:
            raise Exception(
                "No other moderation level should be defined...")