Commit 35b8ea0f authored by Florent Chehab's avatar Florent Chehab

New moderation modulation added

parent 4ef12c45
Pipeline #27353 passed with stages
in 2 minutes and 29 seconds
...@@ -111,7 +111,7 @@ class MyModelSerializer(MySerializerWithJSON): ...@@ -111,7 +111,7 @@ class MyModelSerializer(MySerializerWithJSON):
self.my_pre_save() self.my_pre_save()
ct = ContentType.objects.get_for_model(self.Meta.model) ct = ContentType.objects.get_for_model(self.Meta.model)
if is_moderation_required(self.get_model_config()['moderation_level'], self.instance, self.user, self.user_level): if is_moderation_required(self.get_model_config(), self.instance, self.user, self.user_level):
if self.instance is None: # we need to create the main model if self.instance is None: # we need to create the main model
# Store the user for squashing data in versions models # Store the user for squashing data in versions models
self.validated_data.updated_by = self.user self.validated_data.updated_by = self.user
......
from .noDeleteIfNotStaff import NoDeleteIfNotStaff # noqa: F401
from .isOwner import IsOwner # noqa: F401
from .noDelete import NoDelete # noqa: F401
from .isStaffOrReadOnly import IsStaffOrReadOnly # noqa: F401
from .readOnly import ReadOnly # noqa: F401
from .isDriOrReadOnly import IsDriOrReadOnly # noqa: F401
from .default_viewset_permissions import DEFAULT_VIEWSET_PERMISSIONS # noqa: F401
from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS # noqa: F401 from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS # noqa: F401
from .obj_moderation_permission import DEFAULT_OBJ_MODERATION_LV # noqa: F401 from .obj_moderation_permission import DEFAULT_OBJ_MODERATION_LV # noqa: F401
from .__is_moderation_required import is_moderation_required # noqa: F401 try:
# the two imports are used outside of django
# the try catch is here to prevent the raise of error
from .noDeleteIfNotStaff import NoDeleteIfNotStaff # noqa: F401
from .isOwner import IsOwner # noqa: F401
from .noDelete import NoDelete # noqa: F401
from .isStaffOrReadOnly import IsStaffOrReadOnly # noqa: F401
from .readOnly import ReadOnly # noqa: F401
from .isDriOrReadOnly import IsDriOrReadOnly # noqa: F401
from .default_viewset_permissions import DEFAULT_VIEWSET_PERMISSIONS # noqa: F401
from .__is_moderation_required import is_moderation_required # noqa: F401
except Exception:
pass
...@@ -12,10 +12,17 @@ from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS ...@@ -12,10 +12,17 @@ from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS
###################################### ######################################
###################################### ######################################
def is_moderation_required(model_moderation_level, obj_in_db, user, user_level=None): def is_moderation_required(model_config, obj_in_db, user, user_level=None):
model_moderation_level = model_config['moderation_level']
if user_level is None: if user_level is None:
user_level = get_user_level(user) user_level = get_user_level(user)
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
if model_moderation_level == 0: if model_moderation_level == 0:
return False return False
else: else:
......
...@@ -2,15 +2,21 @@ from django.test import override_settings ...@@ -2,15 +2,21 @@ from django.test import override_settings
from .withUserTestCase import WithUserTestCase from .withUserTestCase import WithUserTestCase
from backend.permissions import is_moderation_required from backend.permissions import is_moderation_required
model_config = {'moderation_level': 2}
class IsModerationActivatedTestCase(WithUserTestCase): class IsModerationActivatedTestCase(WithUserTestCase):
@override_settings(MODERATION_ACTIVATED=True) @override_settings(MODERATION_ACTIVATED=True)
def test_is_moderation_1(self): def test_is_moderation_1(self):
self.assertFalse(is_moderation_required(2, None, self.staff_user)) self.assertFalse(is_moderation_required(
self.assertFalse(is_moderation_required(2, None, self.staff_user, 3)) model_config, None, self.staff_user))
self.assertFalse(is_moderation_required(
model_config, None, self.staff_user, 3))
@override_settings(MODERATION_ACTIVATED=False) @override_settings(MODERATION_ACTIVATED=False)
def test_is_moderation_2(self): def test_is_moderation_2(self):
self.assertFalse(is_moderation_required(2, None, self.staff_user)) self.assertFalse(is_moderation_required(
self.assertFalse(is_moderation_required(2, None, self.staff_user, 3)) model_config, None, self.staff_user))
self.assertFalse(is_moderation_required(
model_config, None, self.staff_user, 3))
...@@ -6,10 +6,14 @@ def get_model_config(model): ...@@ -6,10 +6,14 @@ def get_model_config(model):
for obj in api_config: for obj in api_config:
if obj['model'] == model: if obj['model'] == model:
return { tmp = {
"moderation_level": obj["moderation_level"], "moderation_level": obj["moderation_level"],
"model": model, "model": model,
"read_only": obj["read_only"] "read_only": obj["read_only"]
} }
key = 'enforce_moderation_user_level'
if key in obj.keys():
tmp[key] = obj[key]
return tmp
raise Exception("Model not found in API configuraiton, cannot process !") raise Exception("Model not found in API configuraiton, cannot process !")
...@@ -189,7 +189,7 @@ class GenericModule extends MyComponent { ...@@ -189,7 +189,7 @@ class GenericModule extends MyComponent {
return !isModerationRequired( return !isModerationRequired(
serverModerationStatus.activated, serverModerationStatus.activated,
serverModerationStatus.moderator_level, serverModerationStatus.moderator_level,
rawModelData.model_config.moderation_level, rawModelData.model_config,
rawModelData.obj_moderation_level, rawModelData.obj_moderation_level,
userData.owner_level userData.owner_level
) )
......
...@@ -10,9 +10,16 @@ ...@@ -10,9 +10,16 @@
export default function isModerationRequired( export default function isModerationRequired(
moderationActivated, moderationActivated,
moderatorLevel, moderatorLevel,
modelModerationLevel, modelConfig,
objModerationLevel, objModerationLevel,
userLevel) { userLevel) {
const { modelModerationLevel } = modelConfig;
const key = 'enforce_moderation_user_level';
if (key in modelConfig && userLevel < modelConfig[key]) {
return true;
}
if (modelModerationLevel == 0) { return false; } if (modelModerationLevel == 0) { return false; }
else { else {
......
...@@ -133,7 +133,7 @@ ...@@ -133,7 +133,7 @@
import_location: country import_location: country
api_end_point: countryDri api_end_point: countryDri
api_attr: (?P<country_id>[a-zA-Z]+) api_attr: (?P<country_id>[a-zA-Z]+)
viewset_permission: IsDriOrReadOnly enforce_moderation_user_level: 'DRI'
versionned: true versionned: true
...@@ -178,7 +178,7 @@ ...@@ -178,7 +178,7 @@
import_location: university import_location: university
api_end_point: universityDri api_end_point: universityDri
api_attr: (?P<univ_id>[0-9]+) api_attr: (?P<univ_id>[0-9]+)
viewset_permission: IsDriOrReadOnly enforce_moderation_user_level: 'DRI'
versionned: true versionned: true
......
import yaml import yaml
from os.path import join, realpath, dirname from os.path import join, realpath, dirname
from backend.permissions.obj_moderation_permission import OBJ_MODERATION_PERMISSIONS
def get_api_config(): def get_api_config():
...@@ -18,6 +19,9 @@ def get_api_config(): ...@@ -18,6 +19,9 @@ def get_api_config():
} }
for obj in api_config: for obj in api_config:
tmp = 'enforce_moderation_user_level'
if tmp in obj.keys():
obj[tmp] = OBJ_MODERATION_PERMISSIONS[obj[tmp]]
for key in DEFAULT_SETTINGS: for key in DEFAULT_SETTINGS:
if key not in obj: if key not in obj:
obj[key] = DEFAULT_SETTINGS[key] obj[key] = DEFAULT_SETTINGS[key]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment