From 8d72229e8b0dc7c7b2107c6816e10249a56a8aca Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 3 Oct 2022 11:59:59 +0200 Subject: [PATCH] modernise conversion date --- app/scodoc/notesdb.py | 45 +++++++++---------------------------------- app/views/notes.py | 1 - 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/app/scodoc/notesdb.py b/app/scodoc/notesdb.py index 196274955..e2cb304e3 100644 --- a/app/scodoc/notesdb.py +++ b/app/scodoc/notesdb.py @@ -459,49 +459,22 @@ def dictfilter(d, fields, filter_nulls=True): # --- Misc Tools -def DateDMYtoISO(dmy, null_is_empty=False): +def DateDMYtoISO(dmy: str, null_is_empty=False) -> str: "convert date string from french format to ISO" if not dmy: if null_is_empty: return "" else: return None - if not isinstance(dmy, str): + if hasattr(dmy, "strftime"): return dmy.strftime("%Y-%m-%d") - - t = dmy.split("/") - - if len(t) != 3: - raise ScoValueError('Format de date (j/m/a) invalide: "%s"' % str(dmy)) - day, month, year = t - year = int(year) - month = int(month) - day = int(day) - # accept years YYYY or YY, uses 1970 as pivot - if year < 100: - if year > 70: - year += 1900 - else: - year += 2000 - - if month < 1 or month > 12: - raise ScoValueError("mois de la date invalide ! (%s)" % month) - # compute nb of day in month: - mo = month - if mo > 7: - mo = mo + 1 - if mo % 2: - MonthNbDays = 31 - elif mo == 2: - if year % 4 == 0 and ((year % 100 != 0) or (year % 400 == 0)): - MonthNbDays = 29 # leap - else: - MonthNbDays = 28 - else: - MonthNbDays = 30 - if day < 1 or day > MonthNbDays: - raise ScoValueError("jour de la date invalide ! (%s)" % day) - return "%04d-%02d-%02d" % (year, month, day) + if not isinstance(dmy, str): + raise ScoValueError(f'Date (j/m/a) invalide: "{dmy}"') + try: + dt = datetime.datetime.strptime(dmy, "%d/%m/%Y") + except ValueError as exc: + raise ScoValueError(f'Date (j/m/a) invalide: "{dmy}"') from exc + return dt.date().isoformat() def DateISOtoDMY(isodate): diff --git a/app/views/notes.py b/app/views/notes.py index d9ee54c4f..4dc35eaff 100644 --- a/app/views/notes.py +++ b/app/views/notes.py @@ -1991,7 +1991,6 @@ def formsemestre_bulletins_mailetuds( """ prefer_mail_perso = int(prefer_mail_perso) formsemestre = FormSemestre.query.get_or_404(formsemestre_id) - nt: NotesTableCompat = res_sem.load_formsemestre_results(formsemestre) inscriptions = [ inscription for inscription in formsemestre.inscriptions