Merge pull request 'fix bugs + enhancements' (#110) from jmplace/ScoDoc-Lille:scodoc9_import_utilisateurs into master

Reviewed-on: https://scodoc.org/git/viennet/ScoDoc/pulls/110
This commit is contained in:
Emmanuel Viennet 2021-08-22 19:41:24 +02:00
commit 85ac8b8595
3 changed files with 42 additions and 39 deletions

View File

@ -280,8 +280,9 @@ class ScoExcelSheet:
cell.alignment = style["alignment"] cell.alignment = style["alignment"]
if not comment is None: if not comment is None:
cell.comment = Comment(comment, "scodoc") cell.comment = Comment(comment, "scodoc")
cell.comment.width = 400 lines = comment.splitlines()
cell.comment.height = 150 cell.comment.width = 7 * max([len(line) for line in lines])
cell.comment.height = 20 * len(lines)
return cell return cell
def make_row(self, values: list, style=None, comments=None): def make_row(self, values: list, style=None, comments=None):

View File

@ -142,25 +142,26 @@ def import_users(users, auth_dept=""):
msg_list.append("Ligne %s : %s" % (line, msg)) msg_list.append("Ligne %s : %s" % (line, msg))
if len(users) == 0: if len(users) == 0:
ok = False import_ok = False
msg_list = ["Feuilles vide ou illisible"] msg_list = ["Feuilles vide ou illisible"]
else: else:
created = [] # liste de uid créés created = [] # liste de uid créés
msg_list = [] msg_list = []
line = 1 # satr from excel line #2 line = 1 # satr from excel line #2
ok = True import_ok = True
try: try:
for u in users: for u in users:
user_ok = True
line = line + 1 line = line + 1
ok, msg = sco_users.check_modif_user( user_ok, msg = sco_users.check_modif_user(
0, 0,
user_name=u["user_name"], user_name=u["user_name"],
nom=u["nom"], nom=u["nom"],
prenom=u["prenom"], prenom=u["prenom"],
email=u["email"], email=u["email"],
roles=u["roles"], roles=u["roles"].split(","),
) )
if not ok: if not user_ok:
append_msg("identifiant '%s' %s" % (u["user_name"], msg)) append_msg("identifiant '%s' %s" % (u["user_name"], msg))
# raise ScoValueError( # raise ScoValueError(
# "données invalides pour %s: %s" % (u["user_name"], msg) # "données invalides pour %s: %s" % (u["user_name"], msg)
@ -169,52 +170,56 @@ def import_users(users, auth_dept=""):
# #
# check identifiant # check identifiant
if not re.match(r"^[a-zA-Z0-9@\\\-_\\\.]*$", u["user_name"]): if not re.match(r"^[a-zA-Z0-9@\\\-_\\\.]*$", u["user_name"]):
ok = False user_ok = False
append_msg( append_msg(
"identifiant '%s' invalide (pas d'accents ni de caractères spéciaux)" "identifiant '%s' invalide (pas d'accents ni de caractères spéciaux)"
% u["user_name"] % u["user_name"]
) )
elif len(u["user_name"]) > 64: elif len(u["user_name"]) > 64:
ok = False user_ok = False
append_msg( append_msg(
"identifiant '%s' trop long (64 caractères)" % u["user_name"] "identifiant '%s' trop long (64 caractères)" % u["user_name"]
) )
if len(u["nom"]) > 64: if len(u["nom"]) > 64:
ok = False user_ok = False
append_msg("nom '%s' trop long (64 caractères)" % u["nom"]) append_msg("nom '%s' trop long (64 caractères)" % u["nom"])
if len(u["prenom"]) > 64: if len(u["prenom"]) > 64:
ok = False user_ok = False
append_msg("prenom '%s' trop long (64 caractères)" % u["prenom"]) append_msg("prenom '%s' trop long (64 caractères)" % u["prenom"])
if len(u["email"]) > 120: if len(u["email"]) > 120:
ok = False user_ok = False
append_msg("email '%s' trop long (120 caractères)" % u["email"]) append_msg("email '%s' trop long (120 caractères)" % u["email"])
# check département # check département
if u["dept"] != "": if u["dept"] != "":
dept = Departement.query.filter_by(acronym=u["dept"]).first() dept = Departement.query.filter_by(acronym=u["dept"]).first()
if dept is None: if dept is None:
ok = False user_ok = False
append_msg("département '%s' inexistant" % u["dept"]) append_msg("département '%s' inexistant" % u["dept"])
for role in u["roles"].split(","): for role in u["roles"].split(","):
try: try:
_, _ = UserRole.role_dept_from_string(role) _, _ = UserRole.role_dept_from_string(role)
except ScoValueError as value_error: except ScoValueError as value_error:
ok = False user_ok = False
append_msg("role : %s " % role) append_msg("role : %s " % role)
# Création de l'utilisateur (via SQLAlchemy) # Création de l'utilisateur (via SQLAlchemy)
if ok: if user_ok:
user = User() created.append(u)
user.from_dict(u, new_user=True) else:
db.session.add(user) import_ok = False
created.append(u["user_name"])
db.session.commit()
except ScoValueError as value_error: except ScoValueError as value_error:
log("import_users: exception: abort create %s" % str(created)) log("import_users: exception: abort create %s" % str(created))
db.session.rollback()
raise ScoValueError(msg) # re-raise exception raise ScoValueError(msg) # re-raise exception
if import_ok:
for user in users: for u in created:
mail_password(user) u["roles_string"] = u["roles"]
return ok, msg_list user = User()
user.from_dict(u, new_user=True)
db.session.add(user)
db.session.commit()
mail_password(u)
else:
created = [] # reset # of created users to 0
return import_ok, msg_list, len(created)
# --------- Génération du mot de passe initial ----------- # --------- Génération du mot de passe initial -----------

View File

@ -469,7 +469,9 @@ def create_user_form(REQUEST, user_name=None, edit=0):
def import_users_generate_excel_sample(REQUEST): def import_users_generate_excel_sample(REQUEST):
"une feuille excel pour importation utilisateurs" "une feuille excel pour importation utilisateurs"
data = sco_import_users.generate_excel_sample() data = sco_import_users.generate_excel_sample()
return sco_excel.send_excel_file(REQUEST, data, "ImportUtilisateurs.xlsx") return sco_excel.send_excel_file(
REQUEST, data, "ImportUtilisateurs" + scu.XLSX_SUFFIX
)
@bp.route("/import_users_form", methods=["GET", "POST"]) @bp.route("/import_users_form", methods=["GET", "POST"])
@ -523,23 +525,18 @@ def import_users_form(REQUEST=None):
if tf[0] == 0: if tf[0] == 0:
return "\n".join(H) + tf[1] + "</li></ol>" + help + F return "\n".join(H) + tf[1] + "</li></ol>" + help + F
elif tf[0] == -1: elif tf[0] == -1:
return flask.redirect(url_for("scodoc.index")) return flask.redirect(back_url)
else: else:
# IMPORT # IMPORT
ok, diag = sco_import_users.import_excel_file(tf[2]["xlsfile"]) ok, diag, nb_created = sco_import_users.import_excel_file(tf[2]["xlsfile"])
H = [ H = [html_sco_header.sco_header(page_title="Import utilisateurs")]
html_sco_header.sco_header(page_title="Import utilisateurs"), H.append("<ul>")
"""<h2>Téléchargement d'une nouvelle liste d'utilisateurs</h2> for d in diag:
""", H.append("<li>%s</li>" % d)
] H.append("</ul>")
if diag:
H.append("""<h3>Problèmes rencontrés:</h3><ul>""")
for d in diag:
H.append(f"<li>{d}</li>")
H.append("</ul>")
if ok: if ok:
dest = url_for("users.index_html", scodoc_dept=g.scodoc_dept) dest = url_for("users.index_html", scodoc_dept=g.scodoc_dept)
H.append("<p>Ok, Import terminé !</p>") H.append("<p>Ok, Import terminé (%s utilisateurs créés)!</p>" % nb_created)
H.append('<p><a class="stdlink" href="%s">Continuer</a></p>' % dest) H.append('<p><a class="stdlink" href="%s">Continuer</a></p>' % dest)
else: else:
dest = url_for("users.import_users_form", scodoc_dept=g.scodoc_dept) dest = url_for("users.import_users_form", scodoc_dept=g.scodoc_dept)