From 2169ea83cbd8394759e97874eb3805720d9d2efc Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 7 Aug 2019 10:41:00 +0200 Subject: [PATCH 1/2] api: factorize making a DRF field for an attribute (#24401) --- src/authentic2/api_views.py | 21 +-------------------- src/authentic2/models.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/authentic2/api_views.py b/src/authentic2/api_views.py index 913fa71a..88f6618d 100644 --- a/src/authentic2/api_views.py +++ b/src/authentic2/api_views.py @@ -351,26 +351,7 @@ class BaseUserSerializer(api_mixins.GetOrCreateModelSerializer, if at.name in self.fields: self.fields[at.name].required = at.required else: - kind = attribute_kinds.get_kind(at.kind) - field_class = kind['rest_framework_field_class'] - kwargs = kind.get('rest_framework_field_kwargs') or {} - kwargs.update({ - 'source': 'attributes.%s' % at.name, - 'required': at.required, - }) - if not at.required: - # setting an attribute to null will delete it - # NullBooleanField and BooleanField does not support allow_null - if field_class is serializers.BooleanField: - field_class = serializers.NullBooleanField - elif field_class is not serializers.NullBooleanField: - kwargs['allow_null'] = True - # if not stated otherwise by the definition of the kind, string alike fields - # accept blank values when not required - if (issubclass(field_class, serializers.CharField) and 'allow_blank' not in - kwargs): - kwargs['allow_blank'] = True - self.fields[at.name] = field_class(**kwargs) + self.fields[at.name] = at.get_drf_field() self.fields[at.name + '_verified'] = serializers.BooleanField( source='is_verified.%s' % at.name, required=False) for key in self.fields: diff --git a/src/authentic2/models.py b/src/authentic2/models.py index a16be648..3019f79a 100644 --- a/src/authentic2/models.py +++ b/src/authentic2/models.py @@ -176,6 +176,31 @@ class Attribute(models.Model): kwargs['help_text'] = self.description return attribute_kinds.get_form_field(self.kind, **kwargs) + def get_drf_field(self, **kwargs): + from rest_framework import serializers + + kind = self.get_kind() + field_class = kind['rest_framework_field_class'] + base_kwargs = kind.get('rest_framework_field_kwargs') or {} + base_kwargs.update({ + 'source': 'attributes.%s' % self.name, + 'required': self.required, + }) + if not self.required: + # setting an attribute to null will delete it + # NullBooleanField and BooleanField does not support allow_null + if field_class is serializers.BooleanField: + field_class = serializers.NullBooleanField + elif field_class is not serializers.NullBooleanField: + base_kwargs['allow_null'] = True + # if not stated otherwise by the definition of the kind, string alike fields + # accept blank values when not required + if (issubclass(field_class, serializers.CharField) and 'allow_blank' not in + base_kwargs): + base_kwargs['allow_blank'] = True + base_kwargs.update(kwargs) + return field_class(**base_kwargs) + def get_kind(self): from . import attribute_kinds return attribute_kinds.get_kind(self.kind) -- 2.22.0