From 4c325b70de18a5733e2ebedfa96ab492d7fe0138 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Sat, 8 Jan 2022 17:38:38 +0100 Subject: [PATCH] sanitize formations after upgrades + renumber modules --- app/models/formations.py | 7 +++++++ scodoc.py | 13 ++++++++++--- tools/debian/postinst | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/models/formations.py b/app/models/formations.py index 1dfd8728..e926471c 100644 --- a/app/models/formations.py +++ b/app/models/formations.py @@ -4,6 +4,7 @@ from app import db from app.comp import df_cache from app.models import SHORT_STR_LEN +from app.models.modules import Module from app.scodoc import notesdb as ndb from app.scodoc import sco_cache from app.scodoc import sco_codes_parcours @@ -114,6 +115,7 @@ class Formation(db.Model): return change = False for mod in self.modules: + # --- Indices de semestres: if ( mod.ue.semestre_idx is not None and mod.ue.semestre_idx > 0 @@ -122,10 +124,15 @@ class Formation(db.Model): mod.semestre_id = mod.ue.semestre_idx db.session.add(mod) change = True + # --- Types de modules if mod.module_type is None: mod.module_type = scu.ModuleType.STANDARD db.session.add(mod) change = True + # --- Numéros de modules + if Module.query.filter_by(formation_id=220, numero=None).count() > 0: + scu.objects_renumber(db, self.modules.all()) + db.session.commit() if change: self.invalidate_module_coefs() diff --git a/scodoc.py b/scodoc.py index 28e2f59b..3db205e4 100755 --- a/scodoc.py +++ b/scodoc.py @@ -449,14 +449,21 @@ def photos_import_files(formsemestre_id: int, xlsfile: str, zipfile: str): @app.cli.command() +@click.option("--sanitize/--no-sanitize", default=False) @with_appcontext -def clear_cache(): # clear-cache +def clear_cache(sanitize): # clear-cache """Clear ScoDoc cache This cache (currently Redis) is persistent between invocation - and it may be necessary to clear it during development or tests. + and it may be necessary to clear it during upgrades, + development or tests. """ + click.echo("Flushing Redis cache...") clear_scodoc_cache() - click.echo("Redis caches flushed.") + if sanitize: + # sanitizes all formations: + click.echo("Checking formations...") + for formation in Formation.query: + formation.sanitize_old_formation() def recursive_help(cmd, parent=None): diff --git a/tools/debian/postinst b/tools/debian/postinst index 1f3683ea..a5fcc1b1 100755 --- a/tools/debian/postinst +++ b/tools/debian/postinst @@ -107,7 +107,7 @@ then # utilise les scripts dans migrations/version/ # pour mettre à jour notre base (en tant qu'utilisateur scodoc) export FLASK_ENV="production" - su -c "(cd $SCODOC_DIR && source venv/bin/activate && flask db upgrade && flask clear-cache)" "$SCODOC_USER" + su -c "(cd $SCODOC_DIR && source venv/bin/activate && flask db upgrade && flask clear-cache --sanitize)" "$SCODOC_USER" fi # ------------ LOGROTATE