Message erreur si import Excel d'une date invalide

This commit is contained in:
Emmanuel Viennet 2021-09-08 00:10:36 +02:00
parent 97fe4cc73f
commit 2f78f7f6fc
2 changed files with 9 additions and 46 deletions

View File

@ -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) # 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.
# <br>Special case: if 0.0 <= xldate < 1.0, it is assumed to represent a time;
# a datetime.time object will be returned.
# <br>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): 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) 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: class ScoExcelBook:

View File

@ -378,8 +378,12 @@ def scolars_import_excel_file(
# Excel date conversion: # Excel date conversion:
if titleslist[i].lower() == "date_naissance": if titleslist[i].lower() == "date_naissance":
if val: if val:
# if re.match(r"^[0-9]*\.?[0-9]*$", str(val)): try:
val = sco_excel.xldate_as_datetime(val) val = sco_excel.xldate_as_datetime(val)
except ValueError:
raise ScoValueError(
f"date invalide ({val}) sur ligne {linenum}, colonne {titleslist[i]}"
)
# INE # INE
if ( if (
titleslist[i].lower() == "code_ine" titleslist[i].lower() == "code_ine"