diff --git a/app/models/departements.py b/app/models/departements.py index 8c5df10c..6f3f7759 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")