commande de suppression de département (avec JMP)

This commit is contained in:
Emmanuel Viennet 2021-08-19 21:30:22 +02:00
parent fb6f8b7db6
commit 98ce5de732
2 changed files with 93 additions and 2 deletions

View File

@ -35,6 +35,7 @@ import app.scodoc.sco_utils as scu
from app.scodoc.gen_tables import GenTable
from app.scodoc.sco_permissions import Permission
from app.scodoc import html_sco_header
import app.scodoc.notesdb as ndb
from app.scodoc import sco_formsemestre
from app.scodoc import sco_formsemestre_inscriptions
from app.scodoc import sco_modalites
@ -274,3 +275,88 @@ def _style_sems(context, sems):
sem["semestre_id_n"] = ""
else:
sem["semestre_id_n"] = sem["semestre_id"]
def delete_dept(dept_id: int):
"""Supression irréversible d'un département et de tous les objets rattachés"""
assert isinstance(dept_id, int)
# Un peu complexe, merci JMP :)
cnx = ndb.GetDBConnexion()
cursor = cnx.cursor()
try:
# 1- Create temp tables to store ids
reqs = [
"create temp table etudids_temp as select id from identite where dept_id = %(dept_id)s",
"create temp table formsemestres_temp as select id from notes_formsemestre where dept_id = %(dept_id)s",
"create temp table moduleimpls_temp as select id from notes_moduleimpl where formsemestre_id in (select id from formsemestres_temp)",
"create temp table formations_temp as select id from notes_formations where dept_id = %(dept_id)s",
"create temp table entreprises_temp as select id from entreprises where dept_id = %(dept_id)s",
"create temp table tags_temp as select id from notes_tags where dept_id = %(dept_id)s",
]
for r in reqs:
cursor.execute(r, {"dept_id": dept_id})
# 2- Delete student-related informations
# ordered list of tables
etud_tables = [
"notes_notes",
"group_membership",
"admissions",
"billet_absence",
"adresse",
"absences",
"notes_notes_log",
"notes_moduleimpl_inscription",
"itemsuivi",
"notes_appreciations",
"scolar_autorisation_inscription",
"absences_notifications",
"notes_formsemestre_inscription",
"scolar_formsemestre_validation",
"scolar_events",
]
for table in etud_tables:
cursor.execute(
f"delete from {table} where etudid in (select id from etudids_temp)"
)
reqs = [
"delete from identite where dept_id = %(dept_id)s",
"delete from sco_prefs where dept_id = %(dept_id)s",
"delete from notes_semset_formsemestre where formsemestre_id in (select id from formsemestres_temp)",
"delete from notes_evaluation where moduleimpl_id in (select id from moduleimpls_temp)",
"delete from notes_modules_enseignants where moduleimpl_id in (select id from moduleimpls_temp)",
"delete from notes_formsemestre_uecoef where formsemestre_id in (select id from formsemestres_temp)",
"delete from notes_formsemestre_ue_computation_expr where formsemestre_id in (select id from formsemestres_temp)",
"delete from notes_formsemestre_responsables where formsemestre_id in (select id from formsemestres_temp)",
"delete from notes_moduleimpl where formsemestre_id in (select id from formsemestres_temp)",
"delete from notes_modules_tags where tag_id in (select id from tags_temp)",
"delete from notes_tags where dept_id = %(dept_id)s",
"delete from notes_modules where formation_id in (select id from formations_temp)",
"delete from notes_matieres where ue_id in (select id from notes_ue where formation_id in (select id from formations_temp))",
"delete from notes_formsemestre_etapes where formsemestre_id in (select id from formsemestres_temp)",
"delete from group_descr where partition_id in (select id from partition where formsemestre_id in (select id from formsemestres_temp))",
"delete from partition where formsemestre_id in (select id from formsemestres_temp)",
"delete from notes_formsemestre_custommenu where formsemestre_id in (select id from formsemestres_temp)",
"delete from notes_ue where formation_id in (select id from formations_temp)",
"delete from notes_formsemestre where dept_id = %(dept_id)s",
"delete from scolar_news where dept_id = %(dept_id)s",
"delete from notes_semset where dept_id = %(dept_id)s",
"delete from entreprise_contact where entreprise_id in (select id from entreprises_temp) ",
"delete from entreprise_correspondant where entreprise_id in (select id from entreprises_temp) ",
"delete from entreprises where dept_id = %(dept_id)s",
"delete from notes_formations where dept_id = %(dept_id)s",
"delete from departement where id = %(dept_id)s",
"drop table tags_temp",
"drop table entreprises_temp",
"drop table formations_temp",
"drop table moduleimpls_temp",
"drop table etudids_temp",
"drop table formsemestres_temp",
]
for r in reqs:
cursor.execute(r, {"dept_id": dept_id})
except:
cnx.rollback()
finally:
cnx.commit()

View File

@ -38,6 +38,7 @@ def make_shell_context():
from app.scodoc import notesdb as ndb
from app.scodoc import sco_utils as scu
from flask_login import login_user, logout_user, current_user
import app as mapp # le package app
return {
"db": db,
@ -56,6 +57,7 @@ def make_shell_context():
"logout_user": logout_user,
"ctx": app.test_request_context(),
"models": models,
"mapp": mapp,
}
@ -148,6 +150,9 @@ def user_password(username, password=None): # user-password
@click.argument("dept")
def delete_dept(dept): # delete-dept
"""Delete existing departement"""
from app.scodoc import notesdb as ndb
from app.scodoc import sco_dept
click.confirm(
f"""Attention: Cela va effacer toutes les données du département {dept}
(étudiants, notes, formations, etc)
@ -156,12 +161,12 @@ def delete_dept(dept): # delete-dept
abort=True,
)
db.reflect()
ndb.open_db_connection()
d = models.Departement.query.filter_by(acronym=dept).first()
if d is None:
sys.stderr.write(f"Erreur: le departement {dept} n'existe pas !")
return 2
# XXX TODO: détruire les objets du département !
db.session.delete(d)
sco_dept.delete_dept(d.id)
db.session.commit()
return 0