def add_comment(): if flask.request.method == 'GET': post_id = flask.request.args.get('post_id', type=int) comment_id = flask.request.args.get('comment_id', type=int) context = {'post': PostModel.query.get(post_id)} if comment_id: context['origin_comment'] = CommentModel.query.get(comment_id) return flask.render_template('front/front_addcomment.html', **context) else: form = AddCommentForm(flask.request.form) if form.validate(): post_id = form.post_id.data content = form.content.data comment_id = form.comment_id.data comment_model = CommentModel(content=content) post_model = PostModel.query.get(post_id) comment_model.post = post_model comment_model.author = flask.g.front_user if comment_id: origin_comment = CommentModel.query.get(comment_id) comment_model.origin_comment = origin_comment db.session.add(comment_model) db.session.commit() return xtjson.json_result() else: return xtjson.json_params_error(message=form.get_error())
def add_comment(): if flask.request.method == 'GET': post_id = flask.request.args.get('post_id') post_model = PostModel.query.get(post_id) # db.session.close() return flask.render_template('front/front_addcoment.html', post=post_model) else: if flask.g.front_user.points < settings.COMMENT_ALLOW_POINTS: message = u'你的积分是%s,少于评论需求的%s积分,去发布帖子、写评论和做任务' \ u'挣积分吧~' % (flask.g.front_user.points, settings.COMMENT_ALLOW_POINTS) return xtjson.json_params_error(message=message) form = AddCommentForm(flask.request.form) if form.validate(): post_id = form.post_id.data content = form.content.data comment_model = CommentModel(content=content) post_model = PostModel.query.get(post_id) comment_model.post = post_model comment_model.author = flask.g.front_user # 评论积分+1 comment_model.author.points += 1 db.session.add(comment_model) db.session.commit() # db.session.close() return xtjson.json_result() else: return xtjson.json_params_error(message=form.get_error())
def comment_reply(): form = AddReply(flask.request.form) if form.validate(): comment_id = form.comment_id.data reply = form.reply.data comment_model = CommentModel.query.get(comment_id) reply_model = CommentModel(content=reply) reply_model.author = flask.g.front_user reply_model.origin_comment = comment_model db.session.add(reply_model) db.session.commit() # db.session.close() return xtjson.json_result() else: return xtjson.json_method_error(message=u'bug!!')
def test(): # author = FrontUser.query.first() # board = BoardModel.query.first() # for x in xrange(0,100): # title = '帖子标题:%s' % x # content = '帖子内容:%s' % x # post_model = PostModel(title=title,content=content) # post_model.author = author # post_model.board = board # db.session.add(post_model) # db.session.commit() # return 'success' comment_model = CommentModel(content='xxx') comment_model.post = PostModel.query.first() comment_model.author = FrontUser.query.first() comment_model.origin_comment = CommentModel.query.first() db.session.add(comment_model) db.session.commit() return 'success'