aboutsummaryrefslogtreecommitdiff
path: root/forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'forms.py')
-rw-r--r--forms.py58
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