From edadbdf113b43a6090956594076e659399ad833d Mon Sep 17 00:00:00 2001 From: jean-marie Place Date: Sun, 22 Aug 2021 15:25:25 +0200 Subject: [PATCH] =?UTF-8?q?import=20utilisateurs=20=C3=A0=20compl=C3=A9ter?= =?UTF-8?q?:=20titre=20page=20de=20diagnostic=20et=20sendmail=20=C3=A0=20c?= =?UTF-8?q?abler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/scodoc/sco_import_users.py | 134 +++++++++++++++++---------------- 1 file changed, 69 insertions(+), 65 deletions(-) diff --git a/app/scodoc/sco_import_users.py b/app/scodoc/sco_import_users.py index 655a16c2..8ab1ae39 100644 --- a/app/scodoc/sco_import_users.py +++ b/app/scodoc/sco_import_users.py @@ -141,75 +141,79 @@ def import_users(users, auth_dept=""): def append_msg(msg): msg_list.append("Ligne %s : %s" % (line, msg)) - created = [] # liste de uid créés - msg_list = [] - line = 1 # satr from excel line #2 - ok = True - try: - for u in users: - line = line + 1 - ok, msg = sco_users.check_modif_user( - 0, - user_name=u["user_name"], - nom=u["nom"], - prenom=u["prenom"], - email=u["email"], - roles=u["roles"], - ) - if not ok: - append_msg("identifiant '%s' %s" % (u["user_name"], msg)) - # raise ScoValueError( - # "données invalides pour %s: %s" % (u["user_name"], msg) - # ) - u["passwd"] = generate_password() - # - # check identifiant - if not re.match(r"^[a-zA-Z0-9@\\\-_\\\.]*$", u["user_name"]): - ok = False - append_msg( - "identifiant '%s' invalide (pas d'accents ni de caractères spéciaux)" - % u["user_name"] + if len(users) == 0: + ok = False + msg_list = ["Feuilles vide ou illisible"] + else: + created = [] # liste de uid créés + msg_list = [] + line = 1 # satr from excel line #2 + ok = True + try: + for u in users: + line = line + 1 + ok, msg = sco_users.check_modif_user( + 0, + user_name=u["user_name"], + nom=u["nom"], + prenom=u["prenom"], + email=u["email"], + roles=u["roles"], ) - elif len(u["user_name"]) > 64: - ok = False - append_msg( - "identifiant '%s' trop long (64 caractères)" % u["user_name"] - ) - if len(u["nom"]) > 64: - ok = False - append_msg("nom '%s' trop long (64 caractères)" % u["nom"]) - if len(u["prenom"]) > 64: - ok = False - append_msg("prenom '%s' trop long (64 caractères)" % u["prenom"]) - if len(u["email"]) > 120: - ok = False - append_msg("email '%s' trop long (120 caractères)" % u["email"]) - # check département - if u["dept"] != "": - dept = Departement.query.filter_by(acronym=u["dept"]).first() - if dept is None: + if not ok: + append_msg("identifiant '%s' %s" % (u["user_name"], msg)) + # raise ScoValueError( + # "données invalides pour %s: %s" % (u["user_name"], msg) + # ) + u["passwd"] = generate_password() + # + # check identifiant + if not re.match(r"^[a-zA-Z0-9@\\\-_\\\.]*$", u["user_name"]): ok = False - append_msg("département '%s' inexistant" % u["dept"]) - for role in u["roles"].split(","): - try: - _, _ = UserRole.role_dept_from_string(role) - except ScoValueError as value_error: + append_msg( + "identifiant '%s' invalide (pas d'accents ni de caractères spéciaux)" + % u["user_name"] + ) + elif len(u["user_name"]) > 64: ok = False - append_msg("role : %s " % role) - # Création de l'utilisateur (via SQLAlchemy) - if ok: - user = User() - user.from_dict(u, new_user=True) - db.session.add(user) - created.append(u["user_name"]) - db.session.commit() - except ScoValueError as value_error: - log("import_users: exception: abort create %s" % str(created)) - db.session.rollback() - raise ScoValueError(msg) # re-raise exception + append_msg( + "identifiant '%s' trop long (64 caractères)" % u["user_name"] + ) + if len(u["nom"]) > 64: + ok = False + append_msg("nom '%s' trop long (64 caractères)" % u["nom"]) + if len(u["prenom"]) > 64: + ok = False + append_msg("prenom '%s' trop long (64 caractères)" % u["prenom"]) + if len(u["email"]) > 120: + ok = False + append_msg("email '%s' trop long (120 caractères)" % u["email"]) + # check département + if u["dept"] != "": + dept = Departement.query.filter_by(acronym=u["dept"]).first() + if dept is None: + ok = False + append_msg("département '%s' inexistant" % u["dept"]) + for role in u["roles"].split(","): + try: + _, _ = UserRole.role_dept_from_string(role) + except ScoValueError as value_error: + ok = False + append_msg("role : %s " % role) + # Création de l'utilisateur (via SQLAlchemy) + if ok: + user = User() + user.from_dict(u, new_user=True) + db.session.add(user) + created.append(u["user_name"]) + db.session.commit() + except ScoValueError as value_error: + log("import_users: exception: abort create %s" % str(created)) + db.session.rollback() + raise ScoValueError(msg) # re-raise exception - for user in users: - mail_password(user) + for user in users: + mail_password(user) return ok, msg_list