Modif mail import user. A compléter suivant la PR de JMP
This commit is contained in:
parent
57b1978420
commit
b392d3daeb
|
@ -27,27 +27,23 @@
|
|||
|
||||
"""Import d'utilisateurs via fichier Excel
|
||||
"""
|
||||
import random, time
|
||||
import re
|
||||
import random
|
||||
import time
|
||||
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from email.header import Header
|
||||
from flask import g, url_for
|
||||
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
|
||||
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_preferences
|
||||
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")
|
||||
COMMENTS = (
|
||||
|
@ -90,7 +86,7 @@ def import_excel_file(datafile):
|
|||
"""
|
||||
Import scodoc users from Excel file.
|
||||
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)
|
||||
* call :func:`import_users` to actually do the job
|
||||
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
|
||||
"""
|
||||
# Check current user privilege
|
||||
auth_dept = current_user.dept
|
||||
auth_name = str(current_user)
|
||||
if not current_user.is_administrator():
|
||||
raise AccessDenied("invalid user (%s) must be SuperAdmin" % auth_name)
|
||||
|
@ -127,7 +122,8 @@ def import_excel_file(datafile):
|
|||
del cols[tit]
|
||||
if cols or unknown:
|
||||
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)
|
||||
)
|
||||
# ok, same titles... : build the list of dictionaries
|
||||
|
@ -193,9 +189,7 @@ def import_users(users):
|
|||
)
|
||||
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)
|
||||
# )
|
||||
|
||||
u["passwd"] = generate_password()
|
||||
#
|
||||
# check identifiant
|
||||
|
@ -225,7 +219,7 @@ def import_users(users):
|
|||
import_ok = False
|
||||
except ScoValueError as value_error:
|
||||
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:
|
||||
for u in created.values():
|
||||
# Création de l'utilisateur (via SQLAlchemy)
|
||||
|
@ -245,7 +239,7 @@ def import_users(users):
|
|||
|
||||
|
||||
ALPHABET = r"""ABCDEFGHIJKLMNPQRSTUVWXYZ123456789123456789AEIOU"""
|
||||
PASSLEN = 6
|
||||
PASSLEN = 8
|
||||
RNG = random.Random(time.time())
|
||||
|
||||
|
||||
|
@ -260,23 +254,18 @@ def generate_password():
|
|||
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"
|
||||
if not u["email"]:
|
||||
if not user["email"]:
|
||||
return
|
||||
|
||||
u[
|
||||
"url"
|
||||
] = (
|
||||
scu.ScoURL()
|
||||
) # TODO set auth page URL ? (shared by all departments) ../auth/login
|
||||
|
||||
user["url"] = url_for("scodoc.index")
|
||||
txt = (
|
||||
"""
|
||||
Bonjour %(prenom)s %(nom)s,
|
||||
|
||||
"""
|
||||
% u
|
||||
% user
|
||||
)
|
||||
if reset:
|
||||
txt += (
|
||||
|
@ -286,10 +275,10 @@ votre mot de passe ScoDoc a été ré-initialisé.
|
|||
Le nouveau mot de passe est: %(passwd)s
|
||||
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
|
||||
"""
|
||||
% u
|
||||
% user
|
||||
)
|
||||
else:
|
||||
txt += (
|
||||
|
@ -304,13 +293,13 @@ Le logiciel est accessible sur: %(url)s
|
|||
Vous êtes invité à changer ce mot de passe au plus vite (cliquez sur
|
||||
votre nom en haut à gauche de la page d'accueil).
|
||||
"""
|
||||
% u
|
||||
% user
|
||||
)
|
||||
|
||||
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
|
||||
|
||||
"""
|
||||
|
@ -322,4 +311,4 @@ Pour plus d'informations sur ce logiciel, voir %s
|
|||
else:
|
||||
subject = "Votre accès ScoDoc"
|
||||
sender = sco_preferences.get_preference("email_from_addr")
|
||||
email.send_email(subject, sender, [u["email"]], txt)
|
||||
email.send_email(subject, sender, [user["email"]], txt)
|
||||
|
|
|
@ -401,12 +401,10 @@ def check_modif_user(
|
|||
returns (ok, msg)
|
||||
- ok : si vrai, peut continuer avec ces parametres
|
||||
(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
|
||||
"""
|
||||
if ajout_en_masse:
|
||||
MSG_OPT = (
|
||||
"""<br/>Impossible de forcer l'opération lors d'une importation en masse"""
|
||||
)
|
||||
MSG_OPT = """<br/>Impossible de forcer l'opération lors d'une importation en masse"""
|
||||
else:
|
||||
MSG_OPT = """<br/>Attention: (vous pouvez forcer l'opération en cochant "<em>Ignorer les avertissements</em>" en bas de page)"""
|
||||
# ce login existe ?
|
||||
|
@ -443,7 +441,6 @@ def check_modif_user(
|
|||
and Departement.query.filter_by(acronym=dept).first() is None
|
||||
):
|
||||
return False, "département '%s' inexistant" % dept + MSG_OPT
|
||||
|
||||
if ignore_optionals and not roles:
|
||||
return False, "aucun rôle sélectionné, êtes vous sûr ?" + MSG_OPT
|
||||
# ok
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# -*- mode: python -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
SCOVERSION = "9.0.52"
|
||||
SCOVERSION = "9.0.53"
|
||||
|
||||
SCONAME = "ScoDoc"
|
||||
|
||||
|
|
Loading…
Reference in New Issue