msg.py 文件源码

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

项目:msg 作者: lily-mayfield 项目源码 文件源码
def get(self, user_id=None, username=None):
        """Get a specific user's info by user ID
        *or* username.

        This returns a 400 if neither user_id nor
        username was provided. Returns a 404 if
        cannot find a user by the provided user ID
        or username.

        Arguments:
            user_id (int|None): --
            username (str|None): --

        Warning:
            If both `user_id` and `username` are specified,
            user_id will be used.

        Returns:
            None: If aborted.
            dict: If such a user is found, the return value
                is a dictionary describing the user.

        """

        if user_id is not None:
            user = db.session.query(models.User).get(user_id)

            if user is None:
                message = "No user matching ID: %s" % user_id
                flask_restful.abort(404, message=message)

        elif username is not None:
            user = (db.session.query(models.User)
                    .filter(models.User.username == username).first())

            if user is None:
                message = "No user matching username: %s" % username
                flask_restful.abort(404, message=message)

        else:
            message = "Must specify user_id or username."
            flask_restful.abort(400, message=message)

        return user.to_dict()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号