From b3e1d97f94aaaebc2a0968fd85cd81271357d13b Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Fri, 27 Jan 2023 10:20:52 -0300 Subject: [PATCH] Modernise code (evals) --- app/scodoc/sco_evaluation_db.py | 72 ++++++++++++++--------------- app/scodoc/sco_moduleimpl_status.py | 6 +-- app/views/notes.py | 8 ++-- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/app/scodoc/sco_evaluation_db.py b/app/scodoc/sco_evaluation_db.py index 7d00b2087..b99af2cc3 100644 --- a/app/scodoc/sco_evaluation_db.py +++ b/app/scodoc/sco_evaluation_db.py @@ -25,7 +25,7 @@ # ############################################################################## -"""Gestion evaluations (ScoDoc7, sans SQlAlchemy) +"""Gestion évaluations (ScoDoc7, code en voie de modernisation) """ import pprint @@ -34,16 +34,15 @@ import flask from flask import url_for, g from flask_login import current_user -from app import log +from app import db, log -from app.models import ModuleImpl, ScolarNews +from app.models import Evaluation, ModuleImpl, ScolarNews from app.models.evaluations import evaluation_enrich_dict, check_evaluation_args import app.scodoc.sco_utils as scu import app.scodoc.notesdb as ndb from app.scodoc.sco_exceptions import AccessDenied, ScoValueError from app.scodoc import sco_cache -from app.scodoc import sco_edit_module from app.scodoc import sco_moduleimpl from app.scodoc import sco_permissions_check @@ -135,7 +134,7 @@ def do_evaluation_create( raise ValueError("module not found") check_evaluation_args(args) # Check numeros - module_evaluation_renumber(moduleimpl_id, only_if_unumbered=True) + moduleimpl_evaluation_renumber(moduleimpl_id, only_if_unumbered=True) if not "numero" in args or args["numero"] is None: n = None # determine le numero avec la date @@ -158,7 +157,7 @@ def do_evaluation_create( next_eval = e break if next_eval: - n = module_evaluation_insert_before(mod_evals, next_eval) + n = moduleimpl_evaluation_insert_before(mod_evals, next_eval) else: n = None # a placer en fin if n is None: # pas de date ou en fin: @@ -215,13 +214,11 @@ def do_evaluation_edit(args): def do_evaluation_delete(evaluation_id): "delete evaluation" - the_evals = do_evaluation_list({"evaluation_id": evaluation_id}) - if not the_evals: - raise ValueError("evaluation inexistante !") - moduleimpl_id = the_evals[0]["moduleimpl_id"] - if not sco_permissions_check.can_edit_evaluation(moduleimpl_id=moduleimpl_id): + evaluation: Evaluation = Evaluation.query.get_or_404(evaluation_id) + modimpl: ModuleImpl = evaluation.moduleimpl + if not sco_permissions_check.can_edit_evaluation(moduleimpl_id=modimpl.id): raise AccessDenied( - "Modification évaluation impossible pour %s" % current_user.get_nomplogin() + f"Modification évaluation impossible pour {current_user.get_nomplogin()}" ) notes_db = do_evaluation_get_all_notes(evaluation_id) # { etudid : value } notes = [x["value"] for x in notes_db.values()] @@ -230,24 +227,24 @@ def do_evaluation_delete(evaluation_id): "Impossible de supprimer cette évaluation: il reste des notes" ) - cnx = ndb.GetDBConnexion() + db.session.delete(evaluation) + db.session.commit() - _evaluationEditor.delete(cnx, evaluation_id) # inval cache pour ce semestre - M = sco_moduleimpl.moduleimpl_list(moduleimpl_id=moduleimpl_id)[0] - sco_cache.invalidate_formsemestre(formsemestre_id=M["formsemestre_id"]) + sco_cache.invalidate_formsemestre(formsemestre_id=modimpl.formsemestre_id) # news - - mod = sco_edit_module.module_list(args={"module_id": M["module_id"]})[0] - mod["moduleimpl_id"] = M["moduleimpl_id"] - mod["url"] = ( - scu.NotesURL() + "/moduleimpl_status?moduleimpl_id=%(moduleimpl_id)s" % mod + url = url_for( + "notes.moduleimpl_status", + scodoc_dept=g.scodoc_dept, + moduleimpl_id=modimpl.id, ) ScolarNews.add( typ=ScolarNews.NEWS_NOTE, - obj=moduleimpl_id, - text='Suppression d\'une évaluation dans %(titre)s' % mod, - url=mod["url"], + obj=modimpl.id, + text=f"""Suppression d'une évaluation dans {modimpl.module.titre}""", + url=url, ) @@ -263,7 +260,7 @@ def do_evaluation_get_all_notes( ) # pas de cache pour (rares) appels via undo_notes ou specifiant un enseignant if do_cache: r = sco_cache.EvaluationCache.get(evaluation_id) - if r != None: + if r is not None: return r cnx = ndb.GetDBConnexion() cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor) @@ -291,13 +288,13 @@ def do_evaluation_get_all_notes( return d -def module_evaluation_renumber(moduleimpl_id, only_if_unumbered=False, redirect=0): +def moduleimpl_evaluation_renumber(moduleimpl_id, only_if_unumbered=False, redirect=0): """Renumber evaluations in this module, according to their date. (numero=0: oldest one) Needed because previous versions of ScoDoc did not have eval numeros Note: existing numeros are ignored """ redirect = int(redirect) - # log('module_evaluation_renumber( moduleimpl_id=%s )' % moduleimpl_id ) + # log('moduleimpl_evaluation_renumber( moduleimpl_id=%s )' % moduleimpl_id ) # List sorted according to date/heure, ignoring numeros: # (note that we place evaluations with NULL date at the end) mod_evals = do_evaluation_list( @@ -327,7 +324,7 @@ def module_evaluation_renumber(moduleimpl_id, only_if_unumbered=False, redirect= ) -def module_evaluation_insert_before(mod_evals, next_eval): +def moduleimpl_evaluation_insert_before(mod_evals, next_eval): """Renumber evals such that an evaluation with can be inserted before next_eval Returns numero suitable for the inserted evaluation """ @@ -335,7 +332,7 @@ def module_evaluation_insert_before(mod_evals, next_eval): n = next_eval["numero"] if not n: log("renumbering old evals") - module_evaluation_renumber(next_eval["moduleimpl_id"]) + moduleimpl_evaluation_renumber(next_eval["moduleimpl_id"]) next_eval = do_evaluation_list( args={"evaluation_id": next_eval["evaluation_id"]} )[0] @@ -353,19 +350,20 @@ def module_evaluation_insert_before(mod_evals, next_eval): return n -def module_evaluation_move(evaluation_id, after=0, redirect=1): +def moduleimpl_evaluation_move(evaluation_id: int, after=0, redirect=1): """Move before/after previous one (decrement/increment numero) (published) """ - e = do_evaluation_list(args={"evaluation_id": evaluation_id})[0] + evaluation: Evaluation = Evaluation.query.get_or_404(evaluation_id) + moduleimpl_id = evaluation.moduleimpl_id redirect = int(redirect) # access: can change eval ? - if not sco_permissions_check.can_edit_evaluation(moduleimpl_id=e["moduleimpl_id"]): + if not sco_permissions_check.can_edit_evaluation(moduleimpl_id=moduleimpl_id): raise AccessDenied( - "Modification évaluation impossible pour %s" % current_user.get_nomplogin() + f"Modification évaluation impossible pour {current_user.get_nomplogin()}" ) - module_evaluation_renumber(e["moduleimpl_id"], only_if_unumbered=True) + moduleimpl_evaluation_renumber(moduleimpl_id, only_if_unumbered=True) e = do_evaluation_list(args={"evaluation_id": evaluation_id})[0] after = int(after) # 0: deplace avant, 1 deplace apres @@ -381,8 +379,10 @@ def module_evaluation_move(evaluation_id, after=0, redirect=1): neigh = mod_evals[idx + 1] if neigh: # if neigh["numero"] == e["numero"]: - log("Warning: module_evaluation_move: forcing renumber") - module_evaluation_renumber(e["moduleimpl_id"], only_if_unumbered=False) + log("Warning: moduleimpl_evaluation_move: forcing renumber") + moduleimpl_evaluation_renumber( + e["moduleimpl_id"], only_if_unumbered=False + ) else: # swap numero with neighbor e["numero"], neigh["numero"] = neigh["numero"], e["numero"] diff --git a/app/scodoc/sco_moduleimpl_status.py b/app/scodoc/sco_moduleimpl_status.py index 9b92a7dfe..5bf2a7f83 100644 --- a/app/scodoc/sco_moduleimpl_status.py +++ b/app/scodoc/sco_moduleimpl_status.py @@ -433,7 +433,7 @@ def moduleimpl_status(moduleimpl_id=None, partition_id=None): if nb_evaluations > 0: top_table_links += f""" Trier par date """ @@ -601,7 +601,7 @@ def _ligne_evaluation( # Fleches: if eval_index != (nb_evals - 1) and can_edit_evals: H.append( - f"""{arrow_up}""" ) @@ -609,7 +609,7 @@ def _ligne_evaluation( H.append(arrow_none) if (eval_index > 0) and can_edit_evals: H.append( - f"""{arrow_down}""" ) diff --git a/app/views/notes.py b/app/views/notes.py index da26f9b27..16bc02611 100644 --- a/app/views/notes.py +++ b/app/views/notes.py @@ -367,13 +367,13 @@ sco_publish( Permission.ScoView, ) sco_publish( - "/module_evaluation_renumber", - sco_evaluation_db.module_evaluation_renumber, + "/moduleimpl_evaluation_renumber", + sco_evaluation_db.moduleimpl_evaluation_renumber, Permission.ScoView, ) sco_publish( - "/module_evaluation_move", - sco_evaluation_db.module_evaluation_move, + "/moduleimpl_evaluation_move", + sco_evaluation_db.moduleimpl_evaluation_move, Permission.ScoView, ) sco_publish(