aboutsummaryrefslogtreecommitdiff
path: root/forms.py
blob: d886dbf9ae8690748c02655daa91c6a73a3b8ee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#    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 .utils import tz_now


class QSJPSForm(forms.Form):
    day = forms.DateField(label="Jour")

    # Ces champs n’acceptent pas les secondes
    begin = forms.TimeField(label="Heure de début", input_formats=("%H:%M",))
    end = forms.TimeField(label="Heure de fin", input_formats=("%H:%M",))

    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()
        self.fields["begin"].initial = tz_now().strftime("%H:%M")
        self.fields["end"].initial = (tz_now() + timedelta(hours=1)).strftime("%H:%M")