def post(self, request, blog_id, type): """ 评论 """ try: obj = json.loads(request.body) blog = request.blog user = request.my_user type = obj['type'] comment_info = obj['comment_info'] to_user_id = obj.get('to_user_id') parent_comment_id = obj.get('parent_comment_id') except: return error(400, '缺少必要参数') if not comment_info: return error(400, '请输入评论内容') with transaction.atomic(): try: # 子评论 if to_user_id and parent_comment_id: # 父级评论存在 Comment.objects.get(id=parent_comment_id) # 检查目标用户 User.objects.get(id=to_user_id) comment = Comment.objects.create(genre=type, parent_comment_id=parent_comment_id, to_user_id=to_user_id, blog=blog, user=user) comment_history = self.set_comment_history(type, comment, comment_info) res = { 'parent_comment_id': comment.parent_comment_id, 'type': comment.genre, 'create_time': str(comment.create_time).split('.')[0], 'user_id': comment.user.id, 'avatar': str(comment.user.avatar), 'user_name': comment.user.username, 'to_user_id': comment.to_user_id, 'to_user_name': User.objects.filter(id=comment.to_user_id)[0].username } else: comment = Comment.objects.create(genre=type, blog=blog, user=user) comment_history = self.set_comment_history(type, comment, comment_info) res = { 'comment_id': comment.id, 'type': comment.genre, 'create_time': str(comment.create_time).split('.')[0], 'user_id': comment.user.id, 'avatar': str(comment.user.avatar), 'user_name': comment.user.username, } master = User.objects.get(blog=blog) UserHistory.objects.filter(user=master).update(comments=F('comments') + 1) update.update_comment(blog_id) except: return error(422, '评论失败') return success(res)
def get(self, request, type, page): if int(page) < 0 or int(page) > 90: return error(400, '页面数错误') if type == 'original': return success(self.get_blog_info('original', page)) if type == 'default': return success(self.get_blog_info('default', page)) if type == 'hot': return success(self.get_blog_info('hot', page)) if type == 'featured': blog_list = self.get_featured_info(page) if blog_list: return success(blog_list) return error(404, '推荐已经看完了') return error(400, '排行榜类型错误')
def dispatch(self, request, *args, **kwargs): try: blog = Blog.objects.get(id=kwargs['blog_id'], is_show=True, status=1) request.blog = blog except: return error(404, "博客不存在") return super().dispatch(request, *args, **kwargs)
def post(self, request): # 修改为json obj = json.loads(request.body) content = obj.get('content') # 博客正文 title = obj.get('title') # 博客标题 abstract = obj.get('abstract') # 摘要 # 需要一个列表 category = obj.get('category') # 分类列表 type = obj.get('blog_type') # 博客状态类型 # 数据处理 if not content or not title or not abstract or not category or not type: return error(701, "博客内容不完整") if len(abstract) > 96: return error(400, '摘要超过长度') with transaction.atomic(): try: if type == "save": blog = Blog.objects.create(title=title, abstract=abstract, status='2', user=request.my_user, is_show=False) else: blog = Blog.objects.create(title=title, abstract=abstract, user=request.my_user) Content.objects.create(content=content, blog=blog) for item in category: try: old_cate = Category.objects.get(category=item) except: # 未查询到分类,则创建新分类 new_cate = blog.category_set.create(category=item) else: # 查询到分类,添加多对多属性 old_cate.blog.add(blog) # 添加历史记录 try: request.my_user.userhistory except: # 创建博客记录 UserHistory.objects.create(user=request.my_user) UserHistory.objects.filter(user=request.my_user).update(original=F('original') + 1) except: return error(500, '保存失败') r9.lpush('featured_ids', blog.id) return success({'blog_id': blog.id})
def patch(self, request, user_id): """ 删除博客 """ blog_id = json.loads(request.body).get('blog_id') if update.update_status(blog_id, '3'): return success() return error(404, '删除失败')
def get(self,request, user_id, type): if type == 'user_detail': res = self.get_user_detail(request,user_id) elif type=='user_info': res=self.get_user_info(request,user_id) else: res='' if res: return success(res) return error(400, '加载失败')
def get(self, request, blog_id, type): # 获取博客信息或者评论 if type == 'user_info': res = self.get_user_info(request, blog_id) elif type == 'content': res = self.get_content(request, blog_id) elif type == 'comment': res = self.get_comment(request, blog_id) elif type == 'user_detail': res = self.get_user_detail(request, blog_id) elif type == 'blog_list': res = self.get_blog_list(request, blog_id) else: res = '' if res: return success(res) return error(400, '意外的错误')
def get(self, request): keyword = request.GET.get('keyword') category = request.GET.get('category') if category == 'all': category = '' sort = request.GET.get('sort') # 类别搜索和排序方式 try: if keyword and category and sort: blog_ids = Category.objects.filter(category=category)[0].blog.filter(title__icontains=keyword, is_show=1, status=1).extra( tables=['blog_blog', 'blog_bloghistory'], where=['blog_blog.id=blog_bloghistory.blog_id'], order_by=['-blog_bloghistory.{}'.format(sort)]).values_list('id', flat=True) return success(list(blog_ids)) # 类别搜索 elif keyword and category: blog_ids = Category.objects.filter(category=category)[0].blog.filter(title__icontains=keyword, is_show=1, status=1).extra( tables=['blog_blog', 'blog_bloghistory'], where=['blog_blog.id=blog_bloghistory.blog_id'], order_by=['-blog_bloghistory.score']).values_list('id', flat=True) return success(list(blog_ids)) # 类别搜索 elif keyword and sort: blog_ids = Blog.objects.filter(title__icontains=keyword, is_show=1, status=1).extra( tables=['blog_blog', 'blog_bloghistory'], where=['blog_blog.id=blog_bloghistory.blog_id'], order_by=['-blog_bloghistory.{}'.format(sort)]).values_list('id', flat=True) return success(list(blog_ids)) # 关键字搜索 elif keyword: # 查询所有符合条件的博客id按分数降序排列 高->底 blog_ids = Blog.objects.filter(title__icontains=keyword, is_show=1, status=1).extra( tables=['blog_blog', 'blog_bloghistory'], where=['blog_blog.id=blog_bloghistory.blog_id'], order_by=['-blog_bloghistory.score']).values_list('id', flat=True) print(blog_ids) return success(list(blog_ids)) else: raise except: return error(400, '参数错误')
def patch(self, request, blog_id, type): """ 点赞 """ try: if type == 'praise': blog = request.blog user = request.my_user master = User.objects.get(blog=blog) if BrowserHistory.objects.filter(blog=blog, user=user, praise=False).update(praise=True): # 更新点赞数 UserHistory.objects.filter(user=master).update(praise=F('praise') + 1) update.update_praise(blog_id) return success() elif BrowserHistory.objects.filter(blog=blog, user=user, praise=True).update(praise=False): UserHistory.objects.filter(user=master).update(praise=F('praise') - 1) update.update_praise(blog_id, -1) return success(code=201) except: pass return error(400, '意外的错误')