def comment_add(): # 接收 msg = request.form.get('msg') news_id = request.form.get('news_id') # 如果是评论则不传递此数据,如果是回复,则传递此数据 comment_id = int(request.form.get('comment_id', 0)) # 验证 # 1.非空 if not all([msg, news_id]): return jsonify(result=1) # 2.是否登录 if 'user_id' not in session: return jsonify(result=2) # 处理:添加 comment = NewsComment() comment.msg = msg comment.news_id = int(news_id) comment.user_id = session.get('user_id') # 判断是否有评论编号,如果有则添加 # 添加评论时,不传递此参数 # 添加回复时,传递此参数 if comment_id > 0: comment.comment_id = comment_id db.session.add(comment) db.session.commit() # 响应 return jsonify(result=3)
def comment_add(): msg = request.form.get('msg') news_id = request.form.get('news_id') comment_id = request.form.get('comment_id', '0') if 'user_id' not in session: return jsonify(result=1) if not all([msg, news_id]): return jsonify(result=2) try: news_id = int(news_id) except: return jsonify(result=3) comment = NewsComment() comment.msg = msg comment.news_id = news_id comment.user_id = session.get('user_id') if comment_id != '0': comment.comment_id = int(comment_id) db.session.add(comment) news = NewsInfo.query.get(news_id) news.comment_count += 1 try: db.session.commit() except Exception as e: current_app.logger_xjzx.error('添加评论数据库错误') return jsonify(result=5) return jsonify(result=4)
def comment_reply(): # 获取登录用户id user = UserInfo.query.get( session['user_id']) if session.get('user_id') else None # 未登录返回登录 if not user: return jsonify(login=1) # 获取数据 dict1 = request.form news = NewsInfo.query.get(int(dict1.get('news_id'))) newscomment = NewsComment.query.get(int(dict1.get('comment_id'))) msg = dict1.get('msg') # 如果没有新闻对象或者评论对象,视为非正常请求 if not all((news, newscomment)): current_app.logger_xjzx.info( ' from---/comment_comments has a Non-normal request-------' + request.remote_addr) return jsonify(Non_normal='bad_request') # 添加评论的评论对象 comment = NewsComment() comment.news_id = news.id comment.user_id = user.id comment.comment_id = newscomment.id comment.msg = msg # 评论后news的评论数+1 news.comment_count += 1 db.session.add(comment) db.session.commit() return jsonify(success_info='评论成功')
def commentback(): news_id = request.form.get('news_id') msg = request.form.get('msg') comment_id = request.form.get('comment_id') if not all([news_id, msg, comment_id]): return jsonify(result=1) if 'user_id' not in session: return jsonify(result=2) user_id = session['user_id'] comment = NewsComment() comment.news_id = int(news_id) comment.user_id = user_id comment.comment_id = comment_id comment.msg = msg db.session.add(comment) db.session.commit() return jsonify(result=3)
def commentback(comment_id): # 用户user_id回复了评论comment_id,内容为msg msg = request.form.get('msg') news_id = int(request.form.get('news_id')) if not all([msg]): return jsonify(result=1) if 'user_id' not in session: return jsonify(result=2) user_id = session['user_id'] # 创建评论对象 comment = NewsComment() comment.news_id = news_id comment.user_id = user_id comment.comment_id = comment_id comment.msg = msg # 提交数据库 db.session.add(comment) db.session.commit() return jsonify(result=3)
def comment_add(): # 接收 msg = request.form.get('msg') news_id = request.form.get('news_id') # 如果进行评论则无此值,如果进行回复则有此值 comment_id = request.form.get('comment_id', '0') # 验证 # 判断用户是否登录 if 'user_id' not in session: return jsonify(result=1) if not all([msg, news_id]): return jsonify(result=2) try: news_id = int(news_id) except: return jsonify(result=3) # 处理 comment = NewsComment() comment.msg = msg comment.news_id = news_id comment.user_id = session.get('user_id') if comment_id != '0': # 回复,需要设置评论的编号 comment.comment_id = int(comment_id) db.session.add(comment) # 让新闻的评论量+1 news = NewsInfo.query.get(news_id) news.comment_count += 1 try: db.session.commit() except Exception as e: # 写日志 current_app.logger_xjzx.error('添加评论连接数据库出错') return jsonify(result=5) # 响应 return jsonify(result=4)