diff --git a/app/auth/models.py b/app/auth/models.py index 02b0c5e7..8b359756 100644 --- a/app/auth/models.py +++ b/app/auth/models.py @@ -250,7 +250,7 @@ class User(UserMixin, db.Model): """string repr. of user's roles (with depts) e.g. "Ens_RT, Ens_Info, Secr_CJ" """ - return ",".join("{r.role.name}_{r.dept}".format(r=r) for r in self.user_roles) + return ",".join(f"{r.role.name}_{r.dept or ''}" for r in self.user_roles) def is_administrator(self): "True if i'm an active SuperAdmin" @@ -400,15 +400,17 @@ class UserRole(db.Model): return "".format(self.user, self.role, self.dept) @staticmethod - def role_dept_from_string(role_dept): + def role_dept_from_string(role_dept: str): """Return tuple (role, dept) from the string role_dept, of the forme "Role_Dept". - role is a Role instance, dept is a string. + role is a Role instance, dept is a string, or None. """ fields = role_dept.split("_", 1) # maxsplit=1, le dept peut contenir un "_" if len(fields) != 2: raise ScoValueError("Invalid role_dept") role_name, dept = fields + if dept == "": + dept = None role = Role.query.filter_by(name=role_name).first() if role is None: raise ScoValueError("role %s does not exists" % role_name)