msg erreur si nom ou prenom manquant + code clean

This commit is contained in:
Emmanuel Viennet 2022-03-18 19:45:55 +01:00
parent 30e3328057
commit 20317f7e6b

View File

@ -154,9 +154,9 @@ def sco_import_generate_excel_sample(
with_codesemestre=True, with_codesemestre=True,
only_tables=None, only_tables=None,
with_groups=True, with_groups=True,
exclude_cols=[], exclude_cols=(),
extra_cols=[], extra_cols=(),
group_ids=[], group_ids=(),
): ):
"""Generates an excel document based on format fmt """Generates an excel document based on format fmt
(format is the result of sco_import_format()) (format is the result of sco_import_format())
@ -167,7 +167,7 @@ def sco_import_generate_excel_sample(
style = sco_excel.excel_make_style(bold=True) style = sco_excel.excel_make_style(bold=True)
style_required = sco_excel.excel_make_style(bold=True, color=COLORS.RED) style_required = sco_excel.excel_make_style(bold=True, color=COLORS.RED)
titles = [] titles = []
titlesStyles = [] titles_styles = []
for l in fmt: for l in fmt:
name = l[0].lower() name = l[0].lower()
if (not with_codesemestre) and name == "codesemestre": if (not with_codesemestre) and name == "codesemestre":
@ -177,15 +177,15 @@ def sco_import_generate_excel_sample(
if name in exclude_cols: if name in exclude_cols:
continue # colonne exclue continue # colonne exclue
if int(l[3]): if int(l[3]):
titlesStyles.append(style) titles_styles.append(style)
else: else:
titlesStyles.append(style_required) titles_styles.append(style_required)
titles.append(name) titles.append(name)
if with_groups and "groupes" not in titles: if with_groups and "groupes" not in titles:
titles.append("groupes") titles.append("groupes")
titlesStyles.append(style) titles_styles.append(style)
titles += extra_cols titles += extra_cols
titlesStyles += [style] * len(extra_cols) titles_styles += [style] * len(extra_cols)
if group_ids: if group_ids:
groups_infos = sco_groups_view.DisplayedGroupsInfos(group_ids) groups_infos = sco_groups_view.DisplayedGroupsInfos(group_ids)
members = groups_infos.members members = groups_infos.members
@ -194,7 +194,7 @@ def sco_import_generate_excel_sample(
% (group_ids, len(members)) % (group_ids, len(members))
) )
titles = ["etudid"] + titles titles = ["etudid"] + titles
titlesStyles = [style] + titlesStyles titles_styles = [style] + titles_styles
# rempli table avec données actuelles # rempli table avec données actuelles
lines = [] lines = []
for i in members: for i in members:
@ -213,7 +213,7 @@ def sco_import_generate_excel_sample(
else: else:
lines = [[]] # empty content, titles only lines = [[]] # empty content, titles only
return sco_excel.excel_simple_table( return sco_excel.excel_simple_table(
titles=titles, titles_styles=titlesStyles, sheet_name="Etudiants", lines=lines titles=titles, titles_styles=titles_styles, sheet_name="Etudiants", lines=lines
) )
@ -256,7 +256,7 @@ def scolars_import_excel_file(
formsemestre_id=None, formsemestre_id=None,
check_homonyms=True, check_homonyms=True,
require_ine=False, require_ine=False,
exclude_cols=[], exclude_cols=(),
): ):
"""Importe etudiants depuis fichier Excel """Importe etudiants depuis fichier Excel
et les inscrit dans le semestre indiqué (et à TOUS ses modules) et les inscrit dans le semestre indiqué (et à TOUS ses modules)
@ -302,7 +302,8 @@ def scolars_import_excel_file(
else: else:
unknown.append(f) unknown.append(f)
raise ScoValueError( raise ScoValueError(
"Nombre de colonnes incorrect (devrait être %d, et non %d) <br/> (colonnes manquantes: %s, colonnes invalides: %s)" """Nombre de colonnes incorrect (devrait être %d, et non %d)<br/>
(colonnes manquantes: %s, colonnes invalides: %s)"""
% (len(titles), len(fs), list(missing.keys()), unknown) % (len(titles), len(fs), list(missing.keys()), unknown)
) )
titleslist = [] titleslist = []
@ -313,7 +314,7 @@ def scolars_import_excel_file(
# ok, same titles # ok, same titles
# Start inserting data, abort whole transaction in case of error # Start inserting data, abort whole transaction in case of error
created_etudids = [] created_etudids = []
NbImportedHomonyms = 0 np_imported_homonyms = 0
GroupIdInferers = {} GroupIdInferers = {}
try: # --- begin DB transaction try: # --- begin DB transaction
linenum = 0 linenum = 0
@ -377,10 +378,10 @@ def scolars_import_excel_file(
if val: if val:
try: try:
val = sco_excel.xldate_as_datetime(val) val = sco_excel.xldate_as_datetime(val)
except ValueError: except ValueError as exc:
raise ScoValueError( raise ScoValueError(
f"date invalide ({val}) sur ligne {linenum}, colonne {titleslist[i]}" f"date invalide ({val}) sur ligne {linenum}, colonne {titleslist[i]}"
) ) from exc
# INE # INE
if ( if (
titleslist[i].lower() == "code_ine" titleslist[i].lower() == "code_ine"
@ -404,15 +405,17 @@ def scolars_import_excel_file(
if values["code_ine"] and not is_new_ine: if values["code_ine"] and not is_new_ine:
raise ScoValueError("Code INE dupliqué (%s)" % values["code_ine"]) raise ScoValueError("Code INE dupliqué (%s)" % values["code_ine"])
# Check nom/prenom # Check nom/prenom
ok, NbHomonyms = sco_etud.check_nom_prenom( ok = False
cnx, nom=values["nom"], prenom=values["prenom"] if "nom" in values and "prenom" in values:
) ok, nb_homonyms = sco_etud.check_nom_prenom(
cnx, nom=values["nom"], prenom=values["prenom"]
)
if not ok: if not ok:
raise ScoValueError( raise ScoValueError(
"nom ou prénom invalide sur la ligne %d" % (linenum) "nom ou prénom invalide sur la ligne %d" % (linenum)
) )
if NbHomonyms: if nb_homonyms:
NbImportedHomonyms += 1 np_imported_homonyms += 1
# Insert in DB tables # Insert in DB tables
formsemestre_id_etud = _import_one_student( formsemestre_id_etud = _import_one_student(
cnx, cnx,
@ -425,11 +428,11 @@ def scolars_import_excel_file(
) )
# Verification proportion d'homonymes: si > 10%, abandonne # Verification proportion d'homonymes: si > 10%, abandonne
log("scolars_import_excel_file: detected %d homonyms" % NbImportedHomonyms) log("scolars_import_excel_file: detected %d homonyms" % np_imported_homonyms)
if check_homonyms and NbImportedHomonyms > len(created_etudids) / 10: if check_homonyms and np_imported_homonyms > len(created_etudids) / 10:
log("scolars_import_excel_file: too many homonyms") log("scolars_import_excel_file: too many homonyms")
raise ScoValueError( raise ScoValueError(
"Il y a trop d'homonymes (%d étudiants)" % NbImportedHomonyms "Il y a trop d'homonymes (%d étudiants)" % np_imported_homonyms
) )
except: except:
cnx.rollback() cnx.rollback()