Exemple #1
0
def get_fans_list():
    """
    获取粉丝列表
    :return: {
        "data": [
            {
            "follow_id": "follow_id",
            "user_id": "用户id",
            "user_username": "******",
            "user_avatar": "头像地址",
            "user_description": "个人简介",
            "followed_user_id": "被关注用户id",
            "followed_user_username": "******",
            "followed_user_avatar": "被关注用户头像",
            "followed_user_description": "被关注用户个人简介",
            "status": "true为已关注,false为未关注"
            },
            ...
        ],
        "msg": "OK",
        "status": 200
    }
    """
    uuid = request.args.get('uuid')
    username = request.args.get('username')
    last_follow_id = request.args.get('last_follow_id', default=0)
    limit = request.args.get('limit', default=20)

    if uuid is None and username is None:
        uuid = session.get('uuid')

    return Result.OK().data(
        database.get_fans_list(uuid, username, last_follow_id, limit)).build()
Exemple #2
0
def update_post(post_id: int):
    """
    修改帖子
    :param post_id: 帖子id
    :param: content: 帖子内容
    :return: {
        "data": {
            "post_id": "帖子id"
        },
        "msg": "OK",
        "status": 200
    }
    """
    content: str = request.form.get('content')
    ret = check_post_or_comment_content(content)
    if ret is not True:
        raise HttpError(400, ret)

    imgs_name = [
        save_image(BaseConfig.post_image_dir, img)
        for img in request.files.getlist('imgs')
    ]

    return Result.OK().data({
        'post_id':
        database.update_post(content=content,
                             imgs_name=imgs_name,
                             uuid=session.get('uuid'),
                             post_id=post_id)
    }).build()
Exemple #3
0
def save_comment():
    """
    发表评论
    :param: parent_id: 被评论的帖子或评论的id
    :param: type: 是什么的评论,0是帖子,1是评论
    :param: content: 内容
    :return: {
        "data": {
            "comment_id": "评论id"
        },
        "msg": "OK",
        "status": 200
    }
    """
    content: str = request.form.get('content')
    ret = check_post_or_comment_content(content)
    if ret is not True:
        raise HttpError(400, ret)

    parent_id: int = request.form.get('parent_id')
    _type: int = request.form.get('type')

    img: FileStorage = request.files.get('img')
    img_name = None if img is None else save_image(
        BaseConfig.comment_image_dir, img)

    database.save_comment(content=content,
                          parent_id=parent_id,
                          _type=_type,
                          img_name=img_name,
                          uuid=session.get('uuid'))

    return Result.OK().build()
Exemple #4
0
def search_users():
    """
    查找用户
    :return: {
        "data": [
            {
                "username": "******",
                "uuid": "uuid",
                "sex": "性别 男、女、不明",
    	        "grade": "年级 数字",
    	        "major": "专业",
    	        "description": "个人介绍",
                "posts_num": "帖子数量",
                "avatar": "头像地址"
            },
            ...
        ],
        "msg": "OK",
        "status": 200
    }
    """
    return Result.OK().data(
        database.search_users(
            keyword=request.args.get('keyword'),
            last_user_uuid=request.args.get('last_user_uuid'),
            limit=request.args.get('limit', default=10))).build()
Exemple #5
0
def get_user_info():
    """
    获取用户信息
    :return: {
        "data": {
            "username": "******",
            "uuid": "uuid",
            "sex": "性别 男、女、不明",
    	    "grade": "年级 数字",
    	    "major": "专业",
    	    "description": "个人介绍",
            "posts_num": "帖子数量",
            "avatar": "头像文件名",
            "follow_status": "boolean 是否关注",
            "fans_num": "粉丝数量",
            "follow_num": "关注的人的数量"
        },
        "msg": "OK",
        "status": 200
    }
    """
    uuid = request.args.get('uuid')
    username = request.args.get('username')

    if uuid is None and username is None:
        uuid = session.get('uuid')

    return Result.OK().data(database.get_user_info(uuid, username)).build()
Exemple #6
0
def update_comment(comment_id: int):
    """
    修改评论
    :param comment_id: 评论id
    :param: content: 新内容
    :return: {
        "data": {
            "comment_id": "评论id"
        },
        "msg": "OK",
        "status": 200
    }
    """
    content: str = request.form.get('content')
    ret = check_post_or_comment_content(content)
    if ret is not True:
        raise HttpError(400, ret)

    img: FileStorage = request.files.get('img')
    img_name = None if img is None else save_image(
        BaseConfig.comment_image_dir, img)

    database.update_comment(content=content,
                            img_name=img_name,
                            comment_id=comment_id,
                            uuid=session.get('uuid'))

    return Result.OK().build()
Exemple #7
0
def delete_comment(comment_id: int):
    """
    删除评论
    :param comment_id: 评论id
    :return: {
        "data": null,
        "msg": "OK",
        "status": 200
    }
    """
    database.delete_comment(comment_id, session.get('uuid'))

    return Result.OK().build()
