Skip to content
Snippets Groups Projects
Verified Commit f32afb90 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Validate date of birth is in the past.

Thanks to Magdalena Grunert for spotting!
parent 5741eeb6
Branches
Tags
No related merge requests found
from datetime import date
import re
from dateutil.relativedelta import relativedelta
from django import forms
from django.core import validators
from django.utils.translation import ugettext_lazy as _
......@@ -27,6 +29,17 @@ def is_username_free(uid):
if TeckidsPerson.objects.filter(uid=uid):
raise forms.ValidationError(_('Der Benutzername ist bereits vergeben!'))
def is_date_min_years_ago(min_years):
# Generate a validator for the desired amount of years
def _the_validator(dt):
delta = date.today() - dt
years = int(delta.days / 365.2425)
if self.date_of_birth > today - relativedelta(years=years):
years -= 1
if years < min_years:
raise forms.ValidationError(_('Du musst mindestens %d Jahre alt sein! Hast du vielleicht dein Geburtsdatum falsch angegeben?') % min_years)
return _the_validator
class LoginForm(forms.Form):
uid = forms.CharField(label=_('Benutzername'), max_length=20)
password = forms.CharField(label=_('Passwort'), widget=forms.widgets.PasswordInput)
......@@ -35,7 +48,9 @@ class RegisterForm(forms.Form):
# Name and identity fields
given_name = forms.CharField(label=_('Vorname'), max_length=100)
sn = forms.CharField(label=_('Nachname'), max_length=100)
date_of_birth = forms.DateField(label=_('Geburtsdatum'), widget=forms.DateInput(attrs={'type': 'date'}))
date_of_birth = forms.DateField(label=_('Geburtsdatum'),
widget=forms.DateInput(attrs={'type': 'date'}),
validators=[is_date_min_years_ago(7)])
# Contact fields
mail = forms.EmailField(label=_('E-Mail-Adresse'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment