Commit a0c89801 authored by Florent Chehab's avatar Florent Chehab

moved is_moderation_required outside of myModelSerializer

parent 714db7a2
......@@ -2,12 +2,11 @@ from rest_framework import serializers
from rest_framework.validators import ValidationError
from django.utils import timezone
from .pendingModeration import PendingModeration
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from backend.utils import does_user_have_moderation_rights
from .myModel import MyModel
from .pendingModeration import PendingModerationSerializer
from backend.utils import get_user_level
from backend.permissions import is_moderation_required
CLEANED_MY_MODEL_DATA = {
'moderated_by': None,
......@@ -60,29 +59,6 @@ class MyModelSerializer(serializers.ModelSerializer):
return self.my_validate(attrs)
def moderation_required(self, obj_in_db):
model_moderation_level = self.Meta.model.model_config['moderation_level']
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 self.user_level < obj_moderation_level:
return True
if model_moderation_level == 1:
if settings.MODERATION_ACTIVATED:
return not does_user_have_moderation_rights(self.user)
else:
return False
elif model_moderation_level == 2:
return not does_user_have_moderation_rights(self.user)
else:
raise Exception(
"No other moderation level should be defined...")
def get_user_in_request(self):
return self.context['request'].user
......@@ -126,7 +102,8 @@ class MyModelSerializer(serializers.ModelSerializer):
self.clean_validated_data()
self.my_pre_save()
if self.moderation_required(self.instance):
model_moderation_level = self.Meta.model.model_config['moderation_level']
if is_moderation_required(model_moderation_level, self.instance, self.user, self.user_level):
if self.instance is None: # we need to create the main model
self.instance = super(
MyModelSerializer, self).save(*args, **kwargs)
......
......@@ -5,3 +5,4 @@ from .isStaffOrReadOnly import IsStaffOrReadOnly # 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 .is_moderation_required import is_moderation_required # noqa: F401
from backend.utils import get_user_level
from django.conf import settings
from .obj_moderation_permission import OBJ_MODERATION_PERMISSIONS
def is_moderation_required(model_moderation_level, obj_in_db, user, user_level=None):
if user_level is None:
user_level = get_user_level(user)
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:
return user_level < OBJ_MODERATION_PERMISSIONS["moderator"]
else:
return False
elif model_moderation_level == 2:
return not user_level < OBJ_MODERATION_PERMISSIONS["moderator"]
else:
raise Exception(
"No other moderation level should be defined...")
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