0001-api-factorize-making-a-DRF-field-for-an-attribute-24.patch
src/authentic2/api_views.py | ||
---|---|---|
351 | 351 |
if at.name in self.fields: |
352 | 352 |
self.fields[at.name].required = at.required |
353 | 353 |
else: |
354 |
kind = attribute_kinds.get_kind(at.kind) |
|
355 |
field_class = kind['rest_framework_field_class'] |
|
356 |
kwargs = kind.get('rest_framework_field_kwargs') or {} |
|
357 |
kwargs.update({ |
|
358 |
'source': 'attributes.%s' % at.name, |
|
359 |
'required': at.required, |
|
360 |
}) |
|
361 |
if not at.required: |
|
362 |
# setting an attribute to null will delete it |
|
363 |
# NullBooleanField and BooleanField does not support allow_null |
|
364 |
if field_class is serializers.BooleanField: |
|
365 |
field_class = serializers.NullBooleanField |
|
366 |
elif field_class is not serializers.NullBooleanField: |
|
367 |
kwargs['allow_null'] = True |
|
368 |
# if not stated otherwise by the definition of the kind, string alike fields |
|
369 |
# accept blank values when not required |
|
370 |
if (issubclass(field_class, serializers.CharField) and 'allow_blank' not in |
|
371 |
kwargs): |
|
372 |
kwargs['allow_blank'] = True |
|
373 |
self.fields[at.name] = field_class(**kwargs) |
|
354 |
self.fields[at.name] = at.get_drf_field() |
|
374 | 355 |
self.fields[at.name + '_verified'] = serializers.BooleanField( |
375 | 356 |
source='is_verified.%s' % at.name, required=False) |
376 | 357 |
for key in self.fields: |
src/authentic2/models.py | ||
---|---|---|
176 | 176 |
kwargs['help_text'] = self.description |
177 | 177 |
return attribute_kinds.get_form_field(self.kind, **kwargs) |
178 | 178 | |
179 |
def get_drf_field(self, **kwargs): |
|
180 |
from rest_framework import serializers |
|
181 | ||
182 |
kind = self.get_kind() |
|
183 |
field_class = kind['rest_framework_field_class'] |
|
184 |
base_kwargs = kind.get('rest_framework_field_kwargs') or {} |
|
185 |
base_kwargs.update({ |
|
186 |
'source': 'attributes.%s' % self.name, |
|
187 |
'required': self.required, |
|
188 |
}) |
|
189 |
if not self.required: |
|
190 |
# setting an attribute to null will delete it |
|
191 |
# NullBooleanField and BooleanField does not support allow_null |
|
192 |
if field_class is serializers.BooleanField: |
|
193 |
field_class = serializers.NullBooleanField |
|
194 |
elif field_class is not serializers.NullBooleanField: |
|
195 |
base_kwargs['allow_null'] = True |
|
196 |
# if not stated otherwise by the definition of the kind, string alike fields |
|
197 |
# accept blank values when not required |
|
198 |
if (issubclass(field_class, serializers.CharField) and 'allow_blank' not in |
|
199 |
base_kwargs): |
|
200 |
base_kwargs['allow_blank'] = True |
|
201 |
base_kwargs.update(kwargs) |
|
202 |
return field_class(**base_kwargs) |
|
203 | ||
179 | 204 |
def get_kind(self): |
180 | 205 |
from . import attribute_kinds |
181 | 206 |
return attribute_kinds.get_kind(self.kind) |
182 |
- |