From 2666d823516c9c6ef0a856d9024b44afe7c0041f Mon Sep 17 00:00:00 2001 From: ilona Date: Fri, 18 Oct 2024 23:12:32 +0200 Subject: [PATCH] =?UTF-8?q?CLI:=20user-delete:=20renforce=20v=C3=A9rificat?= =?UTF-8?q?ions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scodoc.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/scodoc.py b/scodoc.py index e2bdc2812..562c67fb7 100755 --- a/scodoc.py +++ b/scodoc.py @@ -14,7 +14,8 @@ from flask.cli import with_appcontext from flask.templating import render_template from flask_login import login_user, logout_user, current_user import psycopg2 -import sqlalchemy +import sqlalchemy as sa + import app as mapp from app import create_app, cli, db @@ -40,6 +41,8 @@ from app.models.but_refcomp import ( ) from app.models.but_validations import ApcValidationAnnee, ApcValidationRCUE from app.models.evaluations import Evaluation +from app.models.formsemestre import notes_formsemestre_responsables +from app.models.moduleimpls import notes_modules_enseignants from app.scodoc import sco_dump_db from app.scodoc.sco_logos import make_logo_local from app.scodoc.sco_permissions import Permission @@ -214,10 +217,36 @@ def user_delete(username): # user-delete if not u: sys.stderr.write(f"user_delete: user {username} not found\n") return 2 + # Vérifie que l'utilisateur n'est pas... utilisé + # - Resp. FormSemestre (table assoc) + query = sa.select(sa.func.count()).where( + notes_formsemestre_responsables.c.responsable_id == u.id + ) + nb = db.session.execute(query).scalar() + if nb > 0: + sys.stderr.write( + "\nuser_delete: erreur: utilisateur déclaré comme resp. de semestre\n" + ) + return 1 + # - Resp. Module + if ModuleImpl.query.filter_by(responsable_id=217).count() > 0: + sys.stderr.write( + "\nuser_delete: erreur: utilisateur déclaré comme enseignant\n" + ) + return 1 + # - Enseignant d'un module (table assoc) + query = sa.select(sa.func.count()).where(notes_modules_enseignants.c.ens_id == u.id) + nb = db.session.execute(query).scalar() + if nb > 0: + sys.stderr.write( + "\nuser_delete: erreur: utilisateur déclaré comme enseignant\n" + ) + return 1 + # db.session.delete(u) try: db.session.commit() - except (sqlalchemy.exc.IntegrityError, psycopg2.errors.ForeignKeyViolation): + except (sa.exc.IntegrityError, psycopg2.errors.ForeignKeyViolation): sys.stderr.write( f"""\nuser_delete: ne peux pas supprimer l'utilisateur {username}\ncar il est associé à des objets dans ScoDoc (modules, notes, ...).\n""" )