def _generate_comments( _count: int, reviewed: bool = True, from_admin: bool = False, is_replied: bool = False ): """ 生成评论数据,内部调用 :param _count: 生成的评论数量 :param reviewed: default=True,默认是已审核评论 :param from_admin: default=False,默认不是管理员评论 :param is_replied: default=False,默认不是回复评论 :return: None """ comments_count = Comment.query.count() posts_count = Post.query.count() for i in range(_count): with db.auto_commit(): comment = Comment() if not from_admin: comment.author = cls.FAKER.name() comment.email = cls.FAKER.email() comment.site = cls.FAKER.url() else: comment.author = Admin.query.get(1).nickname comment.email = "*****@*****.**" comment.site = "localhost:5000" comment.content = cls.FAKER.text(random.randint(40, 200)) comment.from_admin = from_admin comment.reviewed = reviewed if is_replied: comment.replied = Comment.query.get(random.randint(1, comments_count)) comment.post = Post.query.get(random.randint(1, posts_count)) db.session.add(comment)
def post_comment(): form = CommentForm() pid = 1 source = 'my' if form.validate_on_submit(): pid = form.pid.data p = Project.query.filter_by(id=pid).first_or_404() source = form.source.data c = Comment() c.project = p c.author_id = user_manager.current_user.get_id() c.content = form.content.data # c.timestamp = time.time() with safe_session(db): db.session.add(c) url = 'investor.show_project' if source == 'my': url = 'investor.project_list_mine_detail' return redirect(url_for(url, pid=pid))
def make_comment(): '''评论单本小说, 需要登录''' try: novel = Novel.query.get(novel_id) except Exception as e: current_app.logger.debug(e) return jsonify(re_code=RET.DBERR, msg='查询小说失败') if not novel: return jsonify(re_code=RET.NODATA, msg='小说不存在') if not g.user_id: return jsonify(re_code=RET.SESSIONERR, msg='用户未登录') comment = request.json.get('comment') user_id = g.user_id timestamp = datetime.utcnow() comment = Comment() comment.timestamp = timestamp comment.content = comment comment.audienc_id = user_id comment.novel_id = novel_id try: db.session.add(comment) db.session.commit() except Exception as e: current_app.logger.debug(e) db.session.rollback() return jsonify(re_code=RET.DBERR, msg='评论发表失败') else: return jsonify(re_code=RET.OK, msg='评论发表成功')
def post(self): """发表评论(需登录)""" form = request.form mid = form.get('movieId', '') movie = Movie.query.get(mid) if movie is None: return {'message': '电影不存在'}, 233 try: rating = int(form.get('rating', '')) if rating < 0 or rating > 5: return {'message': '评分非法'}, 233 except ValueError: return {'message': '评分非法'}, 233 content = form.get('content', '').strip() if len(content) == 0: return {'message': '评论内容不能为空'}, 233 comment = Comment() comment.id = UUID() comment.rating = rating comment.content = content comment.movieId = mid comment.username = current_user.id db.session.add(comment) total = movie.ratingNum * movie.rating movie.ratingNum += 1 movie.rating = (total + rating) / movie.ratingNum db.session.commit() return {'message': '评论成功', 'id': comment.id}, 200
def new_comment(): from app.models import User, Comment, News, CommentLike, Category from app import db user_id = session.get("user_id") user = User.query.get(user_id) print(user.to_dict()) if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") # 1. 取到请求参数 news_id = request.json.get("news_id") comment_content = request.json.get("comment") parent_id = request.json.get("parent_id") print(news_id) print(comment_content) print(parent_id) # 2. 判断参数 if not all([news_id, comment_content]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: news_id = int(news_id) if parent_id: parent_id = int(parent_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 查询新闻,并判断新闻是否存在 try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库查询错误") if not news: return jsonify(errno=RET.NODATA, errmsg="未查询到新闻数据") # 3. 初始化一个评论模型,并且赋值 comment = Comment() comment.user_id = user.id comment.news_id = news_id comment.content = comment_content if parent_id: comment.parent_id = parent_id # 添加到数据库 print(comment) try: db.session.add(comment) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.OK, errmsg="OK", data=comment.to_dict())
def comment(): form = CommentForm() if form.validate_on_submit(): comment = Comment() comment.content = form.content.data comment.user = current_user db.session.add(comment) db.session.commit() print(current_user.blogs) return redirect(url_for('.content', id=current_user.blogs.id)) return render_template("main/comment.html", form=form)
def create_photo_collection_comment(): collection_id = int(request.form.get("comment_owner_id")) photo_collection = PhotoCollection.query.get(collection_id) collection_comment = PhotoCollectionComment() comment = Comment() comment.user_id = current_user.id comment.content = request.form.get("content") comment.date = datetime.now() collection_comment.comment = comment collection_comment.photo_collection = photo_collection db.session.add(comment) db.session.add(collection_comment) db.session.commit() return redirect(url_for("blog", photographer_id=photo_collection.photographer_id))
def post(self, param=None): data = request.get_json(force=True) comment = Comment() user = User.query.get(current_user_id()) comment.user = user comment.content = data.get('content', None) comment.date = datetime.datetime.now() post = Post.query.get(param) post.comments.append(comment) db.session.commit() return jsonify({ 'message': 'Comentario creado exitosamente', 'redirect': f'posts/view/{post.id}' })
def post(self): args = comment_post_parser.parse_args() user = User.verify_auth_token(args['token']) if not user: raise InvalidToken() dup_comment = Comment.query.filter_by(content=args['content']).first() if dup_comment and dup_comment.user_id == user.id: raise DuplicateInfo('评论重复') comment = Comment() comment.user_id = user.id comment.article_id = args['article_id'] comment.content = args['content'] db.session.add(comment) db.session.commit() return {'message': 'ok'}, 200
def add_news_comment(): """添加评论""" user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登陆") # 获取参数 data_dict = request.json news_id = data_dict.get("news_id") comment_str = data_dict.get("comment") parent_id = data_dict.get("parent_id") if not all([news_id, comment_str]): jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: news = News.query.get(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据查询失败") if not news: return jsonify(errno=RET.NODATA, errmsg="该新闻不存在") # 初始化模型,保存数据 comment = Comment() comment.user_id = user.id comment.news_id = news_id comment.content = comment_str if parent_id: comment.parent_id = parent_id try: db.session.add(comment) db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存评论数据失败") # 返回响应 return jsonify(errno=RET.OK, errmsg="评论成功", data=comment.to_dict())
def article(id): comment_form = CommentsCreateForm() article = db.session.query(Articles).filter(Articles.id == id).first() creator = db.session.query(User).filter(User.id == article.creator).first() comments = db.session.query(Comment).filter( Comment.article_id == id).all() # список кометариев if comment_form.validate_on_submit(): comment = Comment() comment.comment_creator = current_user.id comment.article_id = id comment.content = comment_form.text.data current_user.comment.append(comment) db.session.commit() return render_template("news_template.html", article=article, creator=creator, comments=comments, form=comment_form) return render_template("news_template.html", article=article, creator=creator, comments=comments, form=comment_form)