Commit 108fa917 authored by Florent Chehab's avatar Florent Chehab

Cleaning mostly

parent a0c89801
Pipeline #26854 passed with stages
in 2 minutes and 27 seconds
......@@ -3,10 +3,11 @@ from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericRelation
from .pendingModeration import PendingModeration
from backend.permissions import OBJ_MODERATION_PERMISSIONS
from backend.permissions import DEFAULT_OBJ_MODERATION_LV
from django.core.validators import MinValueValidator
from django.core.exceptions import ValidationError
oml = OBJ_MODERATION_PERMISSIONS["authenticated_user"]
oml = DEFAULT_OBJ_MODERATION_LV
POSSIBLE_OBJ_MODER_LV = [OBJ_MODERATION_PERMISSIONS[key]
for key in OBJ_MODERATION_PERMISSIONS]
......
......@@ -32,7 +32,7 @@ class MyModelSerializer(serializers.ModelSerializer):
pending_moderation = serializers.SerializerMethodField()
model_config = serializers.SerializerMethodField()
def get_model_config(self, obj):
def get_model_config(self, obj=None):
return self.Meta.model.model_config
def get_pending_moderation(self, obj):
......@@ -47,7 +47,10 @@ class MyModelSerializer(serializers.ModelSerializer):
return attrs
def validate(self, attrs):
self.user = self.get_user_in_request()
"""
TODO unit test this
"""
self.user = self.context['request'].user
self.user_level = get_user_level(self.user)
if "obj_moderation_level" in attrs:
......@@ -59,31 +62,22 @@ class MyModelSerializer(serializers.ModelSerializer):
return self.my_validate(attrs)
def get_user_in_request(self):
return self.context['request'].user
def set_model_attr_no_moder(self, moderated_and_updated):
user = self.get_user_in_request()
now = timezone.now()
self.override_validated_data({
'moderated_by': user,
'moderated_by': self.user,
'moderated_on': now,
})
if moderated_and_updated:
self.override_validated_data({
'updated_by': user,
'updated_by': self.user,
'updated_on': now,
})
def clean_validated_data(self):
self.override_validated_data(CLEANED_MY_MODEL_DATA)
def get_pending_models(self):
ct = ContentType.objects.get_for_model(self.Meta.model)
return PendingModeration.objects.filter(
content_type=ct, object_id=self.instance.pk)
def override_validated_data(self, new_data):
"""
Method used to force specific attributes when saving a model
......@@ -98,42 +92,34 @@ class MyModelSerializer(serializers.ModelSerializer):
return self.my_save(*args, **kwargs)
def my_save(self, *args, **kwargs):
user = self.get_user_in_request()
self.clean_validated_data()
self.my_pre_save()
ct = ContentType.objects.get_for_model(self.Meta.model)
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 is_moderation_required(self.get_model_config()['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)
objs_pending_db = self.get_pending_models()
data_to_save = dict()
for key in self.validated_data:
try:
# retreive the submitted data
# retrieve the submitted data and save the clean json
# to make sure it will be savable
data_to_save[key] = self.initial_data[key]
except KeyError:
pass
data_to_save = override_data(data_to_save, CLEANED_MY_MODEL_DATA)
if len(objs_pending_db) > 0:
pending_instance = objs_pending_db[0]
pending_instance.updated_on = timezone.now()
pending_instance.updated_by = user
pending_instance.new_object = data_to_save
else:
# there is no pending instance, we have to create one
pending_instance = PendingModeration(
referenced_object=self.instance,
updated_by=user,
updated_on=timezone.now(),
new_object=data_to_save
)
pending_instance.save()
PendingModeration.objects.update_or_create(
content_type=ct,
object_id=self.instance.pk,
defaults={
'updated_on': timezone.now(),
'updated_by': self.user,
'new_object': data_to_save
})
return self.instance
else:
......@@ -143,17 +129,20 @@ class MyModelSerializer(serializers.ModelSerializer):
self.set_model_attr_no_moder(moderated_and_updated)
return super(MyModelSerializer, self).save(*args, **kwargs)
else:
# We check if we don't have any moderation pending
objs_pending_db = self.get_pending_models()
if len(objs_pending_db) > 0:
obj_pending_db = objs_pending_db[0]
try:
pending_instance = PendingModeration.objects.get(
content_type=ct,
object_id=self.instance.pk,
)
self.clean_validated_data() # Make that it is done...
if obj_pending_db.new_object == self.validated_data:
if pending_instance.new_object == self.validated_data:
moderated_and_updated = False
self.validated_data['updated_by'] = obj_pending_db.updated_by
self.validated_data['updated_on'] = obj_pending_db.updated_on
self.validated_data['updated_by'] = pending_instance.updated_by
self.validated_data['updated_on'] = pending_instance.updated_on
pending_instance.delete()
objs_pending_db.delete()
except PendingModeration.DoesNotExist:
pass
self.set_model_attr_no_moder(moderated_and_updated)
return super(MyModelSerializer, self).save(*args, **kwargs)
......@@ -5,4 +5,5 @@ 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 .obj_moderation_permission import DEFAULT_OBJ_MODERATION_LV # noqa: F401
from .is_moderation_required import is_moderation_required # noqa: F401
......@@ -4,3 +4,5 @@ OBJ_MODERATION_PERMISSIONS = {
"moderator": 1,
"authenticated_user": 0
}
DEFAULT_OBJ_MODERATION_LV = OBJ_MODERATION_PERMISSIONS["authenticated_user"]
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