From d525f2d7d26c9920a3313b5a80d1c632b1e5b270 Mon Sep 17 00:00:00 2001 From: Jochen Saalfeld Date: Wed, 14 Feb 2018 12:18:48 +0100 Subject: [PATCH] patch userdetails without unnecessary double verification --- openslides/users/serializers.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/openslides/users/serializers.py b/openslides/users/serializers.py index 708e0dbd2..65fea361b 100644 --- a/openslides/users/serializers.py +++ b/openslides/users/serializers.py @@ -57,19 +57,23 @@ class UserFullSerializer(ModelSerializer): def validate(self, data): """ - Checks that first_name or last_name is given. Generates the + Checks if the given data is empty. Generates the username if it is empty. """ - if not (data.get('username') or data.get('first_name') or data.get('last_name')): - raise ValidationError({'detail': _('Username, given name and surname can not all be empty.')}) - # Generate username. But only if it is not set and the serializer is not - # called in a PATCH context (partial_update). try: action = self.context['view'].action except (KeyError, AttributeError): action = None + # Check if we are in Patch context, if not, check if we have the mandatory fields + if action != 'partial_update': + if not (data.get('username') or data.get('first_name') or data.get('last_name')): + raise ValidationError({'detail': _('Username, given name and surname can not all be empty.')}) + + # Generate username. But only if it is not set and the serializer is not + # called in a PATCH context (partial_update). + if not data.get('username') and action != 'partial_update': data['username'] = User.objects.generate_username( data.get('first_name', ''),