def user_from_attribute(attr: str, value: str):
""" Return User instance based on given value, if exists, compared to
User.attr. Otherwise, raise InvalidRequestError.
Parameters
----------
attr : str
Attribute of a user to check for with `value` value.
value : str
Alleged value of user attribute `attr`.
Raises
------
NoResultFound
If no user exists where `user`.`attr` == `value`.
MultipleResultsFound
If more than one user exists where `user`.`attr` == `value`.
Returns
-------
`gridback.models.User`
An instance of a user where `user`.`attr` == `value`.
"""
try:
return User.query.filter_by(**{attr: value}).one()
except NoResultFound as e:
print_log(e)
raise NoResultFound('No matching user with the provided value.')
except MultipleResultsFound as e:
print_log(e)
raise MultipleResultsFound('Multiple users existed with the provided value.')
评论列表
文章目录