From 2f78f7f6fc81e260ed75607b981a5e2c8bfc0847 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Wed, 8 Sep 2021 00:10:36 +0200 Subject: [PATCH] Message erreur si import Excel d'une date invalide --- app/scodoc/sco_excel.py | 47 +++------------------------------- app/scodoc/sco_import_etuds.py | 8 ++++-- 2 files changed, 9 insertions(+), 46 deletions(-) diff --git a/app/scodoc/sco_excel.py b/app/scodoc/sco_excel.py index 32cf27c0a3..c9551b692b 100644 --- a/app/scodoc/sco_excel.py +++ b/app/scodoc/sco_excel.py @@ -79,52 +79,11 @@ def send_excel_file(request, data, filename, mime=scu.XLSX_MIMETYPE): # font, border, number_format, fill, .. (cf https://openpyxl.readthedocs.io/en/stable/styles.html#working-with-styles) -# (stolen from xlrd) -# Convert an Excel number (presumed to represent a date, a datetime or a time) into -# a Python datetime.datetime -# @param xldate The Excel number -# @param datemode 0: 1900-based, 1: 1904-based. -# @return a datetime.datetime object, to the nearest_second. -#
Special case: if 0.0 <= xldate < 1.0, it is assumed to represent a time; -# a datetime.time object will be returned. -#
Note: 1904-01-01 is not regarded as a valid date in the datemode 1 system; its "serial number" -# is zero. -# -# _XLDAYS_TOO_LARGE = (2958466, 2958466 - 1462) # This is equivalent to 10000-01-01 -# - - def xldate_as_datetime(xldate, datemode=0): + """Conversion d'une date Excel en date + Peut lever une ValueError + """ return openpyxl.utils.datetime.from_ISO8601(xldate) - # if datemode not in (0, 1): - # raise ValueError("invalid mode %s" % datemode) - # if xldate == 0.00: - # return datetime.time(0, 0, 0) - # if xldate < 0.00: - # raise ValueError("invalid date code %s" % xldate) - # xldays = int(xldate) - # frac = xldate - xldays - # seconds = int(round(frac * 86400.0)) - # assert 0 <= seconds <= 86400 - # if seconds == 86400: - # seconds = 0 - # xldays += 1 - # if xldays >= _XLDAYS_TOO_LARGE[datemode]: - # raise ValueError("date too large %s" % xldate) - # - # if xldays == 0: - # # second = seconds % 60; minutes = seconds // 60 - # minutes, second = divmod(seconds, 60) - # # minute = minutes % 60; hour = minutes // 60 - # hour, minute = divmod(minutes, 60) - # return datetime.time(hour, minute, second) - # - # if xldays < 61 and datemode == 0: - # raise ValueError("ambiguous date %s" % xldate) - # - # return datetime.datetime.fromordinal( - # xldays + 693594 + 1462 * datemode - # ) + datetime.timedelta(seconds=seconds) class ScoExcelBook: diff --git a/app/scodoc/sco_import_etuds.py b/app/scodoc/sco_import_etuds.py index 5e5016bcd8..b9b8b5de53 100644 --- a/app/scodoc/sco_import_etuds.py +++ b/app/scodoc/sco_import_etuds.py @@ -378,8 +378,12 @@ def scolars_import_excel_file( # Excel date conversion: if titleslist[i].lower() == "date_naissance": if val: - # if re.match(r"^[0-9]*\.?[0-9]*$", str(val)): - val = sco_excel.xldate_as_datetime(val) + try: + val = sco_excel.xldate_as_datetime(val) + except ValueError: + raise ScoValueError( + f"date invalide ({val}) sur ligne {linenum}, colonne {titleslist[i]}" + ) # INE if ( titleslist[i].lower() == "code_ine"