fix(users): Reduce amount of SELECT queries in set_roles

This commit is contained in:
Ferdinand Thiessen 2021-12-18 01:52:35 +01:00
parent ec7bf39666
commit 9f6aa38925
1 changed files with 7 additions and 9 deletions

View File

@ -90,15 +90,13 @@ def update_user(user):
db.session.commit() db.session.commit()
def set_roles(user: User, roles: list[str], create=False): def set_roles(user: User, roles: list[str]):
user.roles_.clear() if not isinstance(roles, list) and any([not isinstance(r, str) for r in roles]):
for role_name in roles: raise BadRequest("Invalid role name")
role = Role.query.filter(Role.name == role_name).one_or_none() fetched = Role.query.filter(Role.name.in_(roles)).all()
if not role: if len(fetched) < len(roles):
if not create: raise BadRequest("Invalid role name, role not found")
raise BadRequest("Role not found >{}<".format(role_name)) user.roles_ = fetched
role = Role(name=role_name)
user.roles_.append(role)
def modify_user(user, password, new_password=None): def modify_user(user, password, new_password=None):