fix: affiche role SuperAdmin

This commit is contained in:
Emmanuel Viennet 2021-08-30 16:55:07 +02:00
parent f672c17bbf
commit c14684a1dc

View File

@ -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 "<UserRole u={} r={} dept={}>".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)