def commentadd(): # 接收数据:新闻编号,评论内容 dict1 = request.form news_id = dict1.get('news_id') msg = dict1.get('msg') # 验证 if not all([news_id, msg]): return jsonify(result=1) news = NewsInfo.query.get(news_id) # 判断新闻对象是否存在 if news is None: return jsonify(result=2) # 判断登陆状态 if 'user_id' not in session: return jsonify(result=3) # 评论量+1 news.comment_count += 1 # 保存 comment = NewsComment() comment.news_id = int(news_id) comment.user_id = session['user_id'] comment.msg = msg db.session.add(comment) db.session.commit() # 响应 return jsonify(result=4, comment_count=news.comment_count)
def commentadd(): dict1 = request.form news_id = dict1.get('news_id') msg = dict1.get('msg') if not all([news_id, msg]): return jsonify(result=2) news = NewsInfo.query.get(news_id) if news is None: return jsonify(result=3) if 'user_id' not in session: return jsonify(result=4) user_id = session['user_id'] comment = NewsComment() comment.news_id = int(news_id) comment.user_id = user_id comment.msg = msg news.comment_count += 1 db.session.add(comment) db.session.commit() return jsonify(result=1, comment_count=news.comment_count)
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)
def comment(): user_id = session.get('user_id') or 0 # get请求做数据返回操作 if request.method == 'GET': news = NewsInfo.query.get(int(request.args.get('news_id', '0'))) comment_list = [] # 剔除评论的回复 for comment in news.comments.filter_by(comment_id=None).order_by( NewsComment.create_time.desc()): # 查询评论的回复数据 reply_list = [] for comment_reply in comment.comments: reply_list.append({ 'nick_name': comment_reply.user.nick_name, 'msg': comment_reply.msg }) comment_list.append({ 'is_like': 1 if comment.id in [ int(comment_id) for comment_id in current_app.redis_client.lrange( 'likecomments%d' % user_id, 0, -1) ] else None, 'id': comment.id, 'user_avatar_url': comment.user.avatar_url, 'nick_name': comment.user.nick_name, 'msg': comment.msg, # strftime可以将日期格式化 'create_time': comment.create_time.strftime('%Y-%m-%d %H:%M:%S'), 'reply_list': reply_list, 'like_count': comment.like_count }) return jsonify(comment_list=comment_list, comment_count=news.comment_count) # post请求做添加评论对象操作 elif request.method == 'POST': dict1 = request.form news_id = int(dict1.get('news_id')) msg = dict1.get('msg') # 验证 # 1. 如果数据不完整显示提示消息 if not all((news_id, msg)): return jsonify(error_info='数据不完整') news = NewsInfo.query.get(news_id) user = UserInfo.query.get(session['user_id']) # 2.如果非正常请求,记录客户端ip if not all((user, news)): current_app.logger_xjzx.info( 'from---/comment--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.msg = msg # 操作成功后,新闻的评论数加1 news.comment_count += 1 try: db.session.add(comment) db.session.commit() return jsonify(success_info='操作成功') except: return jsonify(error_info='服务器出错')