diff options
author | Alban Gruin | 2018-04-23 16:20:38 +0200 |
---|---|---|
committer | Alban Gruin | 2018-04-23 16:20:38 +0200 |
commit | 63ceb7ff232cd4015ae148bec31c6d72d9239c19 (patch) | |
tree | 9aee056480b5fae0c97f5e1c8be4ab1358602926 /forms.py | |
parent | af055dbf703af0eca03862194de1a0f34ac1fcc5 (diff) | |
parent | 9853d375d9e482c23f5fdbed9a3cf041029a92cb (diff) |
Merge branch 'qsjps' into futur
Diffstat (limited to 'forms.py')
-rw-r--r-- | forms.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..00dbf9e --- /dev/null +++ b/forms.py @@ -0,0 +1,58 @@ +# Copyright (C) 2018 Alban Gruin +# +# celcatsanitizer is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# celcatsanitizer is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with celcatsanitizer. If not, see <http://www.gnu.org/licenses/>. + +from datetime import timedelta + +from django import forms +from django.forms.widgets import DateInput, TimeInput + +from .utils import tz_now + + +class QSJPSForm(forms.Form): + day = forms.DateField(label="Jour", + widget=DateInput(attrs={"type": "date"})) + + # Ces champs n’acceptent pas les secondes + begin = forms.TimeField(label="Heure de début", input_formats=("%H:%M",), + widget=TimeInput(attrs={"type": "time"})) + end = forms.TimeField(label="Heure de fin", input_formats=("%H:%M",), + widget=TimeInput(attrs={"type": "time"})) + + def __init__(self, *args, **kwargs): + super(QSJPSForm, self).__init__(*args, **kwargs) + + # On définit les valeurs par défaut de cette manière pour + # éviter les mauvaises surprises. On retire les secondes des + # heures de début et de fin. + self.fields["day"].initial = tz_now().strftime("%Y-%m-%d") + self.fields["begin"].initial = tz_now().strftime("%H:%M") + self.fields["end"].initial = (tz_now() + timedelta(hours=1)) \ + .strftime("%H:%M") + + def clean(self): + form_data = self.cleaned_data + + # On vérifie que les valeurs de début et de fin sont correctes + # (si ce n’est pas le cas, elles ne se trouvent pas dans le + # dictionnaire), et, le cas échéant, on vérifie que l’heure de + # début est strictement inférieure à l’heure de fin. + if "begin" in form_data and "end" in form_data and \ + form_data["begin"] >= form_data["end"]: + # Si l’heure de fin est plus petite ou égale, on affiche + # une erreur. + self._errors["end"].append("L’heure de début doit être supérieure " + "à celle de fin.") + return form_data |