Modif mail import user. A compléter suivant la PR de JMP

This commit is contained in:
Emmanuel Viennet 2021-10-13 10:33:20 +02:00
parent 6e1bc9665d
commit dc26d1edea
3 changed files with 28 additions and 37 deletions

View File

@ -27,27 +27,23 @@
"""Import d'utilisateurs via fichier Excel """Import d'utilisateurs via fichier Excel
""" """
import random, time import random
import re import time
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from flask import g, url_for
from email.header import Header from flask_login import current_user
from app import db, Departement from app import db
from app import email
from app.auth.models import User, UserRole
import app.scodoc.sco_utils as scu import app.scodoc.sco_utils as scu
from app import log from app import log
from app.scodoc.sco_exceptions import AccessDenied, ScoValueError, ScoException from app.scodoc.sco_exceptions import AccessDenied, ScoValueError
from app.scodoc import sco_excel from app.scodoc import sco_excel
from app.scodoc import sco_preferences from app.scodoc import sco_preferences
from app.scodoc import sco_users from app.scodoc import sco_users
from flask import g
from flask_login import current_user
from app.auth.models import User, UserRole
from app import email
TITLES = ("user_name", "nom", "prenom", "email", "roles", "dept") TITLES = ("user_name", "nom", "prenom", "email", "roles", "dept")
COMMENTS = ( COMMENTS = (
@ -90,7 +86,7 @@ def import_excel_file(datafile):
""" """
Import scodoc users from Excel file. Import scodoc users from Excel file.
This method: This method:
* checks that the current_user has the ability to do so (at the moment only a SuperAdmin). He may thereoff import users with any well formed role into any deprtment (or all) * checks that the current_user has the ability to do so (at the moment only a SuperAdmin). He may thereoff import users with any well formed role into any department (or all)
* Once the check is done ans successfull, build the list of users (does not check the data) * Once the check is done ans successfull, build the list of users (does not check the data)
* call :func:`import_users` to actually do the job * call :func:`import_users` to actually do the job
history: scodoc7 with no SuperAdmin every Admin_XXX could import users. history: scodoc7 with no SuperAdmin every Admin_XXX could import users.
@ -98,7 +94,6 @@ def import_excel_file(datafile):
:return: same as import users :return: same as import users
""" """
# Check current user privilege # Check current user privilege
auth_dept = current_user.dept
auth_name = str(current_user) auth_name = str(current_user)
if not current_user.is_administrator(): if not current_user.is_administrator():
raise AccessDenied("invalid user (%s) must be SuperAdmin" % auth_name) raise AccessDenied("invalid user (%s) must be SuperAdmin" % auth_name)
@ -127,7 +122,8 @@ def import_excel_file(datafile):
del cols[tit] del cols[tit]
if cols or unknown: if cols or unknown:
raise ScoValueError( raise ScoValueError(
"colonnes incorrectes (on attend %d, et non %d) <br/> (colonnes manquantes: %s, colonnes invalides: %s)" """colonnes incorrectes (on attend %d, et non %d) <br/>
(colonnes manquantes: %s, colonnes invalides: %s)"""
% (len(TITLES), len(fs), list(cols.keys()), unknown) % (len(TITLES), len(fs), list(cols.keys()), unknown)
) )
# ok, same titles... : build the list of dictionaries # ok, same titles... : build the list of dictionaries
@ -192,9 +188,7 @@ def import_users(users):
) )
if not user_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(
# "données invalides pour %s: %s" % (u["user_name"], msg)
# )
u["passwd"] = generate_password() u["passwd"] = generate_password()
# #
# check identifiant # check identifiant
@ -224,7 +218,7 @@ def import_users(users):
import_ok = False import_ok = False
except ScoValueError as value_error: except ScoValueError as value_error:
log("import_users: exception: abort create %s" % str(created.keys())) log("import_users: exception: abort create %s" % str(created.keys()))
raise ScoValueError(msg) # re-raise exception raise ScoValueError(msg) from value_error
if import_ok: if import_ok:
for u in created.values(): for u in created.values():
# Création de l'utilisateur (via SQLAlchemy) # Création de l'utilisateur (via SQLAlchemy)
@ -244,7 +238,7 @@ def import_users(users):
ALPHABET = r"""ABCDEFGHIJKLMNPQRSTUVWXYZ123456789123456789AEIOU""" ALPHABET = r"""ABCDEFGHIJKLMNPQRSTUVWXYZ123456789123456789AEIOU"""
PASSLEN = 6 PASSLEN = 8
RNG = random.Random(time.time()) RNG = random.Random(time.time())
@ -259,23 +253,18 @@ def generate_password():
return "".join(RNG.sample(l, PASSLEN)) return "".join(RNG.sample(l, PASSLEN))
def mail_password(u, context=None, reset=False): def mail_password(user: dict, reset=False) -> None:
"Send password by email" "Send password by email"
if not u["email"]: if not user["email"]:
return return
u[ user["url"] = url_for("scodoc.index")
"url"
] = (
scu.ScoURL()
) # TODO set auth page URL ? (shared by all departments) ../auth/login
txt = ( txt = (
""" """
Bonjour %(prenom)s %(nom)s, Bonjour %(prenom)s %(nom)s,
""" """
% u % user
) )
if reset: if reset:
txt += ( txt += (
@ -288,7 +277,7 @@ Votre nom d'utilisateur est %(user_name)s
Vous devrez changer ce mot de passe lors de votre première connexion Vous devrez changer ce mot de passe lors de votre première connexion
sur %(url)s sur %(url)s
""" """
% u % user
) )
else: else:
txt += ( txt += (
@ -303,13 +292,13 @@ Le logiciel est accessible sur: %(url)s
Vous êtes invité à changer ce mot de passe au plus vite (cliquez sur Vous êtes invité à changer ce mot de passe au plus vite (cliquez sur
votre nom en haut à gauche de la page d'accueil). votre nom en haut à gauche de la page d'accueil).
""" """
% u % user
) )
txt += ( txt += (
""" """
ScoDoc est un logiciel libre développé à l'Université Paris 13 par Emmanuel Viennet. ScoDoc est un logiciel libre développé par Emmanuel Viennet et l'association ScoDoc.
Pour plus d'informations sur ce logiciel, voir %s Pour plus d'informations sur ce logiciel, voir %s
""" """
@ -321,4 +310,4 @@ Pour plus d'informations sur ce logiciel, voir %s
else: else:
subject = "Votre accès ScoDoc" subject = "Votre accès ScoDoc"
sender = sco_preferences.get_preference("email_from_addr") sender = sco_preferences.get_preference("email_from_addr")
email.send_email(subject, sender, [u["email"]], txt) email.send_email(subject, sender, [user["email"]], txt)

View File

@ -400,9 +400,11 @@ def check_modif_user(
returns (ok, msg) returns (ok, msg)
- ok : si vrai, peut continuer avec ces parametres - ok : si vrai, peut continuer avec ces parametres
(si ok est faux, l'utilisateur peut quand même forcer la creation) (si ok est faux, l'utilisateur peut quand même forcer la creation)
- msg: message warning a presenter l'utilisateur - msg: message warning à presenter à l'utilisateur
"""
MSG_OPT = """
(vous pouvez forcer l'opération en cochant "<em>Ignorer les avertissements</em>" en bas de page)
""" """
MSG_OPT = """Attention: %s (vous pouvez forcer l'opération en cochant "<em>Ignorer les avertissements</em>" en bas de page)"""
# ce login existe ? # ce login existe ?
user = _user_list(user_name) user = _user_list(user_name)
if edit and not user: # safety net, le user_name ne devrait pas changer if edit and not user: # safety net, le user_name ne devrait pas changer

View File

@ -1,7 +1,7 @@
# -*- mode: python -*- # -*- mode: python -*-
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
SCOVERSION = "9.0.52" SCOVERSION = "9.0.53"
SCONAME = "ScoDoc" SCONAME = "ScoDoc"