diff --git a/app/auth/models.py b/app/auth/models.py index 4b411c35..7b0b2052 100644 --- a/app/auth/models.py +++ b/app/auth/models.py @@ -243,6 +243,15 @@ class User(UserMixin, db.Model): self.user_name, ) + @staticmethod + def get_user_name_from_nomplogin(nomplogin): + """Returns user_name from the string "Dupont Pierre (dupont)" """ + m = re.match(r".*\((.*)\)", nomplogin.strip()) + if m: + return m.group(1) + else: + return None + class AnonymousUser(AnonymousUserMixin): def has_permission(self, perm, dept=None): diff --git a/app/scodoc/html_sco_header.py b/app/scodoc/html_sco_header.py index 991fa6fa..656a8e63 100644 --- a/app/scodoc/html_sco_header.py +++ b/app/scodoc/html_sco_header.py @@ -30,6 +30,8 @@ import cgi +from flask_login import current_user + import app.scodoc.sco_utils as scu from app.scodoc.notes_log import log from app.scodoc import html_sidebar @@ -314,18 +316,14 @@ def sco_header( # Avertissement si mot de passe à changer if user_check: - authuser = REQUEST.AUTHENTICATED_USER - # passwd_temp = context.Users.user_info(user_name=str(authuser))["passwd_temp"] - log("XXX TODO: Users.user_info") - passwd_temp = False # XXX TODO - if passwd_temp: + if current_user.passwd_temp: H.append( """
Responsable: | """, - context.Users.user_info(M["responsable_id"])["nomprenom"], + sco_users.user_info(M["responsable_id"])["nomprenom"], """(%(responsable_id)s)""" % M, ] try: @@ -210,7 +211,7 @@ def moduleimpl_status(context, moduleimpl_id=None, partition_id=None, REQUEST=No pass H.append(""" | """) H.append( - ", ".join([context.Users.user_info(m["ens_id"])["nomprenom"] for m in M["ens"]]) + ", ".join([sco_users.user_info(m["ens_id"])["nomprenom"] for m in M["ens"]]) ) H.append(""" | """)
try:
diff --git a/app/scodoc/sco_news.py b/app/scodoc/sco_news.py
index e99309c4..c9ffe606 100644
--- a/app/scodoc/sco_news.py
+++ b/app/scodoc/sco_news.py
@@ -45,6 +45,7 @@ from app.scodoc.sco_utils import SCO_ENCODING, SCO_ANNONCES_WEBSITE
from app.scodoc import sco_formsemestre
from app.scodoc import sco_moduleimpl
from app.scodoc import sco_preferences
+from app.scodoc import sco_users
_scolar_news_editor = ndb.EditableTable(
"scolar_news",
@@ -85,7 +86,7 @@ def add(context, REQUEST, typ, object=None, text="", url=None, max_frequency=Fal
cnx = ndb.GetDBConnexion()
args = {
"authenticated_user": authuser_name,
- "user_info": context.Users.user_info(user_name=authuser_name),
+ "user_info": sco_users.user_info(user_name=authuser_name),
"type": typ,
"object": object,
"text": text,
@@ -153,7 +154,7 @@ def scolar_news_summary(context, n=5):
)
n["text"] += (
" par "
- + context.Users.user_info(user_name=n["authenticated_user"])["nomcomplet"]
+ + sco_users.user_info(user_name=n["authenticated_user"])["nomcomplet"]
)
return news
diff --git a/app/scodoc/sco_recapcomplet.py b/app/scodoc/sco_recapcomplet.py
index 43a6f40c..db61c9f3 100644
--- a/app/scodoc/sco_recapcomplet.py
+++ b/app/scodoc/sco_recapcomplet.py
@@ -49,6 +49,7 @@ from app.scodoc import sco_permissions
from app.scodoc import sco_permissions_check
from app.scodoc import sco_preferences
from app.scodoc import sco_etud
+from app.scodoc import sco_users
from app.scodoc.sco_codes_parcours import DEF, UE_SPORT
@@ -621,7 +622,7 @@ def make_formsemestre_recapcomplet(
cls,
mod["moduleimpl_id"],
mod["module"]["titre"],
- context.Users.user_info(mod["responsable_id"])["nomcomplet"],
+ sco_users.user_info(mod["responsable_id"])["nomcomplet"],
F[0][i],
)
else:
diff --git a/app/scodoc/sco_undo_notes.py b/app/scodoc/sco_undo_notes.py
index 4ae468fa..3649120a 100644
--- a/app/scodoc/sco_undo_notes.py
+++ b/app/scodoc/sco_undo_notes.py
@@ -54,6 +54,7 @@ from app.scodoc import sco_evaluations
from app.scodoc import sco_formsemestre
from app.scodoc import sco_moduleimpl
from app.scodoc import sco_preferences
+from app.scodoc import sco_users
from app.scodoc import VERSION
from app.scodoc.gen_tables import GenTable
@@ -243,7 +244,7 @@ def get_note_history(context, evaluation_id, etudid, REQUEST=None, fmt=""):
# et cherche nom complet de l'enseignant:
for x in history:
x["comment"] = x["comment"] or ""
- x["user_name"] = context.Users.user_info(x["uid"])["nomcomplet"]
+ x["user_name"] = sco_users.user_info(x["uid"])["nomcomplet"]
if fmt == "json":
return scu.sendJSON(REQUEST, history)
diff --git a/app/scodoc/sco_users.py b/app/scodoc/sco_users.py
index 7a7173f0..fb6416d6 100644
--- a/app/scodoc/sco_users.py
+++ b/app/scodoc/sco_users.py
@@ -54,7 +54,6 @@ from app.scodoc import sco_excel
from app.scodoc import sco_preferences
from app.scodoc.gen_tables import GenTable
from app.scodoc.notes_log import log
-from app.scodoc.sco_permissions_check import can_handle_passwd
from app.scodoc.scolog import logdb
from app.scodoc.sco_etud import format_prenom, format_nom
from app.scodoc.TrivialFormulator import TrivialFormulator, tf_error_message
@@ -160,6 +159,8 @@ def list_users(
REQUEST=None,
):
"List users, returns a table in the specified format"
+ from app.scodoc.sco_permissions_check import can_handle_passwd
+
if dept and not all_depts:
users = get_user_list(dept=dept, with_inactives=with_inactives)
comm = "dept. %s" % dept.encode(scu.SCO_ENCODING) # sco8
@@ -325,6 +326,8 @@ def user_info_page(context, user_name=None, REQUEST=None):
"""Display page of info about given user.
If user_name not specified, user current_user
"""
+ from app.scodoc.sco_permissions_check import can_handle_passwd
+
# peut on divulguer ces infos ?
if not can_handle_passwd(current_user, allow_admindepts=True):
raise AccessDenied("Vous n'avez pas la permission de voir cette page")
diff --git a/app/views/notes.py b/app/views/notes.py
index 9fcae794..5aea9d88 100644
--- a/app/views/notes.py
+++ b/app/views/notes.py
@@ -41,6 +41,9 @@ from flask import current_app
from config import Config
import scodoc_manager
+
+from app.auth.models import User
+
from app.decorators import (
scodoc7func,
ScoDoc7Context,
@@ -779,7 +782,7 @@ def edit_enseignants_form(context, REQUEST, moduleimpl_id):
"moduleimpl_status?moduleimpl_id=" + moduleimpl_id
)
else:
- ens_id = context.Users.get_user_name_from_nomplogin(tf[2]["ens_id"])
+ ens_id = User.get_user_name_from_nomplogin(tf[2]["ens_id"])
if not ens_id:
H.append(
' Pour ajouter un enseignant, choisissez un nom dans le menu ' @@ -875,9 +878,7 @@ def edit_moduleimpl_resp(context, REQUEST, moduleimpl_id): "moduleimpl_status?moduleimpl_id=" + moduleimpl_id ) else: - responsable_id = context.Users.get_user_name_from_nomplogin( - tf[2]["responsable_id"] - ) + responsable_id = User.get_user_name_from_nomplogin(tf[2]["responsable_id"]) if ( not responsable_id ): # presque impossible: tf verifie les valeurs (mais qui peuvent changer entre temps) @@ -1194,7 +1195,7 @@ def formsemestre_enseignants_list(context, REQUEST, formsemestre_id, format="htm # ajoute infos sur enseignant: for ens in sem_ens: - sem_ens[ens].update(context.Users.user_info(ens)) + sem_ens[ens].update(sco_users.user_info(ens)) if sem_ens[ens]["email"]: sem_ens[ens]["_email_target"] = "mailto:%s" % sem_ens[ens]["email"] @@ -2566,5 +2567,3 @@ sco_publish( ) context.populate(globals()) - -context.Users = scodoc_manager.FakeUsers() diff --git a/app/views/scolar.py b/app/views/scolar.py index b5e36dfa..1eb53271 100644 --- a/app/views/scolar.py +++ b/app/views/scolar.py @@ -2006,5 +2006,3 @@ sco_publish( ) context.populate(globals()) - -context.Users = scodoc_manager.FakeUsers() diff --git a/app/views/users.py b/app/views/users.py index 5551629c..c7512aac 100644 --- a/app/views/users.py +++ b/app/views/users.py @@ -169,8 +169,6 @@ def create_user_form(context, REQUEST, user_name=None, edit=0): if displayed_roles_strings[i] not in editable_roles_strings: disabled_roles[i] = True - # stop() # XXX - descr = [ ("edit", {"input_type": "hidden", "default": edit}), ("nom", {"title": "Nom", "size": 20, "allow_null": False}), @@ -315,7 +313,6 @@ def create_user_form(context, REQUEST, user_name=None, edit=0): }, ), ] - # stop() # XXX if "tf-submitted" in REQUEST.form and not "roles" in REQUEST.form: REQUEST.form["roles"] = [] if "tf-submitted" in REQUEST.form: @@ -337,7 +334,7 @@ def create_user_form(context, REQUEST, user_name=None, edit=0): if tf[0] == 0: return "\n".join(H) + "\n" + tf[1] + F elif tf[0] == -1: - return REQUEST.RESPONSE.redirect(context.UsersURL()) + return REQUEST.RESPONSE.redirect(scu.UsersURL()) else: vals = tf[2] roles = set(vals["roles"]).intersection(editable_roles_strings) @@ -366,7 +363,6 @@ def create_user_form(context, REQUEST, user_name=None, edit=0): return "\n".join(H) + "\n" + tf[1] + F if not force: - # XXX x = stop() ok, msg = sco_users.check_modif_user( edit, user_name=user_name, diff --git a/scotests/sco_fake_gen.py b/scotests/sco_fake_gen.py index 0d297ae0..e312d310 100644 --- a/scotests/sco_fake_gen.py +++ b/scotests/sco_fake_gen.py @@ -433,4 +433,4 @@ class ScoFake: # band aid for #sco8 dev (temporaire en attendant Users) class Sco8Context(object): - Users = scodoc_manager.FakeUsers() + pass diff --git a/scotests/test_scenario1_app.py b/scotests/test_scenario1_app.py index 1d88e9b1..714c991e 100644 --- a/scotests/test_scenario1_app.py +++ b/scotests/test_scenario1_app.py @@ -1,8 +1,15 @@ +# -*- mode: python -*- +# -*- coding: utf-8 -*- + +# XXX A REVOIR POUR SCODOC8 + import random -# La variable context est définie par le script de lancement -# l'affecte ainsi pour évietr les warnins pylint: -context = context # pylint: disable=undefined-variable -REQUEST = REQUEST # pylint: disable=undefined-variable +from app import db +from app.auth.models import User +from app.auth.models import Role + +DEPT = "TEST" + import scotests.sco_fake_gen as sco_fake_gen # pylint: disable=import-error G = sco_fake_gen.ScoFake(context.Notes) @@ -11,10 +18,12 @@ G.verbose = False # --- Création d'étudiants etuds = [G.create_etud(code_nip=None) for _ in range(10)] -# --- Création de l'utilisateur responsable du semestres, des modules etc.... - -arg = {"passwd": "scodocpass", "user_name" : "unutil", "roles" :"Ens,RespPe"} -user = context.Users.create_user(args=arg, REQUEST=REQUEST) +# --- Création de l'utilisateur responsable du semestre, des modules etc.... +user = User(user_name="unutil") +user.set_password("scodocpass") +user.add_role(Role.get_named_role("Ens"), DEPT) +db.session.add(u) +db.session.commit() print(user) # --- Création formation et de deux UE @@ -75,9 +84,13 @@ for module_id, formsemestre_id, responsable_id in [ (mod11["module_id"], sem["formsemestre_id"], "bach"), (mod12["module_id"], sem["formsemestre_id"], "bach"), (mod21["module_id"], sem["formsemestre_id"], "bach"), - (mod22["module_id"], sem["formsemestre_id"], "bach") -] : - mi = G.create_moduleimpl(module_id=module_id, formsemestre_id=formsemestre_id, responsable_id=responsable_id) + (mod22["module_id"], sem["formsemestre_id"], "bach"), +]: + mi = G.create_moduleimpl( + module_id=module_id, + formsemestre_id=formsemestre_id, + responsable_id=responsable_id, + ) mods.append(mi) @@ -88,7 +101,7 @@ for etud in etuds: # --- Création d'évaluations (2 par modules) -evals=[] +evals = [] for moduleimpl_id, jour, description, coefficient in [ (mods[0]["moduleimpl_id"], "01/02/2021", "e1", 1.0), @@ -99,19 +112,20 @@ for moduleimpl_id, jour, description, coefficient in [ (mods[2]["moduleimpl_id"], "06/02/2021", "e6", 1.0), (mods[3]["moduleimpl_id"], "07/02/2021", "e7", 1.0), (mods[3]["moduleimpl_id"], "08/02/2021", "e8", 1.0), -] : - e = G.create_evaluation(moduleimpl_id=moduleimpl_id, jour=jour, description=description, coefficient=coefficient) +]: + e = G.create_evaluation( + moduleimpl_id=moduleimpl_id, + jour=jour, + description=description, + coefficient=coefficient, + ) evals.append(e) # --- Saisie des notes aléatoires -for eval in evals : +for eval in evals: for etud in etuds: nb_changed, nb_suppress, existing_decisions = G.create_note( evaluation=eval, etud=etud, note=float(random.randint(0, 20)) ) - - - - diff --git a/scotests/test_user.py b/scotests/test_user.py deleted file mode 100644 index eb803620..00000000 --- a/scotests/test_user.py +++ /dev/null @@ -1,75 +0,0 @@ -""" Créer un utilisateur, recupérer ses infos, changer son mdp, le supprimer """ - -import random - -# La variable context est définie par le script de lancement -# l'affecte ainsi pour évietr les warnins pylint: -context = context # pylint: disable=undefined-variable -REQUEST = REQUEST # pylint: disable=undefined-variable -import scotests.sco_fake_gen as sco_fake_gen # pylint: disable=import-error -import ZScoUsers -import ZScoDoc -import ZScolar - -nomdept = raw_input( - "Quel est le nom de votre département test? ATTENTION A NE PAS VOUS TROMPER : " -) - -# --- Création d'un utilisateur - -arg = { - "passwd": "scodocpass", - "user_name": "unutil", - "nom": "unnom", - "prenom": "unprenom", - "email": "unemail@mail.fr", - "roles": "Ens" + nomdept + ",RespPe" + nomdept, -} -# user1 = context.Users.create_user(args=arg, REQUEST=REQUEST) -user_info1 = context.Users.user_info(user_name="unutil") - -assert user_info1["email"] == arg["email"] -assert user_info1["user_name"] == arg["user_name"] -assert user_info1["nom"] == "unnom" -assert user_info1["prenom"] == "unprenom" -assert user_info1["roles"] == arg["roles"] - - -# --- Récupération de la liste des Users - -liste_xml = context.Users.get_user_list_xml(REQUEST=REQUEST) -print(liste_xml) - -liste_user = context.Users.get_userlist() -len_liu1 = len(liste_user) - -assert user_info1 in liste_user # le nouvel utilisateur est bien dans la liste ! - -# --- Récupérer user_name à partir de nomplogin - -user_name1 = context.Users.get_user_name_from_nomplogin( - nomplogin=user_info1["nomplogin"] -) -assert user_name1 == "unutil" - -# --- Changement du mot de passe de l'utilisateur - -context.Users.do_change_password(user_name="unutil", password="scodocpass2") -# vérification du mdp changé directement sur scodoc web car je ne trouve pas comment récupérer le mdp en python -# Vérification : ok! - -# --- Supression d'un utilisateur - -# context.Users.delete_user_form(REQUEST=REQUEST, user_name="unutil") - -# --- Test de la récupération de l'URL - -url = context.Users.UsersURL() -assert url == "ScoDoc/" + nomdept + "/Scolarite/Users" - -""" -Commentaire : - -La supression d'un utilisateur ne fonctionne pas car nécessite l'envoie d'un formulaire. - -""" \ No newline at end of file |