1
0
forked from ScoDoc/ScoDoc

Modernise code (evals)

This commit is contained in:
Emmanuel Viennet 2023-01-27 10:20:52 -03:00
parent b716863bb8
commit b3e1d97f94
3 changed files with 43 additions and 43 deletions

View File

@ -25,7 +25,7 @@
# #
############################################################################## ##############################################################################
"""Gestion evaluations (ScoDoc7, sans SQlAlchemy) """Gestion évaluations (ScoDoc7, code en voie de modernisation)
""" """
import pprint import pprint
@ -34,16 +34,15 @@ import flask
from flask import url_for, g from flask import url_for, g
from flask_login import current_user 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 from app.models.evaluations import evaluation_enrich_dict, check_evaluation_args
import app.scodoc.sco_utils as scu import app.scodoc.sco_utils as scu
import app.scodoc.notesdb as ndb import app.scodoc.notesdb as ndb
from app.scodoc.sco_exceptions import AccessDenied, ScoValueError from app.scodoc.sco_exceptions import AccessDenied, ScoValueError
from app.scodoc import sco_cache from app.scodoc import sco_cache
from app.scodoc import sco_edit_module
from app.scodoc import sco_moduleimpl from app.scodoc import sco_moduleimpl
from app.scodoc import sco_permissions_check from app.scodoc import sco_permissions_check
@ -135,7 +134,7 @@ def do_evaluation_create(
raise ValueError("module not found") raise ValueError("module not found")
check_evaluation_args(args) check_evaluation_args(args)
# Check numeros # 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: if not "numero" in args or args["numero"] is None:
n = None n = None
# determine le numero avec la date # determine le numero avec la date
@ -158,7 +157,7 @@ def do_evaluation_create(
next_eval = e next_eval = e
break break
if next_eval: if next_eval:
n = module_evaluation_insert_before(mod_evals, next_eval) n = moduleimpl_evaluation_insert_before(mod_evals, next_eval)
else: else:
n = None # a placer en fin n = None # a placer en fin
if n is None: # pas de date ou 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): def do_evaluation_delete(evaluation_id):
"delete evaluation" "delete evaluation"
the_evals = do_evaluation_list({"evaluation_id": evaluation_id}) evaluation: Evaluation = Evaluation.query.get_or_404(evaluation_id)
if not the_evals: modimpl: ModuleImpl = evaluation.moduleimpl
raise ValueError("evaluation inexistante !") if not sco_permissions_check.can_edit_evaluation(moduleimpl_id=modimpl.id):
moduleimpl_id = the_evals[0]["moduleimpl_id"]
if not sco_permissions_check.can_edit_evaluation(moduleimpl_id=moduleimpl_id):
raise AccessDenied( 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_db = do_evaluation_get_all_notes(evaluation_id) # { etudid : value }
notes = [x["value"] for x in notes_db.values()] 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" "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 # inval cache pour ce semestre
M = sco_moduleimpl.moduleimpl_list(moduleimpl_id=moduleimpl_id)[0] sco_cache.invalidate_formsemestre(formsemestre_id=modimpl.formsemestre_id)
sco_cache.invalidate_formsemestre(formsemestre_id=M["formsemestre_id"])
# news # news
url = url_for(
mod = sco_edit_module.module_list(args={"module_id": M["module_id"]})[0] "notes.moduleimpl_status",
mod["moduleimpl_id"] = M["moduleimpl_id"] scodoc_dept=g.scodoc_dept,
mod["url"] = ( moduleimpl_id=modimpl.id,
scu.NotesURL() + "/moduleimpl_status?moduleimpl_id=%(moduleimpl_id)s" % mod
) )
ScolarNews.add( ScolarNews.add(
typ=ScolarNews.NEWS_NOTE, typ=ScolarNews.NEWS_NOTE,
obj=moduleimpl_id, obj=modimpl.id,
text='Suppression d\'une évaluation dans <a href="%(url)s">%(titre)s</a>' % mod, text=f"""Suppression d'une évaluation dans <a href="{
url=mod["url"], url
}">{modimpl.module.titre}</a>""",
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 ) # pas de cache pour (rares) appels via undo_notes ou specifiant un enseignant
if do_cache: if do_cache:
r = sco_cache.EvaluationCache.get(evaluation_id) r = sco_cache.EvaluationCache.get(evaluation_id)
if r != None: if r is not None:
return r return r
cnx = ndb.GetDBConnexion() cnx = ndb.GetDBConnexion()
cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor) cursor = cnx.cursor(cursor_factory=ndb.ScoDocCursor)
@ -291,13 +288,13 @@ def do_evaluation_get_all_notes(
return d 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) """Renumber evaluations in this module, according to their date. (numero=0: oldest one)
Needed because previous versions of ScoDoc did not have eval numeros Needed because previous versions of ScoDoc did not have eval numeros
Note: existing numeros are ignored Note: existing numeros are ignored
""" """
redirect = int(redirect) 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: # List sorted according to date/heure, ignoring numeros:
# (note that we place evaluations with NULL date at the end) # (note that we place evaluations with NULL date at the end)
mod_evals = do_evaluation_list( 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 """Renumber evals such that an evaluation with can be inserted before next_eval
Returns numero suitable for the inserted evaluation Returns numero suitable for the inserted evaluation
""" """
@ -335,7 +332,7 @@ def module_evaluation_insert_before(mod_evals, next_eval):
n = next_eval["numero"] n = next_eval["numero"]
if not n: if not n:
log("renumbering old evals") log("renumbering old evals")
module_evaluation_renumber(next_eval["moduleimpl_id"]) moduleimpl_evaluation_renumber(next_eval["moduleimpl_id"])
next_eval = do_evaluation_list( next_eval = do_evaluation_list(
args={"evaluation_id": next_eval["evaluation_id"]} args={"evaluation_id": next_eval["evaluation_id"]}
)[0] )[0]
@ -353,19 +350,20 @@ def module_evaluation_insert_before(mod_evals, next_eval):
return n 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) """Move before/after previous one (decrement/increment numero)
(published) (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) redirect = int(redirect)
# access: can change eval ? # 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( 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] e = do_evaluation_list(args={"evaluation_id": evaluation_id})[0]
after = int(after) # 0: deplace avant, 1 deplace apres 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] neigh = mod_evals[idx + 1]
if neigh: # if neigh: #
if neigh["numero"] == e["numero"]: if neigh["numero"] == e["numero"]:
log("Warning: module_evaluation_move: forcing renumber") log("Warning: moduleimpl_evaluation_move: forcing renumber")
module_evaluation_renumber(e["moduleimpl_id"], only_if_unumbered=False) moduleimpl_evaluation_renumber(
e["moduleimpl_id"], only_if_unumbered=False
)
else: else:
# swap numero with neighbor # swap numero with neighbor
e["numero"], neigh["numero"] = neigh["numero"], e["numero"] e["numero"], neigh["numero"] = neigh["numero"], e["numero"]

View File

@ -433,7 +433,7 @@ def moduleimpl_status(moduleimpl_id=None, partition_id=None):
if nb_evaluations > 0: if nb_evaluations > 0:
top_table_links += f""" top_table_links += f"""
<a class="stdlink" style="margin-left:2em;" href="{ <a class="stdlink" style="margin-left:2em;" href="{
url_for("notes.module_evaluation_renumber", scodoc_dept=g.scodoc_dept, moduleimpl_id=M['moduleimpl_id'], url_for("notes.moduleimpl_evaluation_renumber", scodoc_dept=g.scodoc_dept, moduleimpl_id=M['moduleimpl_id'],
redirect=1) redirect=1)
}">Trier par date</a> }">Trier par date</a>
""" """
@ -601,7 +601,7 @@ def _ligne_evaluation(
# Fleches: # Fleches:
if eval_index != (nb_evals - 1) and can_edit_evals: if eval_index != (nb_evals - 1) and can_edit_evals:
H.append( H.append(
f"""<a href="{url_for("notes.module_evaluation_move", f"""<a href="{url_for("notes.moduleimpl_evaluation_move",
scodoc_dept=g.scodoc_dept, evaluation_id=evaluation.id, after=0) scodoc_dept=g.scodoc_dept, evaluation_id=evaluation.id, after=0)
}" class="aud">{arrow_up}</a>""" }" class="aud">{arrow_up}</a>"""
) )
@ -609,7 +609,7 @@ def _ligne_evaluation(
H.append(arrow_none) H.append(arrow_none)
if (eval_index > 0) and can_edit_evals: if (eval_index > 0) and can_edit_evals:
H.append( H.append(
f"""<a href="{url_for("notes.module_evaluation_move", f"""<a href="{url_for("notes.moduleimpl_evaluation_move",
scodoc_dept=g.scodoc_dept, evaluation_id=evaluation.id, after=1) scodoc_dept=g.scodoc_dept, evaluation_id=evaluation.id, after=1)
}" class="aud">{arrow_down}</a>""" }" class="aud">{arrow_down}</a>"""
) )

View File

@ -367,13 +367,13 @@ sco_publish(
Permission.ScoView, Permission.ScoView,
) )
sco_publish( sco_publish(
"/module_evaluation_renumber", "/moduleimpl_evaluation_renumber",
sco_evaluation_db.module_evaluation_renumber, sco_evaluation_db.moduleimpl_evaluation_renumber,
Permission.ScoView, Permission.ScoView,
) )
sco_publish( sco_publish(
"/module_evaluation_move", "/moduleimpl_evaluation_move",
sco_evaluation_db.module_evaluation_move, sco_evaluation_db.moduleimpl_evaluation_move,
Permission.ScoView, Permission.ScoView,
) )
sco_publish( sco_publish(