fix(users): Reduce amount of SELECT queries in set_roles
This commit is contained in:
parent
ec7bf39666
commit
9f6aa38925
|
@ -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):
|
||||||
|
|
Loading…
Reference in New Issue