database.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:GridLight-Server 作者: Lunabit 项目源码 文件源码
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.')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号