Commit 60184081 authored by Florent Chehab's avatar Florent Chehab

Renamed my_pre_save to do_before_save

Also: fixed self.user not being set anymore
parent fd1835e9
......@@ -89,6 +89,12 @@ class MyModelSerializer(MySerializerWithJSON):
class Meta:
model = MyModel
def get_user_from_request(self):
"""
Function to retrieve the user from the request
"""
return self.context["request"].user
def validate(self, attrs):
"""
Validate `MyModel` fields and enforce certain field at the backend level.
......@@ -100,8 +106,7 @@ class MyModelSerializer(MySerializerWithJSON):
requested_obj_moder_lv = attrs["obj_moderation_level"]
try:
user = self.context["request"].user
user_level = get_user_level(user)
user_level = get_user_level(self.get_user_from_request())
except KeyError:
# if for some reason we don't have the user in the request
# we set the level to the default one
......@@ -139,11 +144,10 @@ class MyModelSerializer(MySerializerWithJSON):
for key in new_data:
self.validated_data[key] = new_data[key]
def my_pre_save(self):
def do_before_save(self):
"""
TODO, Analyse if usefull
Action to perform before saving a model
"""
pass
def save(self, *args, **kwargs):
......@@ -157,7 +161,7 @@ class MyModelSerializer(MySerializerWithJSON):
user_level = get_user_level(user)
self.clean_validated_data()
self.my_pre_save()
self.do_before_save()
ct = ContentType.objects.get_for_model(self.Meta.model)
if is_moderation_required(
......
......@@ -36,11 +36,18 @@ class UserDataSerializer(MyModelSerializer):
def get_owner_can_post_to(self, obj):
return list_user_post_permission(obj.owner)
def my_pre_save(self):
self.override_validated_data({"owner": self.user})
def do_before_save(self):
"""
For safety: enforce (for sure) that we update the model corresponding to the user/owner.
"""
super().do_before_save()
user = self.get_user_from_request()
self.override_validated_data({"owner": user})
# we try to recover the correct instance
query = UserData.objects.filter(owner=self.user)
query = UserData.objects.filter(owner=user)
if len(query) == 1:
self.instance = query[0]
......
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