Exemple #8
0
def logout():
    """
    退出登录
    :return: {
        "data": null,
        "msg": "OK",
        "status": 200
    }
    """
    if session.get('uuid'):
        session['uuid'] = None

    return Result.OK().build()
Exemple #9
0
def delete_post(post_id: int):
    """
    删除帖子
    :param post_id: 帖子id
    :return: {
        "data": null,
        "msg": "OK",
        "status": 200
    }
    """
    database.delete_post(post_id, session.get('uuid'))

    return Result.OK().build()
Exemple #10
0
def update_user_avatar():
    """
    上传头像
    :return: {
        "data": "头像地址",
        "msg": "OK",
        "status": 200
    }
    """
    avatar: FileStorage = request.files.get('avatar')
    filename = save_image(BaseConfig.avatar_dir, avatar)

    database.update_avatar(session.get('uuid'), filename)
    return Result.OK().data(filename).build()
Exemple #11
0
def update_user_background_image():
    """
    上传背景
    :return: {
        "data": "背景地址",
        "msg": "OK",
        "status": 200
    }
    """
    bg: FileStorage = request.files.get('bg')
    filename = save_image(BaseConfig.bg_dir, bg)

    database.update_bg(session.get('uuid'), filename)
    return Result.OK().data(filename).build()
Exemple #12
0
def update_user_info():
    """
    修改用户信息
    :return: {
        "data": null,
        "msg": "OK",
        "status": 200
    }
    """
    user_detail: UserDetailModel = UserDetailModel.get_parameters()
    database.update_user_detail(uuid=session.get('uuid'),
                                user_detail=user_detail.to_dict())
    session['username'] = user_detail.username
    return Result.OK().build()
Exemple #13
0
def get_user_info():
    """
    获取用户信息
    :return: {
        "data": {
            "username": "******",
            "uuid": "uuid"
        },
        "msg": "OK",
        "status": 200
    }
    """
    return Result.OK().data(database.get_user_info(
        session.get('uuid'))).build()
Exemple #14
0
def create_user():
    """
    用户注册
    :param: username: 用户名
    :param: password: 密码
    :param: check_pwd: 二次验证密码
    :return: {
        "data": null,
        "msg": "OK",
        "status": 200
    }
    """
    user: UserModel = UserModel.get_parameters()
    if user.password != user.check_pwd:
        raise HttpError(400, '两次填写的密码不一致')
    database.create_user(username=user.username, password=user.password)
    return Result.OK().build()
Exemple #15
0
def get_posts():
    """
    获取多个帖子
    :param: uuid: uuid
    :param: username: 用户名
    :param: keyword: 搜索关键字
    :param: last_id: 已获取帖子中最后一个帖子的id,默认为0
    :param: limit: 要获取的数目, 默认为5
    :return: {
        "data":[
                {
                    "post_id": "帖子id",
                    "username": "******",
                    "uuid": "发帖人uuid",
                    "content": "内容",
                    "comments_num": "评论数目",
                    "created_at": "创建时间",
                    "updated_at": "修改时间"
                    "comments": [
                        {
                            "comment_id": "评论id",
                            "parent_id": "被评论的帖子或评论的id",
                            "type": "是什么的评论,0是帖子,1是评论",
                            "content": "评论内容",
                            "comments_num": "评论数目",
                            "username": "******",
                            "uuid"': "发帖人uuid",
                            "created_at": "创建时间",
                            "updated_at": "修改时间"
                        }
                        ...
                    ]
                },
                ...
            ],
        "msg": "OK",
        "status": 200
    }
    """
    return Result.OK().data(
        database.get_posts(uuid=request.args.get('uuid'),
                           username=request.args.get('username'),
                           keyword=request.args.get('keyword'),
                           last_id=request.args.get('last_id', default=0),
                           limit=request.args.get('limit',
                                                  default=10))).build()
Exemple #16
0
def follow_user():
    """
    关注或取关用户
    """
    data: dict = request.get_json(force=True)

    username: str = data.get('username')
    if username is None:
        raise HttpError(400, '用户名错误')
    if username == session.get('username'):
        raise HttpError(400, '无法关注自己')

    status: bool = data.get('status')
    if status is None:
        raise HttpError(400, '请选择要进行的操作')

    database.follow_user(session.get('uuid'), username, status)
    return Result.OK().build()
Exemple #17
0
def login():
    """
    用户登录
    :param: username: 用户名
    :param: password: 密码
    :return: {
        "data": {
            "uuid": "uuid",
            "username":"******"
        },
        "msg": "OK",
        "status": 200
    }
    """
    user: UserModel = UserModel.get_parameters()

    uuid, username = database.user_login(user.username, user.password)
    session['uuid'] = uuid
    return Result.OK().data({'uuid': uuid, 'username': username}).build()
