def create_comment(comment_in: CommentIn): author, post = get_user(user_name=comment_in.author), get_post( post_id=comment_in.post_id) if author and post: comment = Comment(author=comment_in.author, post_id=comment_in.post_id, comment=comment_in.comment) try: comment.save() return comment except: return False
def delete_comments_by_author(author: str): user = User.objects(user_name=author) if user: comments = Comment.objects(author=author) for comment in comments: comment.delete() return True return False
def delete_comments_by_post(post_id: str): post = Post.objects(pk=post_id) if post: comments = Comment.objects(post_id=post_id) for comment in comments: comment.delete() return True return False
def submit_comment(post_id): req_data = get_post_data_from_req(request) comment = Comment.submit_comment(current_user, post_id, req_data.get('body')) if isinstance(comment, basestring): return transitify({"error": comment}) app_state = {"success": "commented successfully"} app_state.update(handle_asks(post_id, ["actions"])) return transitify(app_state)
def submit_comment(post_id): req_data = get_post_data_from_req(request) comment = Comment.submit_comment(current_user, post_id, req_data.get('body')) if isinstance(comment, str): return transitify({"error": comment}) app_state = {"success": "commented successfully"} app_state.update(handle_asks(post_id, ["actions"])) return transitify(app_state)
def handle_comment(self, str_comment, str_post_id, str_user_id): if not str_comment: # if comment is empty render error message return self.render_front(str_user_id, str_post_id, "Your Comment may not be empty") else: int_user_id = int(str_user_id) user = User.User.by_id(int_user_id) str_username = user.name int_post_id = int(str_post_id) db_comment = Comment(text=str_comment, user_id=int_user_id, post_id=int_post_id, username=str_username) db_comment.put() return self.delayed_render_front(str_user_id)
def handle_edit_comment(self, str_new_comment, str_comment_id, str_post_id, str_user_id): if not str_new_comment: return self.render_front(str_user_id, str_post_id, "Your Comment may not be empty") else: # get original comment and adjust comment = Comment.by_id(int(str_comment_id)) comment.text = str_new_comment comment.put() return self.delayed_render_front(str_user_id)
async def api_comment_create(request): """ 创建评论API函数 :param request: 请求 :return: 返回响应消息 """ # 只有登录用户才能发表评论 user = request.__user__ if user is None: return data_error(u'请先登录') # 限制每10秒只能发送一条评论 comments = await Comment.find_all('user_id=? and created_at > ?', [user.id, time.time() - 10.0]) if len(comments) > 0: return data_error(u'评论过于频繁(10秒后再试)') # 获取评论内容 request_data = RequestData(request) if not await request_data.json_load(): return data_error(u'非法数据格式, 请使用JSON格式') # 检查评论内容 content = request_data.content if not content or not content.strip(): return data_error(u'评论内容不能为空') blog_id = request.match_info['blog_id'] blog = await Blog.find(blog_id) if blog is None: return data_error(u'评论的博客不存在') # 检查评论目标人名称,如果为NULL,表示对博客直接评论 target_user_name = request_data.target_name if not target_user_name or not target_user_name.strip(): target_user_name = blog.user_name # 检查评论目标人id,如果为NULL,表示对博客直接评论 target_user_id = request_data.target_id if not target_user_id or not target_user_id.strip(): target_user_id = blog.user_id comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, target_user_id=target_user_id, target_user_name=target_user_name, content=content.strip()) await comment.save() return comment
async def api_comment_create(request): """ 创建评论API函数 :param request: 请求 :return: 返回响应消息 """ # 只有登录用户才能发表评论 user = request.__user__ if user is None: return data_error(u'请先登录') # 限制每10秒只能发送一条评论 comments = await Comment.find_all('user_id=? and created_at > ?', [user.id, time.time() - 10.0]) if len(comments) > 0: return data_error(u'评论过于频繁(10秒后再试)') # 获取评论内容 ct = request.content_type.lower() if ct.startswith('application/json'): params = await request.json() if not isinstance(params, dict): return data_error() else: return data_error() # 检查评论内容 content = None if 'content' in params: content = params['content'] if not content or not content.strip(): return data_error(u'评论内容不能为空') blog_id = request.match_info['blog_id'] blog = await Blog.find(blog_id) if blog is None: return data_error(u'评论的博客不存在') comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip()) await comment.save() return comment
def show_post(post_id): form = CommentForm() requested_post = BlogPost.query.get(post_id) if form.validate_on_submit(): if not current_user.is_authenticated: flash("You need to login or register to comment.") return redirect(url_for("login")) new_comment = Comment(text=form.comment_text.data, comment_author=current_user, parent_post=requested_post) db_session.add(new_comment) db_session.commit() return render_template("post.html", post=requested_post, form=form, current_user=current_user)
def get_comment(comment_id: str): try: comment = Comment.objects(pk=comment_id) return comment except: return False
def handle_delete_comment(self, str_comment_id, str_user_id): # get original comment and delete comment = Comment.by_id(int(str_comment_id)) comment.delete() return self.delayed_render_front(str_user_id)
class CommentRating(Model): name = CharField() movie_id = IntegerField() rating = FloatField() count = IntegerField() class Meta: database = db ratings = {} # 对全部评论循环一遍 for comment in Comment.select(): # 只有打分类型为int的数据才处理 if isinstance(comment.rating, int): # 如果是第一次遇到这部电影,做一些初始化操作 if comment.movieid not in ratings: try: movie_name = Movie.get(Movie.id == comment.movieid).name ratings[comment.movieid] = { 'name': movie_name, 'rating': int(comment.rating), 'count': 1 } print('*' * 80) print(movie_name) except Exception as e: # 如果出现问题可以打印出来或者做其他异常处理
def get_comments_by_author(author: str): comments = Comment.objects(author=author) return comments
def delete_comment(comment_id: str): comment = Comment.objects(pk=comment_id) if comment: comment.delete() return True return False
def get_comments_by_post(post_id: str): try: comments = Comment.objects(post_id=post_id) return comments except: return False