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"]
if not comment is None:
cell.comment = Comment(comment, "scodoc")
cell.comment.width = 400
cell.comment.height = 150
lines = comment.splitlines()
cell.comment.width = 7 * max([len(line) for line in lines])
cell.comment.height = 20 * len(lines)
return cell
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))
if len(users) == 0:
ok = False
import_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
import_ok = True
try:
for u in users:
user_ok = True
line = line + 1
ok, msg = sco_users.check_modif_user(
user_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"],
roles=u["roles"].split(","),
)
if not ok:
if not user_ok:
append_msg("identifiant '%s' %s" % (u["user_name"], msg))
# raise ScoValueError(
# "données invalides pour %s: %s" % (u["user_name"], msg)
@ -169,52 +170,56 @@ def import_users(users, auth_dept=""):
#
# check identifiant
if not re.match(r"^[a-zA-Z0-9@\\\-_\\\.]*$", u["user_name"]):
ok = False
user_ok = False
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
user_ok = False
append_msg(
"identifiant '%s' trop long (64 caractères)" % u["user_name"]
)
if len(u["nom"]) > 64:
ok = False
user_ok = False
append_msg("nom '%s' trop long (64 caractères)" % u["nom"])
if len(u["prenom"]) > 64:
ok = False
user_ok = False
append_msg("prenom '%s' trop long (64 caractères)" % u["prenom"])
if len(u["email"]) > 120:
ok = False
user_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
user_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
user_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()
if user_ok:
created.append(u)
else:
import_ok = False
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)
return ok, msg_list
if import_ok:
for u in created:
u["roles_string"] = u["roles"]
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 -----------

View File

@ -469,7 +469,9 @@ def create_user_form(REQUEST, user_name=None, edit=0):
def import_users_generate_excel_sample(REQUEST):
"une feuille excel pour importation utilisateurs"
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"])
@ -523,23 +525,18 @@ def import_users_form(REQUEST=None):
if tf[0] == 0:
return "\n".join(H) + tf[1] + "</li></ol>" + help + F
elif tf[0] == -1:
return flask.redirect(url_for("scodoc.index"))
return flask.redirect(back_url)
else:
# IMPORT
ok, diag = sco_import_users.import_excel_file(tf[2]["xlsfile"])
H = [
html_sco_header.sco_header(page_title="Import utilisateurs"),
"""<h2>Téléchargement d'une nouvelle liste d'utilisateurs</h2>
""",
]
if diag:
H.append("""<h3>Problèmes rencontrés:</h3><ul>""")
for d in diag:
H.append(f"<li>{d}</li>")
H.append("</ul>")
ok, diag, nb_created = sco_import_users.import_excel_file(tf[2]["xlsfile"])
H = [html_sco_header.sco_header(page_title="Import utilisateurs")]
H.append("<ul>")
for d in diag:
H.append("<li>%s</li>" % d)
H.append("</ul>")
if ok:
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)
else:
dest = url_for("users.import_users_form", scodoc_dept=g.scodoc_dept)