Exemple #18
0
def update_user_password():
    """
    修改密码
    :param: old_password: 旧密码
    :param: password: 密码
    :param: check_pwd: 二次验证密码
    :return: {
        "data": null,
        "msg": "OK",
        "status": 200
    }
    """
    password: PasswordModel = PasswordModel.get_parameters()
    if password.password != password.check_pwd:
        raise HttpError(400, '两次填写的密码不一致')
    database.update_user_password(uuid=session.get('uuid'),
                                  password=password.password,
                                  old_pwd=password.old_pwd)
    return Result.OK().build()
Exemple #19
0
def save_post():
    """
    发表帖子
    :param: content: 帖子内容
    :return: {
        "data": {
            "post_id": "帖子id"
        },
        "msg": "OK",
        "status": 200
    }
    """
    content: str = request.get_json(force=True).get('content')
    ret = check_post_or_comment_content(content)
    if ret is not True:
        raise HttpError(400, ret)

    post_id = database.save_post(content, session.get('uuid'))

    return Result.OK().data({'post_id': post_id}).build()
Exemple #20
0
def get_post(post_id: int):
    """
    通过id获取帖子
    :param post_id: 帖子id
    :param: last_comment_id: 最后一个评论的id,默认为0
    :param: limit: 要获取的评论数目, 默认为5
    :return: {
        "data": {
            "post_id": "帖子id",
            "username": "******",
            "uuid": "发帖人uuid",
            "content": "内容",
            "comments_num": "评论数目",
            "created_at": "创建时间",
            "updated_at": "修改时间",
            "comments": [
                {
                    "id": "评论id",
                    "parent_id": "被评论的帖子或评论的id",
                    "type": "是什么的评论,0是帖子,1是评论",
                    "content": "评论内容",
                    "comments_num": "评论数目",
                    "username": "******",
                    "uuid"': "发帖人uuid",
                    "created_at": "创建时间",
                    "updated_at": "修改时间"
                }
                ...
            ]
        },
        "msg": "OK",
        "status": 200
    }
    """
    return Result.OK().data(
        database.get_post(post_id=post_id,
                          last_comment_id=request.args.get('last_comment_id',
                                                           default=0),
                          limit=request.args.get('limit',
                                                 default=10))).build()
Exemple #21
0
def save_comment():
    """
    发表评论
    :param: parent_id: 被评论的帖子或评论的id
    :param: type: 是什么的评论,0是帖子,1是评论
    :param: content: 内容
    :return: {
        "data": {
            "comment_id": "评论id"
        },
        "msg": "OK",
        "status": 200
    }
    """
    comment: CommentModel = CommentModel.get_parameters()

    return Result.OK().data({
        'comment_id':
        database.save_comment(content=comment.content,
                              parent_id=comment.parent_id,
                              _type=comment.type,
                              uuid=session.get('uuid'))
    }).build()
Exemple #22
0
def get_comments():
    """
    通过id获取某个帖子或评论的多条评论
    :param: parent_id: 被评论的帖子或评论的id
    :param: type: 是什么的评论,0是帖子,1是评论
    :param: last_id: 已获取评论中最后一个评论的id,默认为0
    :param: limit: 要获取的数目, 默认为5
    :return: {
        "data": {
            "comments": [
                {
                    "comment_id": "评论id",
                    "parent_id": "被评论的帖子或评论的id",
                    "type": "是什么的评论,0是帖子,1是评论",
                    "content": "评论内容",
                    "comments_num": "评论数目",
                    "username": "******",
                    "uuid": "发帖人uuid",
                    "created_at": "创建时间",
                    "updated_at": "修改时间"
                }
                ...
            ]
        },
        "msg": "OK",
        "status": 200
    }
    """
    ret = database.get_comments(parent_id=request.args.get('parent_id'),
                                _type=request.args.get('type'),
                                last_comment_id=request.args.get('last_id',
                                                                 default=0),
                                limit=request.args.get('limit', default=5))
    return Result.OK().data({
        'comments': ret[0],
        'comments_num': ret[1]
    }).build()
Exemple #23
0
def update_comment(comment_id: int):
    """
    修改评论
    :param comment_id: 评论id
    :param: content: 新内容
    :return: {
        "data": {
            "comment_id": "评论id"
        },
        "msg": "OK",
        "status": 200
    }
    """
    content: str = request.get_json(force=True).get('content')
    ret = check_post_or_comment_content(content)
    if ret is not True:
        raise HttpError(400, ret)

    return Result.OK().data({
        'comment_id':
        database.update_comment(content=content,
                                comment_id=comment_id,
                                uuid=session.get('uuid'))
    })
Exemple #24
0
def update_post(post_id: int):
    """
    修改帖子
    :param post_id: 帖子id
    :param: content: 帖子内容
    :return: {
        "data": {
            "post_id": "帖子id"
        },
        "msg": "OK",
        "status": 200
    }
    """
    content: str = request.get_json(force=True).get('content')
    ret = check_post_or_comment_content(content)
    if ret is not True:
        raise HttpError(400, ret)

    return Result.OK().data({
        'post_id':
        database.update_post(content=content,
                             uuid=session.get('uuid'),
                             post_id=post_id)
    }).build()