attribute_kinds: add a birthdate field kind (fixes #21691)

Similar to date, it just checks that the given date is in the past.
This commit is contained in:
Benjamin Dauvergne 2018-02-07 16:25:04 +01:00
parent 38bb35c856
commit 61e9b064f4
2 changed files with 31 additions and 0 deletions

View File

@ -25,6 +25,23 @@ DEFAULT_TITLE_CHOICES = (
)
class BirthdateWidget(widgets.DateWidget):
def __init__(self, *args, **kwargs):
options = kwargs.setdefault('options', {})
options['endDate'] = '-1d'
super(BirthdateWidget, self).__init__(*args, **kwargs)
class BirthdateField(forms.DateField):
widget = BirthdateWidget
def clean(self, value):
value = super(BirthdateField, self).clean(value)
if value and value >= datetime.date.today():
raise ValidationError(_('birthdate must be in the past.'))
return value
@to_iter
def get_title_choices():
return app_settings.A2_ATTRIBUTE_KIND_TITLE_CHOICES or DEFAULT_TITLE_CHOICES
@ -110,6 +127,14 @@ DEFAULT_ATTRIBUTE_KINDS = [
'deserialize': lambda x: x and datetime.datetime.strptime(x, '%Y-%m-%d').date(),
'rest_framework_field_class': serializers.DateField,
},
{
'label': _('birthdate'),
'name': 'birthdate',
'field_class': BirthdateField,
'serialize': lambda x: x.isoformat(),
'deserialize': lambda x: x and datetime.datetime.strptime(x, '%Y-%m-%d').date(),
'rest_framework_field_class': serializers.DateField,
},
{
'label': _('french postcode'),
'name': 'fr_postcode',

View File

@ -199,6 +199,12 @@ msgstr "booléen"
msgid "date"
msgstr "date"
msgid "birthdate must be in the past."
msgstr "Une date de naissance doit être dans le passé."
msgid "birthdate"
msgstr "date de naissance"
#: src/authentic2/attribute_kinds.py:114
msgid "french postcode"
msgstr "code postal"