from flask import Blueprint, abort, render_template, request, session, jsonify from module.comment import Comment from module.credit import Credit from module.article import Article from module.thumb import Thumb from module.user import Users comment = Blueprint("comment",__name__) @comment.route('/comment',methods = ['POST']) def add_comment(): articleid = request.form.get('articleid') content = request.form.get('content') userid = session.get('userid') ipaddr = request.remote_addr # 直接获取ip地址 # 对得到的comment进行校验,这里只进行最简单的字数校验 if len(content) < 5 or len(content) > 1000: return 'content-invalid' # 校验结束,进行各种操作 try: # 更新积分详情表 积分变化原因:category 1评论comment 2注册enroll 3登录login 4投稿submission 5文章阅读read Credit().insert_detail(category='1', target=userid, credit=2) # 更新个人积分 Users().update_credit(2) # 评论数量增加 Article().replaycount_add(articleid) Comment().add_comment(articleid=articleid,content=content,ipaddr=ipaddr) return 'add-pass' except: return 'add-fail' # 评论点赞 @comment.route('/upcomment',methods = ['post']) def up_comment(): commentid = request.form.get('commentid') # 通过post从前端页面那边拿取数据 articleid = request.form.get('articleid') if session.get('islogin') is None: # 如果没有登录 return 'not-login' else: try: Comment().update_add_agreecount(commentid=commentid) Thumb().insert_up_detail(articleid=articleid,commentid=commentid) return 'up-pass' except: return 'up-fail' # 评论取消点赞 @comment.route('/cancelup',methods = ['post']) def canel_comment(): commentid = request.form.get('commentid') # 通过post从前端页面那边拿取数据 articleid = request.form.get('articleid') if session.get('islogin') is None: # 如果没有登录 return 'not-login' else: try: Comment().update_sub_agreecount(commentid=commentid) Thumb().insert_sub_up_detail(commentid=commentid) return 'cancel-pass' except: return 'cancel-fail' # 评论反对 @comment.route('/downcomment',methods = ['post']) def down_comment(): commentid = request.form.get('commentid') # 通过post从前端页面那边拿取数据 articleid = request.form.get('articleid') if session.get('islogin') is None: # 如果没有登录 return 'not-login' else: try: Comment().update_add_opposecount(commentid=commentid) Thumb().insert_down_detail(articleid=articleid, commentid=commentid) return 'down-pass' except: return 'down-fail' # 评论取消反对 @comment.route('/canceldown',methods = ['post']) def down_cancel(): commentid = request.form.get('commentid') # 通过post从前端页面那边拿取数据 articleid = request.form.get('articleid') if session.get('islogin') is None: # 如果没有登录 return 'not-login' else: try: Comment().update_sub_opposecount(commentid=commentid) Thumb().insert_sub_down_detail(commentid=commentid) return 'cancel-pass' except: return 'cancel-fail' # 评论隐藏 @comment.route('/hidecomment',methods = ['post']) def hide_comment(): commentid = request.form.get('commentid') # 通过post从前端页面那边拿取数据 if session.get('islogin') is None: # 如果没有登录 return 'not-login' else: try: Comment().update_hide_comment(commentid=commentid) return 'hide-pass' except: return 'hide-fail' # 删除评论 @Comment.route('/admin/comment/delete/', methods=['GET']) def delete_comment(commentid): comemnt = Comment() try: # 查找评论 userid = session['userid'] result = comemnt.find_coment_by_id(commentid,userid) if result is None: return jsonify({"success": False, "message": "该评论不属于您"}) # 删除人员 comemnt.do_delete_comment(commentid) return jsonify({"success": True, "message": "删除成功"}) except Exception as e: return jsonify({"success": False, "message": str(e)})