From 83f522f08cb0e06ad8f00719cdb09183615f2589 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 30 Nov 2021 10:54:12 +0100 Subject: [PATCH] Fix: cascade on ue coefs --- app/models/formations.py | 42 ++++++++++-- ...6b3103697d_fix_cascades_on_moduleuecoef.py | 64 +++++++++++++++++++ 2 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 migrations/versions/a26b3103697d_fix_cascades_on_moduleuecoef.py diff --git a/app/models/formations.py b/app/models/formations.py index 942e1a9bc..45f42d29b 100644 --- a/app/models/formations.py +++ b/app/models/formations.py @@ -43,6 +43,13 @@ class Formation(db.Model): def __repr__(self): return f"<{self.__class__.__name__}(id={self.id}, dept_id={self.dept_id}, acronyme='{self.acronyme}')>" + def to_dict(self): + e = dict(self.__dict__) + e.pop("_sa_instance_state", None) + # ScoDoc7 output_formators: (backward compat) + e["formation_id"] = self.id + return e + def get_parcours(self): """get l'instance de TypeParcours de cette formation""" return sco_codes_parcours.get_parcours_from_code(self.type_parcours) @@ -215,6 +222,19 @@ class Module(db.Model): f"" ) + def to_dict(self): + e = dict(self.__dict__) + e.pop("_sa_instance_state", None) + # ScoDoc7 output_formators: (backward compat) + e["module_id"] = self.id + e["heures_cours"] = 0.0 if self.heures_cours is None else self.heures_cours + e["heures_td"] = 0.0 if self.heures_td is None else self.heures_td + e["heures_tp"] = 0.0 if self.heures_tp is None else self.heures_tp + e["numero"] = 0 if self.numero is None else self.numero + e["coefficient"] = 0.0 if self.coefficient is None else self.coefficient + e["module_type"] = 0 if self.module_type is None else self.module_type + return e + def is_apc(self): "True si module SAÉ ou Ressource" return scu.ModuleType(self.module_type) in { @@ -274,20 +294,34 @@ class ModuleUECoef(db.Model): __tablename__ = "module_ue_coef" module_id = db.Column( - db.Integer, db.ForeignKey("notes_modules.id"), primary_key=True + db.Integer, + db.ForeignKey("notes_modules.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) coef = db.Column( db.Float, nullable=False, ) module = db.relationship( Module, - backref=db.backref("ue_coefs", cascade="all, delete-orphan"), + backref=db.backref( + "ue_coefs", + passive_deletes=True, + cascade="save-update, merge, delete, delete-orphan", + ), ) ue = db.relationship( UniteEns, - backref=db.backref("module_ue_coefs", cascade="all, delete-orphan"), + backref=db.backref( + "module_ue_coefs", + passive_deletes=True, + cascade="save-update, merge, delete, delete-orphan", + ), ) diff --git a/migrations/versions/a26b3103697d_fix_cascades_on_moduleuecoef.py b/migrations/versions/a26b3103697d_fix_cascades_on_moduleuecoef.py new file mode 100644 index 000000000..38d4d497d --- /dev/null +++ b/migrations/versions/a26b3103697d_fix_cascades_on_moduleuecoef.py @@ -0,0 +1,64 @@ +"""fix cascades on ModuleUECoef + +Revision ID: a26b3103697d +Revises: c8efc54586d8 +Create Date: 2021-11-30 10:47:23.465897 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "a26b3103697d" +down_revision = "c8efc54586d8" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint( + "module_ue_coef_module_id_fkey", "module_ue_coef", type_="foreignkey" + ) + op.drop_constraint( + "module_ue_coef_ue_id_fkey", "module_ue_coef", type_="foreignkey" + ) + op.create_foreign_key( + "ev_module_ue_coef_ue_id_fkey", + "module_ue_coef", + "notes_modules", + ["module_id"], + ["id"], + ondelete="CASCADE", + ) + op.create_foreign_key( + "ev_module_ue_coef_module_id_fkey", + "module_ue_coef", + "notes_ue", + ["ue_id"], + ["id"], + ondelete="CASCADE", + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint( + "ev_module_ue_coef_ue_id_fkey", "module_ue_coef", type_="foreignkey" + ) + op.drop_constraint( + "ev_module_ue_coef_module_id_fkey", "module_ue_coef", type_="foreignkey" + ) + op.create_foreign_key( + "module_ue_coef_ue_id_fkey", "module_ue_coef", "notes_ue", ["ue_id"], ["id"] + ) + op.create_foreign_key( + "module_ue_coef_module_id_fkey", + "module_ue_coef", + "notes_modules", + ["module_id"], + ["id"], + ) + # ### end Alembic commands ###