Fix: cascade on ue coefs

This commit is contained in:
Emmanuel Viennet 2021-11-30 10:54:12 +01:00
parent 47e752c95c
commit 83f522f08c
2 changed files with 102 additions and 4 deletions

View File

@ -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"<Module{ModuleType(self.module_type).name} id={self.id} code={self.code}>"
)
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",
),
)

View File

@ -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 ###