From c38b8aa29713f44ac71f1b7db56b703828dc8c04 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sun, 15 Sep 2024 11:14:50 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20=C3=A9dition=20heures=20assiduit=C3=A9.?= =?UTF-8?q?=20Introduces=20ScoDateField/ScoTimeField.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/forms/__init__.py | 50 +++++++++++++ app/forms/assiduite/ajout_assiduite_etud.py | 68 +++--------------- app/forms/assiduite/edit_assiduite_etud.py | 75 +++----------------- app/forms/main/config_assiduites.py | 36 ++-------- app/templates/jury/formsemestre_bilan_ues.j2 | 7 +- 5 files changed, 78 insertions(+), 158 deletions(-) diff --git a/app/forms/__init__.py b/app/forms/__init__.py index 3b8722daf..434d20e79 100644 --- a/app/forms/__init__.py +++ b/app/forms/__init__.py @@ -1,7 +1,9 @@ """WTF Forms for ScoDoc """ +import re from flask_wtf import FlaskForm +from wtforms import StringField, validators class ScoDocForm(FlaskForm): @@ -21,3 +23,51 @@ class ScoDocForm(FlaskForm): self.error_messages.append(err_msg) if field: field.errors.append(err_msg) + + +class ScoDateField(StringField): + """A WTF field for date, using datepicker""" + + def __init__(self, label: str, field_id: str, *args, **kwargs): + render_kw = { + "class": "datepicker", + "size": 10, + "id": field_id, + } + if "render_kw" in kwargs: + render_kw.update(kwargs["render_kw"]) + super().__init__( + label, + validators=[validators.Length(max=10)], + render_kw=render_kw, + *args, + **kwargs + ) + + +class ScoTimeField(StringField): + """A WTF field for time, accept 12:34, 12h34, 12H34 + Uses timepicker + """ + + def __init__(self, label: str, field_id: str, *args, **kwargs): + render_kw = { + "class": "timepicker", + "size": 5, + "id": field_id, + } + if "render_kw" in kwargs: + render_kw.update(kwargs["render_kw"]) + super().__init__( + label, + validators=[validators.Length(max=5)], + default="", + render_kw=render_kw, + filters=[ + lambda x: ( + re.sub(r"(^\d{2})[hH](\d{2}$)", r"\1:\2", x.strip()) if x else None + ) + ], + *args, + **kwargs + ) diff --git a/app/forms/assiduite/ajout_assiduite_etud.py b/app/forms/assiduite/ajout_assiduite_etud.py index 5489ac350..378f2b660 100644 --- a/app/forms/assiduite/ajout_assiduite_etud.py +++ b/app/forms/assiduite/ajout_assiduite_etud.py @@ -34,14 +34,13 @@ from flask_wtf.file import MultipleFileField from wtforms import ( BooleanField, SelectField, - StringField, SubmitField, RadioField, TextAreaField, validators, ) from wtforms.validators import DataRequired -from app.scodoc import sco_utils as scu +from app.forms import ScoDateField, ScoTimeField class AjoutAssiOrJustForm(FlaskForm): @@ -67,64 +66,15 @@ class AjoutAssiOrJustForm(FlaskForm): for field in self: field.render_kw = {"disabled": True} - date_debut = StringField( - "Date de début", - validators=[validators.Length(max=10)], - render_kw={ - "class": "datepicker", - "size": 10, - "id": "assi_date_debut", - }, - ) - heure_debut = StringField( - "Heure début", - default="", - validators=[validators.Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_heure_debut", - }, - ) - heure_fin = StringField( - "Heure fin", - default="", - validators=[validators.Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_heure_fin", - }, - ) - date_fin = StringField( - "Date de fin (si plusieurs jours)", - validators=[validators.Length(max=10)], - render_kw={ - "class": "datepicker", - "size": 10, - "id": "assi_date_fin", - }, - ) + date_debut = ScoDateField("Date de début", "assi_date_debut") + heure_debut = ScoTimeField("Heure début", "assi_heure_debut") + heure_fin = ScoTimeField("Heure fin", "assi_heure_fin") + + date_fin = ScoDateField("Date de fin (si plusieurs jours)", "assi_date_fin") + + entry_date = ScoDateField("Date de dépôt ou saisie", "entry_date") + entry_time = ScoTimeField("Heure dépôt", "entry_time") - entry_date = StringField( - "Date de dépôt ou saisie", - validators=[validators.Length(max=10)], - render_kw={ - "class": "datepicker", - "size": 10, - "id": "entry_date", - }, - ) - entry_time = StringField( - "Heure dépôt", - default="", - validators=[validators.Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_heure_fin", - }, - ) submit = SubmitField("Enregistrer") cancel = SubmitField("Annuler", render_kw={"formnovalidate": True}) diff --git a/app/forms/assiduite/edit_assiduite_etud.py b/app/forms/assiduite/edit_assiduite_etud.py index cdcff62e0..6f16cc6ff 100644 --- a/app/forms/assiduite/edit_assiduite_etud.py +++ b/app/forms/assiduite/edit_assiduite_etud.py @@ -1,14 +1,9 @@ """ """ from flask_wtf import FlaskForm -from wtforms import ( - StringField, - SelectField, - RadioField, - TextAreaField, - validators, - SubmitField, -) +from wtforms import SelectField, RadioField, TextAreaField, validators, SubmitField + +from app.forms import ScoDateField, ScoTimeField from app.scodoc.sco_utils import EtatAssiduite @@ -60,63 +55,13 @@ class EditAssiForm(FlaskForm): "maxlength": 500, }, ) - date_debut = StringField( - "Date de début", - validators=[validators.Length(max=10)], - render_kw={ - "class": "datepicker", - "size": 10, - "id": "assi_date_debut", - }, - ) - heure_debut = StringField( - "Heure début", - default="", - validators=[validators.Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_heure_debut", - }, - ) - heure_fin = StringField( - "Heure fin", - default="", - validators=[validators.Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_heure_fin", - }, - ) - date_fin = StringField( - "Date de fin", - validators=[validators.Length(max=10)], - render_kw={ - "class": "datepicker", - "size": 10, - "id": "assi_date_fin", - }, - ) - entry_date = StringField( - "Date de dépôt ou saisie", - validators=[validators.Length(max=10)], - render_kw={ - "class": "datepicker", - "size": 10, - "id": "entry_date", - }, - ) - entry_time = StringField( - "Heure dépôt", - default="", - validators=[validators.Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_heure_fin", - }, - ) + date_debut = ScoDateField("Date de début", "assi_date_debut") + heure_debut = ScoTimeField("Heure début", "assi_heure_debut") + heure_fin = ScoTimeField("Heure fin", "assi_heure_fin") + + date_fin = ScoDateField("Date de fin", "assi_date_fin") + entry_date = ScoDateField("Date de dépôt ou saisie", "entry_date") + entry_time = ScoTimeField("Heure dépôt", "entry_time") submit = SubmitField("Enregistrer") cancel = SubmitField("Annuler", render_kw={"formnovalidate": True}) diff --git a/app/forms/main/config_assiduites.py b/app/forms/main/config_assiduites.py index 99b85beba..fa880d31d 100644 --- a/app/forms/main/config_assiduites.py +++ b/app/forms/main/config_assiduites.py @@ -28,7 +28,6 @@ """ Formulaire configuration Module Assiduités """ -import datetime import re from flask_wtf import FlaskForm @@ -36,7 +35,7 @@ from wtforms import DecimalField, SubmitField, ValidationError from wtforms.fields.simple import StringField from wtforms.validators import Optional, Length -from wtforms.widgets import TimeInput +from app.forms import ScoDateField, ScoTimeField def check_tick_time(form, field): @@ -77,36 +76,11 @@ def check_ics_regexp(form, field): class ConfigAssiduitesForm(FlaskForm): "Formulaire paramétrage Module Assiduité" - assi_morning_time = StringField( - "Début de la journée", - default="", - validators=[Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_morning_time", - }, - ) - assi_lunch_time = StringField( - "Heure de midi (date pivot entre matin et après-midi)", - default="", - validators=[Length(max=5)], - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_lunch_time", - }, - ) - assi_afternoon_time = StringField( - "Fin de la journée", - validators=[Length(max=5)], - default="", - render_kw={ - "class": "timepicker", - "size": 5, - "id": "assi_afternoon_time", - }, + assi_morning_time = ScoTimeField("Début de la journée", "assi_morning_time") + assi_lunch_time = ScoTimeField( + "Heure de midi (date pivot entre matin et après-midi)", "assi_lunch_time" ) + assi_afternoon_time = ScoTimeField("Fin de la journée", "assi_afternoon_time") assi_tick_time = DecimalField( "Granularité de la timeline (temps en minutes)", diff --git a/app/templates/jury/formsemestre_bilan_ues.j2 b/app/templates/jury/formsemestre_bilan_ues.j2 index acb89b0e9..efd62494b 100644 --- a/app/templates/jury/formsemestre_bilan_ues.j2 +++ b/app/templates/jury/formsemestre_bilan_ues.j2 @@ -6,8 +6,9 @@ {{ super() }}