From 5e492dc145c480b4f86ca630d7b7bd37462be40b Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 13 Dec 2021 23:44:13 +0100 Subject: [PATCH] Fixes: cascades et invalidations de caches --- app/comp/moy_mod.py | 2 + app/models/evaluations.py | 10 ++- app/scodoc/sco_edit_ue.py | 13 +++- .../a57a6ee2e3cb_cascade_evaluation_poids.py | 72 +++++++++++++++++++ 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 migrations/versions/a57a6ee2e3cb_cascade_evaluation_poids.py diff --git a/app/comp/moy_mod.py b/app/comp/moy_mod.py index 24f941be6..10da10e21 100644 --- a/app/comp/moy_mod.py +++ b/app/comp/moy_mod.py @@ -78,6 +78,8 @@ def check_moduleimpl_conformity( return True # modules vides conformes if nb_ues == 0: return False # situation absurde (pas d'UE) + if len(modules_coefficients) != nb_ues: + raise ValueError("check_moduleimpl_conformity: nb ue incoherent") module_evals_poids = evals_poids.transpose().sum(axis=1).to_numpy() != 0 check = all( (modules_coefficients[moduleimpl.module.id].to_numpy() != 0) diff --git a/app/models/evaluations.py b/app/models/evaluations.py index ae91493fd..1870607a7 100644 --- a/app/models/evaluations.py +++ b/app/models/evaluations.py @@ -107,9 +107,15 @@ class EvaluationUEPoids(db.Model): """ evaluation_id = db.Column( - db.Integer, db.ForeignKey("notes_evaluation.id"), primary_key=True + db.Integer, + db.ForeignKey("notes_evaluation.id", ondelete="CASCADE"), + primary_key=True, + ) + ue_id = db.Column( + db.Integer, + db.ForeignKey("notes_ue.id", ondelete="CASCADE"), + primary_key=True, ) - ue_id = db.Column(db.Integer, db.ForeignKey("notes_ue.id"), primary_key=True) poids = db.Column( db.Float, nullable=False, diff --git a/app/scodoc/sco_edit_ue.py b/app/scodoc/sco_edit_ue.py index 5a6b347b6..6e05fc4a9 100644 --- a/app/scodoc/sco_edit_ue.py +++ b/app/scodoc/sco_edit_ue.py @@ -33,7 +33,7 @@ from flask import url_for, render_template from flask import g, request from flask_login import current_user -from app.models import Formation, UniteEns +from app.models import Formation, UniteEns, ModuleImpl, Module import app.scodoc.notesdb as ndb import app.scodoc.sco_utils as scu from app.scodoc.sco_utils import ModuleType @@ -106,8 +106,15 @@ def do_ue_create(args): if ues: raise ScoValueError('Acronyme d\'UE "%s" déjà utilisé !' % args["acronyme"]) # create - r = _ueEditor.create(cnx, args) + ue_id = _ueEditor.create(cnx, args) + # Invalidate cache: vire les poids de toutes les évals de la formation + for modimpl in ModuleImpl.query.filter( + ModuleImpl.module_id == Module.id, Module.formation_id == args["formation_id"] + ): + modimpl.invalidate_evaluations_poids() + formation = Formation.query.get(args["formation_id"]) + formation.invalidate_module_coefs() # news F = sco_formations.formation_list(args={"formation_id": args["formation_id"]})[0] sco_news.add( @@ -116,7 +123,7 @@ def do_ue_create(args): text="Modification de la formation %(acronyme)s" % F, max_frequency=3, ) - return r + return ue_id def do_ue_delete(ue_id, delete_validations=False, force=False): diff --git a/migrations/versions/a57a6ee2e3cb_cascade_evaluation_poids.py b/migrations/versions/a57a6ee2e3cb_cascade_evaluation_poids.py new file mode 100644 index 000000000..33532693b --- /dev/null +++ b/migrations/versions/a57a6ee2e3cb_cascade_evaluation_poids.py @@ -0,0 +1,72 @@ +"""cascade evaluation/poids + +Revision ID: a57a6ee2e3cb +Revises: 730ac71eea23 +Create Date: 2021-12-13 23:39:37.586952 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "a57a6ee2e3cb" +down_revision = "730ac71eea23" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint( + "evaluation_ue_poids_ue_id_fkey", "evaluation_ue_poids", type_="foreignkey" + ) + op.drop_constraint( + "evaluation_ue_poids_evaluation_id_fkey", + "evaluation_ue_poids", + type_="foreignkey", + ) + op.create_foreign_key( + "evaluation_ue_poids_evaluation_id_fkey", + "evaluation_ue_poids", + "notes_evaluation", + ["evaluation_id"], + ["id"], + ondelete="CASCADE", + ) + op.create_foreign_key( + "evaluation_ue_poids_ue_id_fkey", + "evaluation_ue_poids", + "notes_ue", + ["ue_id"], + ["id"], + ondelete="CASCADE", + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint( + "evaluation_ue_poids_evaluation_id_fkey", + "evaluation_ue_poids", + type_="foreignkey", + ) + op.drop_constraint( + "evaluation_ue_poids_ue_id_fkey", "evaluation_ue_poids", type_="foreignkey" + ) + op.create_foreign_key( + "evaluation_ue_poids_evaluation_id_fkey", + "evaluation_ue_poids", + "notes_evaluation", + ["evaluation_id"], + ["id"], + ) + op.create_foreign_key( + "evaluation_ue_poids_ue_id_fkey", + "evaluation_ue_poids", + "notes_ue", + ["ue_id"], + ["id"], + ) + # ### end Alembic commands ###