aboutsummaryrefslogtreecommitdiff
path: root/forms.py
diff options
context:
space:
mode:
authorAlban Gruin2018-04-25 21:26:03 +0200
committerAlban Gruin2018-04-25 21:26:03 +0200
commit54c5dbb98293acea9b470808e5a35e99c004f265 (patch)
tree7fb350d07e2f2a4d687b00fd034e638bd02f8526 /forms.py
parent772caa72ce7f80bfeb5fbb1d05b57838dafd48c3 (diff)
parent5488a93bf2e04d2f19e287186011dcbb436a238b (diff)
Merge branch 'stable/0.13.z' into prod/pa1ch/0.y.z
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