From 738de9d5ed90edb2d731ba6d6d647039ed8916da Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 20 Mar 2023 12:17:28 +0100 Subject: [PATCH] =?UTF-8?q?V=C3=A9rification=20acronymes=20dept?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/departements.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/models/departements.py b/app/models/departements.py index 8c5df10ceb..6f3f775989 100644 --- a/app/models/departements.py +++ b/app/models/departements.py @@ -2,12 +2,15 @@ """ScoDoc models : departements """ +import re from app import db from app.models import SHORT_STR_LEN from app.models.preferences import ScoPreference from app.scodoc.sco_exceptions import ScoValueError +VALID_DEPT_EXP = re.compile(r"^[\w@\\\-\.]+$") + class Departement(db.Model): """Un département ScoDoc""" @@ -60,6 +63,15 @@ class Departement(db.Model): } return data + @classmethod + def invalid_dept_acronym(cls, dept_acronym: str) -> bool: + "Check that dept_acronym is invalid" + return ( + not dept_acronym + or (len(dept_acronym) >= SHORT_STR_LEN) + or not VALID_DEPT_EXP.match(dept_acronym) + ) + @classmethod def from_acronym(cls, acronym): dept = cls.query.filter_by(acronym=acronym).first_or_404() @@ -70,6 +82,8 @@ def create_dept(acronym: str, visible=True) -> Departement: "Create new departement" from app.models import ScoPreference + if Departement.invalid_dept_acronym(acronym): + raise ScoValueError("acronyme departement invalide") existing = Departement.query.filter_by(acronym=acronym).count() if existing: raise ScoValueError(f"acronyme {acronym} déjà existant")