class LogoutView(BaseView): decorators = [login_require('login')] async def post(self, request): """ @api {post} /logout 登出 @apiVersion 0.0.1 @apiName Logout @apiDescription 登出用户 @apiGroup Auth @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 49 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 62 Content-Type: application/json Keep-Alive: 60 { "code": 1002, "message": "请求参数有误" } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } """ current_user = request.get('current_user') with await request.app.redis as r: key = request.app.config.TOKEN_STR + str(current_user.id) token = await r.get(key) if not token: return json(Response.make(code=1002), status=400) r.delete(token.decode()) return json(Response.make())
class CommentView(BaseView): decorators = [login_require('login')] async def delete(self, request, comment_id): """ @api {delete} /blogs/<blog_id:int>/comments 删除博客评论 @apiVersion 0.0.1 @apiName Delete-Blog-comment @apiDescription 删除一个博客的评论一个评论 @apiGroup Comment @apiSuccessExample {json} Success-response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 233 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1005, "message": "请求资源不存在" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1003, "message": "数据库错误" } """ comment = await Comment.db_get(id=comment_id, is_delete=False) if not comment: return json(Response.make(code=1005), status=400) if not comment.author == request.get('current_user'): return json(Response.make(code=1003), status=400) await comment.db_update(is_delete=True) return json(Response.make())
class SingleBlogView(BaseView): decorators = [login_require('login')] async def get(self, request, blog_id): """ @api {get} /blogs/<blog_id:int> 查看单个博客内容 @apiVersion 0.0.1 @apiName Single-blog @apiDescription 查询单个博客的具体内容 @apiGroup Blog @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 280 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": { "author": { "follow_value": 0, "followed_value": 0, "gender": 2, "id": 1, "username": "******" }, "content": "yyyyyy", "create_time": "20:20:01.730670+00:00", "id": 1, "is_delete": false, "last_update_time": "20:20:01.730676+00:00", "like_value": 0, "title": "wwwww" } } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 120 Content-Type: application/json Keep-Alive: 60 { "code": 1000, "message": "系统错误" } """ blog = await Blog.db_get(id=blog_id, is_delete=False) self._check_data(blog, BlogsSchema()) if self.error: return self.error_resp return json(Response.make(result=self.response_arg)) async def patch(self, request, blog_id): """ @api {patch} /blogs/<blog_id:int> 修改博客数据 @apiVersion 0.0.1 @apiName update-blog @apiDescription 修改博客的数据 @apiGroup Blog @apiParam {string} [title] 博客标题 @apiParam {string} [content] 博客内容 @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 280 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": { "author": { "follow_value": 0, "followed_value": 0, "gender": 2, "id": 1, "username": "******" }, "content": "yyyyyy", "create_time": "20:20:01.730670+00:00", "id": 1, "is_delete": false, "last_update_time": "20:20:01.730676+00:00", "like_value": 0, "title": "wwwww" } } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 50 Content-Type: application/json Keep-Alive: 60 { "code": 1003, "message": "权限不足" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1005, "message": "请求资源不存在" } """ current_user = request.get('current_user') self._check_request(request, PatchBlogSchema) if self.error: return self.error_resp blog = await Blog.db_get(id=blog_id, is_delete=False) if not blog: return json(Response.make(code=1005), status=400) if not current_user.id == blog.author_id: return json(Response.make(code=1003), status=400) blog_n = await blog.db_update(**self.request_arg) if not blog_n: return json(Response.make(code=1004), status=400) return await self.get(request, blog_id) async def delete(self, request, blog_id): """ @api {delete} /blogs/<blog_id:int> 删除博客 @apiVersion 0.0.1 @apiName delete-blog @apiDescription 删除博客 @apiGroup Blog @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 280 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 50 Content-Type: application/json Keep-Alive: 60 { "code": 1003, "message": "权限不足" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1005, "message": "请求资源不存在" } """ current_user = request.get('current_user') blog = await Blog.db_get(id=blog_id, is_delete=False) if not blog: return json(Response.make(code=1005), status=400) if not current_user.id == blog.author_id: return json(Response.make(code=1003), status=400) blog_n = await blog.db_update(is_delete=True) if not blog_n: return json(Response.make(code=1004), status=400) return json(Response.make())
class MyBlogView(BaseView): decorators = [login_require('login')] async def get(self, request): """ @api {get} /my/blogs 我的博客 @apiVersion 0.0.1 @apiName my-blogs @apiDescription 获取我的博客 @apiGroup Blog @apiParam {String} [sort=create_time] 排序条件 @apiParam {Integer} [page=1] 页码 @apiParam {Integer} [count=10] 每页数量 @apiParam {Integer=0,1} [desc=0] 是否倒序排列 @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 193 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": { "author": { "follow_value": 0, "followed_value": 0, "gender": 2, "id": 1, "username": "******" }, "blogs": [ { "content": "lallala", "create_time": "11:27:59.981733+00:00", "id": 1, "like_value": 0, "title": "llalalla" } ] } } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 120 Content-Type: application/json Keep-Alive: 60 { "code": 1000, "message": "系统错误" } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } """ self._check_request(request, PageSchema) if self.error: return self.error_resp current_user = request.get('current_user') blogs = await Blog.get_by(**self.request_arg, author=current_user, is_delete=False) self._check_data(blogs, BaseBlogSchema(many=True)) if self.error: return self.error_resp result = {"blogs": self.response_arg} self._check_data(current_user, BaseUserSchema()) if self.error: return self.error_resp result.update({"author": self.response_arg}) return json(Response.make(result=result))
class BlogView(BaseView): decorators = [login_require('login')] async def post(self, request): """ @api {post} /blog 博客发布 @apiVersion 0.0.1 @apiName blog-create @apiDescription 发布博客 @apiGroup Blog @apiParam {string} title blog标题 @apiParam {string} content blog内容 @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 49 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": { "content": "firstjob", "title": "lalalla" } } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 62 Content-Type: application/json Keep-Alive: 60 { "code": 1000, "message": "系统错误" } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 120 Content-Type: application/json Keep-Alive: 60 { "code": 1002, "message": "请求参数有误", "result": { "content": [ "Missing data for required field." ] } } """ current_user = request['current_user'] self._check_request(request, BlogSchema) if self.error: return self.error_resp blog = await Blog.db_create(author=current_user, **self.request_arg) if not blog: return json(Response.make(code=1000), status=400) self._check_data(blog, BlogSchema()) if self.error: return self.error_resp return json(Response.make(result=self.response_arg))
class CommentsView(BaseView): decorators = [login_require('login')] async def get(self, request, blog_id): """ @api {get} /blogs/<blog_id:int>/comments 查询博客评论 @apiVersion 0.0.1 @apiName Blog-comments @apiDescription 查询一个博客的评论 @apiGroup Comment @apiParam {String} [sort=create_time] 排序条件 @apiParam {Integer} [page=1] 页码 @apiParam {Integer} [count=10] 每页数量 @apiParam {Integer=0,1} [desc=0] 是否倒序排列 @apiSuccessExample {json} Success-response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 233 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": [ { "author": { "follow_value": 0, "followed_value": 0, "gender": 2, "id": 1, "username": "******" }, "blog": 1, "content": "yyyyyuu", "create_time": "20:20:11.777450+00:00", "id": 1, "is_delete": false, "like_value": 0 } ] } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1005, "message": "请求资源不存在" } """ self._check_request(request, PageSchema) blog = await Blog.db_get(id=blog_id, is_delete=False) if not blog: return json(Response.make(code=1005), status=400) comments = await Comment.get_by(**self.request_arg, blog=blog) self._check_data(comments, CommentSchema(many=True)) if self.error: return self.error_resp return json(Response.make(result=self.response_arg)) async def post(self, request, blog_id): """ @api {post} /blogs/<blog_id:int>/comments 博客评论 @apiVersion 0.0.1 @apiName Post-comment @apiDescription 评论一个博客的评论 @apiGroup Comment @apiSuccessExample {json} Success-response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 233 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": [ { "author": { "follow_value": 0, "followed_value": 0, "gender": 2, "id": 1, "username": "******" }, "blog": 1, "content": "yyyyyuu", "create_time": "20:20:11.777450+00:00", "id": 1, "is_delete": false, "like_value": 0 } ] } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1005, "message": "请求资源不存在" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1004, "message": "数据库错误" } """ self._check_request(request, CommentSchema) if self.error: return self.error_resp blog = await Blog.db_get(id=blog_id, is_delete=False) if not blog: return json(Response.make(code=1005), status=400) author = request.get('current_user') comment = await Comment.db_create(blog=blog, author=author, **self.request_arg) if not comment: return json(Response.make(code=1004), status=400) return await self.get(request, blog_id)
class ChangeAuthView(BaseView): decorators = [login_require('login')] async def post(self, request): """ @api {post} /account 邮箱,密码修改 @apiVersion 0.0.1 @apiName Change-auth @apiDescription 更改邮箱,密码 @apiGroup Auth @apiParam {string} email 邮箱 @apiParam {string} password 用户密码 @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 49 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": "Success" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 62 Content-Type: application/json Keep-Alive: 60 { "code": 1000, "message": "系统错误" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 62 Content-Type: application/json Keep-Alive: 60 { "code": 1002, "message": "请求参数有误", "result": { "email": [ "Missing data for required field." ] } } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } """ current_user = request.get("current_user") self._check_request(request, UserSchema) if self.error: return self.error_resp email = self.request_arg.get('email') password = self.request_arg.get('password') exist = await User.db_get(email=email) if email == current_user.email and\ exist: return json(Response.make(result="Not Change")) with await request.app.redis as coon: token = request.headers.get('authorization').split(" ")[1] await coon.delete(token) update_status = await current_user.db_update(email=email, password=password, active=False) if not update_status: return json(Response.make(code=1000), status=400) token = get_random_str(20) email_status = await email_msg(request, email, self.request_arg.get('username'), token) await current_user.gen_confirm_code(request, token) if not email_status: return json(Response.make(code=1000), status=400) return json(Response.make(result='Success'))
class MyInfoView(BaseView): decorators = [login_require('login')] async def get(self, request): """ @api {get} /my/info 获取我的信息 @apiVersion 0.0.1 @apiName my-info @apiDescription 获取我的个人信息 @apiGroup My @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 193 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": { "create_time": "09:22:56.251781+00:00", "email": "*****@*****.**", "follow_value": 0, "followed_value": 0, "gender": 2, "id": 1, "last_login_time": "09:22:56.251787+00:00", "username": "******" } } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 120 Content-Type: application/json Keep-Alive: 60 { "code": 1002, "message": "请求参数有误", "result": { "content": [ "Missing data for required field." ] } } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } """ current_user = request.get('current_user') user = await User.db_get(id=current_user.id) self._check_data(user, MyInfoSchema()) if self.error: return self.error_resp return json(Response.make(result=self.response_arg)) async def patch(self, request): """ @api {patch} /my/info 修改我的信息 @apiVersion 0.0.1 @apiName my-info-patch @apiDescription 对个人信息修改 @apiGroup My @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 193 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": { "create_time": "09:22:56.251781+00:00", "follow_value": 0, "followed_value": 0, "gender": 2, "id": 1, "last_login_time": "09:22:56.251787+00:00", "username": "******" } } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 120 Content-Type: application/json Keep-Alive: 60 { "code": 1002, "message": "请求参数有误", "result": { "content": [ "Missing data for required field." ] } } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } """ current_user = request.get('current_user') self._check_request(request, UserSchema) if self.error: return self.error_resp current_user = await current_user.db_update(**self.request_arg) if not current_user: return json(Response.make(code=1004), status=400) return await self.get(request) async def post(self, request): current_user = request.get('current_user') self._check_request(request, UserSchema) if self.error: return self.error_resp current_user = await current_user.db_update(active=False) if not current_user: return json(Response.make(code=1004), status=400) return await self.get(request)
class GetFollowedView(BaseView): decorators = [login_require('login')] async def get(self, request): """ @api {post} /my/follow 关注我的用户 @apiVersion 0.0.1 @apiName Follow-Me @apiDescription 关注的我用户 @apiGroup My @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 128 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": [ { "follower": { "follow_value": 1, "followed_value": 0, "gender": 2, "id": 1, "username": "******" } } ] } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 62 Content-Type: application/json Keep-Alive: 60 { "code": 1000, "message": "系统错误" } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } """ self._check_request(request, PageSchema) if self.error: return self.error_resp current_user = request.get('current_user') followed_users = await Follow.get_by(**self.request_arg, followed=current_user) self._check_data(followed_users, FollowerSchema(many=True)) if self.error: return self.error_resp return json(Response.make(result=self.response_arg))
class FollowView(BaseView): decorators = [login_require('login')] async def post(self, request, pk): """ @api {post} /follow/<pk:int> 用户关注 @apiVersion 0.0.1 @apiName Follow @apiDescription 关注某个用户 @apiGroup User @apiSuccessExample {json} Success-Response: HTTP/1.1 200 OK Connection: keep-alive Content-Length: 49 Content-Type: application/json Keep-Alive: 60 { "code": 0, "message": "success", "result": "Success" } @apiErrorExample {json} Error-Response: HTTP/1.1 400 Bad Request Connection: keep-alive Content-Length: 62 Content-Type: application/json Keep-Alive: 60 { "code": 1000, "message": "系统错误" } @apiErrorExample {json} Error-Response: HTTP/1.1 401 Unauthorized Connection: keep-alive Content-Length: 68 Content-Type: application/json Keep-Alive: 60 { "code": 1001, "message": "账号或密码错误" } """ current_user = request.get('current_user') follow_user = await User.db_get(id=pk) follow_record = await Follow.db_get(follower_id=current_user.id, followed_id=follow_user.id) if not follow_user: return json(Response.make(code=1005), status=400) result = await current_user.follow(follow_record=follow_record, follow_user=follow_user, current_user=current_user) if not result: return json(Response.make(code=1000), status=400) return json(Response.make(result='success'), status=200)