def comment_like(): """ 评论点赞 :return: """ # 用户是否登陆 print(123132132132132) user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录') # 取到请求参数 comment_id = request.json.get('comment_id') news_id = request.json.get('news_id') action = request.json.get('action') # 判断参数 if not all([comment_id, news_id, action]): return jsonify(errno=RET.PARAMERR, errmsg='参数错误') if action not in ('add', 'remove'): return jsonify(errno=RET.PARAMERR, errmsg='参数错误') # 获取到要被点赞的评论模型 # action的状态,如果点赞,则查询后将用户id和评论id添加到数据库 # 点赞评论 # 更新点赞次数 # 查询评论数据 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.errnor(e) return jsonify(errno=RET.DBERR, errmsg='查询参数失败') if not comment: return jsonify(errno=RET.NODATA, errmsg='评论数据不存在') if action == 'add': comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) comment.like_count += 1 # 取消点赞评论,查询数据库,如果以点在,则删除点赞信息 # 更新点赞次数 else: comment_like = CommentLike.query.filter_by( user_id=user.id, comment_id=comment_id).first() if comment_like: db.session.delete(comment_like) # 减小点赞条数 comment.like_count -= 1 try: db.session.commit() except Exception as e: current_app.logger.errnor(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='操作失败') # 返回结果 return jsonify(errno=RET.OK, errmsmg='操作成功')
def comment_like(): '''评论点赞''' user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="User is not logged in!") # 获取参数 comment_id = request.json.get("comment_id") news_id = request.json.get('news_id') action = request.json.get('action') # 校验参数 if not all([comment_id, news_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="Parameter error!") if action not in ['add', 'remove']: return jsonify(errno=RET.PARAMERR, errmsg="Parameter error!") try: comment_id = int(comment_id) news_id = int(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='Parameter error!') try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="Data query error!") if not comment: return jsonify(errno=RET.NODATA, errmsg="No data is queried!") if action == 'add': comment_like_model = CommentLike.query.filter(CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if not comment_like_model: comment_like_model = CommentLike() comment_like_model.user_id = user.id comment_like_model.comment_id = comment.id # 数据库增加一条点赞数量 comment.like_count += 1 db.session.add(comment_like_model) else: # 取消点赞 comment_like_model = CommentLike.query.filter(CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if comment_like_model: db.session.delete(comment_like_model) # 数据库减少一条点赞记录 comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="Data save failed!") return jsonify(errno=RET.OK, errmsg="OK")
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") # 1. 取到请求参数 comment_id = request.json.get("comment_id") action = request.json.get("action") # 2. 判断参数 if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") if action not in ["add", "remove"]: return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comment_id = int(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 3. 获取到要被点赞的评论模型 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据查询错误") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论不存在") if action == "add": comment_like_model = CommentLike.query.filter(CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if not comment_like_model: # 点赞评论 comment_like_model = CommentLike() comment_like_model.user_id = user.id comment_like_model.comment_id = comment.id db.session.add(comment_like_model) # 更新点赞次数 comment.like_count += 1 else: # 取消点赞评论 comment_like_model = CommentLike.query.filter(CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if comment_like_model: db.session.delete(comment_like_model) # 更新点赞次数 comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库操作失败") return jsonify(errno=RET.OK, errmsg="OK")
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="请登录") comment_id = request.json.get("comment_id") news_id = request.json.get("news_id") action = request.json.get("action") if not all([comment_id, news_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") comment = Comment.query.get(comment_id) if action == "add": comment_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) comment.like_count += 1 else: comment_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() if comment_like: db.session.delete(comment_like) comment.like_count -= 1 db.session.commit() return jsonify(errno=RET.OK, errmsg="点赞成功")
def comment_like(): ''' 评论点赞 :return: ''' # 判断用户用户是否登录 user = g.user if not user: return jsonify(error=RET.PARAMERR, errmsg='用户未登录') # 获取参数 comment_id = request.json.get('comment_id') news_id = request.json.get('news_id') action = request.json.get('action') # 判断参数 if not all([comment_id, news_blu, action]): return jsonify(error=RET.PARAMERR, errmsg='参数错误') if action not in ('add', 'remove'): return jsonify(error=RET.PARAMERR) # 查询评论数据 # 查询评论数据 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.DBERR, errmsg='数据查询错误') if not comment: return jsonify(error=RET.NODATA, errmsg='评论地址不存在') if action == "add": comment_like_model = CommentLike.query.filter_by( comment_id=comment_id, user_id=g.user.id).first() if not comment_like_model: comment_like_model = CommentLike() comment_like_model.comment_id = comment_id comment_like_model.user_id = g.user.id db.session.add(comment_like_model) # 增加点赞条数 comment.like_count += 1 else: comment_like_model = CommentLike.query.filter_by( comment_id=comment_id, user_id=g.user.id).first() if comment_like_model: db.session.delete(comment_like_model) # 减少点赞条数 comment.like_count -= 1 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(error=RET.DBERR, errmsg='操作失败') return jsonify(error=RET.OK, errmsg='操作成功')
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="请登陆") comment_id = request.json.get("comment_id") news_id = request.json.get("news_id") # 判断当前用户的动作,到底是想点赞,还是想取消点赞 action = request.json.get("action") comment = Comment.query.get(comment_id) if action == "add": # 用户想点赞 comment_like = CommentLike.query.filter(CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() # 查询出来之后,需要判断当前这条评论用户是否已经点赞,如果查询出来为空,说明之前没有点赞,那么就可以点赞 if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) # 因为点赞了,所以需要把当前的评论进行加1 comment.like_count += 1 else: # 取消点赞的动作 comment_like = CommentLike.query.filter(CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() if comment_like: db.session.delete(comment_like) comment.like_count -= 1 db.session.commit() return jsonify(errno=RET.OK, errmsg="点赞成功")
def comment_like(): '''评论和点赞''' # 1 获取登录的信息 user = g.user if not user: return jsonify(errno=response_code.RET.SESSIONERR, errmsg='用户未登录') # 2 获取参数 comment_id = request.json.get('comment_id') action = request.json.get('action') # 3 校验参数 if not all([comment_id, action]): return jsonify(errno=response_code.RET.PARAMERR, errmsg='缺少参数') if action not in ['add', 'remove']: return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数错误') # 4 查询要点赞的评论是否存在 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数错误') if not comment: return jsonify(errno=response_code.RET.NODATA, errmsg='评论不存在') # 5 点赞和取消点赞 # 查询要点赞的记录是否存在 comment_like_model = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() if action == 'add': # 点赞 if not comment_like_model: comment_like_model = CommentLike() comment_like_model.comment_id = comment_id comment_like_model.user_id = user.id # 提交新闻记录 db.session.add(comment_like_model) # 累计点赞量 comment.like_count += 1 else: # 取消点赞 if comment_like_model: # 删除该记录 db.session.delete(comment_like_model) # 减少点赞量 comment.like_count -= 1 # 将点赞和取消点赞的数据同步到数据库 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) # 6 响应点赞和取消点赞的结果 return jsonify(errno=response_code.RET.OK, errmsg='Ok')
def set_comment_like(): """评论点赞""" if not g.user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") comment_id = request.json.get("comment_id") news_id = request.json.get("news_id") action = request.json.get("action") if not all([news_id, comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数不足") if action not in ("add", "remove"): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comment_id = int(comment_id) news_id = int(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 查询评论数据 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据失败") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论数据不存在") if action == "add": comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=g.user.id).first() if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = g.user.id db.session.add(comment_like) comment.like_count += 1 # 删除点赞 else: comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=g.user.id).first() if comment_like: db.session.delete(comment_like) comment.like_count -= 1 try: db.session.commit() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="操作失败") return jsonify(errno=RET.OK, errmsg="操作成功")
def comment_like(): user = g.user if not user: # 点赞用户必须登录 return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录') comment_id = request.json.get('comment_id') # 评论id news_id = request.json.get('news_id') # 新闻id action = request.json.get('action') # 点赞/取消点赞 # 根据评论id获取要点赞的评论 comment = Comment.query.get(comment_id) # 查询当前评论的点赞信息, 根据评论id和用户id查询 comment_like = CommentLike.query.filter(CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() if action == 'add': # 点赞操作 if not comment_like: comment_like = CommentLike() # 实例化点赞对象 comment_like.comment_id = comment_id # 评论id comment_like.user_id = user.id # 用户id db.session.add(comment_like) # 添加到会话 comment.like_count += 1 # 点赞数 + 1 else: # 取消点赞操作 if comment_like: db.session.delete(comment_like) # 删除点赞 comment.like_count -= 1 # 点赞数 - 1 db.session.commit() return jsonify(errno=RET.OK, errmsg='操作成功')
def show_like(): """ 用户点赞功能 :return: """ comment_action = request.json comment_id = comment_action.get('comment_id') action = comment_action.get('action') if g.user: try: comment = Comment.query.get(comment_id) if action == 'add': comment.like_count += 1 comment_like = CommentLike() comment_like.user_id = g.user.id comment_like.comment_id = comment_id g.user.comment_likes.append(comment_like) db.session.commit() else: comment_like = CommentLike.query.filter(CommentLike.user_id == g.user.id, CommentLike.comment_id == comment_id).first() db.session.delete(comment_like) comment.like_count -= 1 db.session.commit() return jsonify(errno=RET.OK, errmsg='操作成功') except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=error_map[RET.DBERR]) else: return jsonify(errno=RET.SESSIONERR, errmsg=error_map[RET.SESSIONERR])
def like_comment(): """用户点赞评论""" # 获取当前登录的用户信息 user = g.user if not user: return jsonify(errno=response_code.RET.SESSIONERR, errmsg='您还没登录') # 用户登录了 # 接收参数 comment_id = request.json.get('comment_id') action = request.json.get('action') # 校验参数 try: comment_id = int(comment_id) except Exception as e: logging.error(e) return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数错误') if action not in ['add', 'remove']: return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数错误') # 根据传入的comment_id 查看评论是否存在 try: comment = Comment.query.filter(Comment.id == comment_id).first() except Exception as e: logging.error(e) return jsonify(errno=response_code.RET.DBERR, errmsg='查询评论出错') if not comment: return jsonify(errno=response_code.RET.DBERR, errmsg='评论不存在') try: # 查看用户的赞是否存在 comment_like_model = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() except Exception as e: return jsonify(errno=response_code.RET.DBERR, errmsg='查询点赞出错') if action == 'add': if not comment_like_model: user_comment_like = CommentLike() user_comment_like.comment_id = comment_id, user_comment_like.user_id = user.id db.session.add(user_comment_like) # 累加点赞 comment.like_count += 1 else: if comment_like_model: db.session.delete(comment_like_model) # 减少点赞量 comment.like_count -= 1 # 同步数据库 try: db.session.commit() except Exception as e: logging.error(e) db.session.rollback() return jsonify(errno=response_code.RET.DBERR, errmsg='点赞失败') if action == 'add': return jsonify(errno=response_code.RET.OK, errmsg='点赞成功') if action == 'remove': return jsonify(errno=response_code.RET.OK, errmsg='取消成功')
def comment_like(): """新闻评论点赞""" user = g.user if not user: return jsonify(errno=response_code.RET.SESSIONERR,errmsg= '用户未登录') """接受参数""" comment_id = request.json.get('comment_id') action = request.json.get('action') # 校验参数 if not all([comment_id,action]): return jsonify(errno=response_code.RET.PARAMERR,errmsg= '缺少参数') if action not in ['add','remove']: return jsonify(errno=response_code.RET.PARAMERR,errmsg= '参数错误') # 根据传入的comment_id 查询点赞的评论 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=response_code.RET.DBERR, errmsg='查询评论失败') if not comment: return jsonify(errno = response_code.RET.NODATA, errmsg="评论不存在") # 查询要点赞的评论是否存在 try: comment_like_model = CommentLike.query.filter(CommentLike.comment_id == comment_id,CommentLike.user_id == user.id).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=response_code.RET.DBERR,errmsg= '查询点赞失败') # 5点赞和取消点赞 if action == 'add': if not comment_like_model: comment_like_model = CommentLike() comment_like_model.user_id = user.id comment_like_model.comment_id = comment_id # db.session.add(comment_like_model) db.session.add(comment_like_model) # 累加点赞 comment.like_count += 1 else: if comment_like_model: # 将记录从书库中删除 # db.session.delete(comment_like_model) db.session.delete(comment_like_model) # 累加点赞 comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno = response_code.RET.DBERR, errmsg='操作失败') # 点赞和取消点赞 return jsonify(errno = response_code.RET.OK, errmsg='OK')
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR,errmsg="请登录") comment_id = request.json.get("comment_id") news_id = request.json.get("news_id") action = request.json.get("action") comment = Comment.query.get(comment_id) if action == "add": comment_like = CommentLike.query.filter_by(comment_id=comment_id,user_id = user.id).first() if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) comment.like_count += 1 db.session.commit() else: comment_like = CommentLike.query.filter_by(comment_id = comment_id,user_id = user.id).first() if comment_like: db.session.delete(comment_like) comment.like_count -= 1 db.session.commit() try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR,errmsg="操作失败") return jsonify(errno=RET.OK,errmsg="操作成功")
def comment_like(): user = g.user comment_id = request.json.get("comment_id") news_id = request.json.get("news_id") action = request.json.get("action") comment = Comment.query.get(comment_id) if not user: return jsonify(errno=RET.SESSIONERR, erromsg="请登陆") # 查询该评论是否点赞 comment_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() # 判断用户鼠标点击事件 if action == "add": if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) comment.like_count += 1 else: db.session.delete(comment_like) comment.like_count -= 1 db.session.commit() return jsonify(errno=RET.OK, errmsg="点赞成功")
def comment_like(): """用户对评论进行点赞或者取消点赞""" # 接收参数(comment_id,action) # action值可以是add或者remove user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") data_dict = request.json comment_id = data_dict.get("comment_id") action = data_dict.get("action") print(comment_id) print(action) # 根据comment_id去查询评论的信息(如果查不到,说明评论信息不存在) if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数不足") if action not in ("add", "remove"): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询失败") # 判断comment是否存在 if not comment: return jsonify(errno=RET.NODATA, errmsg="评论数据不存在") # 新建模型对象 commentLike = CommentLike() commentLike.comment_id = comment.id commentLike.user_id = user.id # 根据action执行对应的操作 if action == "add": # 点赞 db.session.add(commentLike) # 增加当前评论的点赞数量 comment.like_count += 1 else: # 取消点赞 commentLike = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() db.session.delete(commentLike) # 减去当前评论的点赞数量 comment.like_count -= 1 # 保存数据库操作 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存数据失败") # 返回响应结果, 评论点赞或者取消点赞成功 return jsonify(errno=RET.OK, errmsg="操作成功", like_count=comment.like_count)
def news_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录') # 获取参数 comment_id = request.json.get('comment_id') action = request.json.get('action') # 检查参数 if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg='参数缺失') try: comment_id = int(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='参数类型错误') if action not in ('add', 'remove'): return jsonify(errno=RET.PARAMERR, errmsg='参数格式错误') # 数据处理 comment = None try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据失败") if not comment: return jsonify(errno=RET.NODATA, errmsg='无评论对象') if action == 'add': # 添加点赞记录 comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=g.user.id).first() if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) # 增加点赞数 comment.like_count += 1 else: comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=g.user.id).first() if comment_like: # 删除点赞记录 db.session.delete(comment_like) # 将评论点赞数减一 comment.like_count -= 1 # 提交数据 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg='保存数据失败') # 返回响应 return jsonify(errno=RET.OK, errmsg='OK')
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录') comment_id = request.json.get('comment_id') news_id = request.json.get('news_id') action = request.json.get('action') if not all([comment_id, news_id, action]): return jsonify(errno=RET.PARAMERR, errmsg='参数错误') if action not in ['add', 'remove']: return jsonify(errno=RET.PARAMERR, errmsg='参数错误') try: comment_id = int(comment_id) news_id = int(news_id) except Exception as e: current_app.looger.error(e) return jsonify(errno=RET.PARAMERR, errmsg='参数错误') try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='数据查询错误') if not comment: return jsonify(errno=RET.NODATA, errmsg='评论不存在') if action == 'add': comment_like_model = CommentLike.query.filter( CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if not comment_like_model: comment_like_model = CommentLike() comment_like_model.user_id = user.id comment_like_model.comment_id = comment.id db.session.add(comment_like_model) comment.like_count += 1 else: comment_like_model = CommentLike.query.filter( CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if comment_like_model: db.session.delete(comment_like_model) if comment.like_count >= 1: comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='数据库操作失败') return jsonify(errno=RET.OK, errmsg='OK')
def comment_like(): """ 评论点赞 :return: json """ # 1.用户登录信息 user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") # 2.获取数据 json_data = request.json comment_id = json_data.get("comment_id") action = json_data.get("action") # 前端请求的动作,点赞或者是取消点赞 # 3.校验参数 if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") if action not in ["add", "remove"]: return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comment_id = int(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 4.找到需要点赞的评论模型 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据查询错误") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论不存在") # 5.创建评论点赞的模型 if action == "add": # 查询该评论是否已经被点赞 comment_like_model = CommentLike.query.filter(CommentLike.user_id == user.id, CommentLike.comment_id == comment_id).first() if not comment_like_model: # 点赞评论,生成点赞模型并存储进数据库 comment_like_model = CommentLike() comment_like_model.user_id = user.id comment_like_model.comment_id = comment_id db.session.add(comment_like_model) comment.like_count += 1 else: # 取消评论的点赞 comment_like_model = CommentLike.query.filter(CommentLike.user_id == user.id, CommentLike.comment_id == comment_id).first() if comment_like_model: db.session.delete(comment_like_model) comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库操作错误") return jsonify(errno=RET.OK, errmsg="操作成功")
def news_comment_like(): if not g.user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") resp_dict = request.get_json() comment_id = resp_dict.get("comment_id") action = resp_dict.get("action") news_id = resp_dict.get("news_id") if not all([comment_id, action, news_id]): return jsonify(errno=RET.DATAERR, errmsg="参数不足") if not action in ["add", "remove"]: return jsonify(errno=RET.DATAERR, errmsg="操作类型错误") # 查询点赞的评论 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg=" 数据库查询异常") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论不存在") if action == "add": # 判断用户是否已经点过赞了 comment_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == g.user.id).first() if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = g.user.id db.session.add(comment_like) # 将点赞数量 +1 comment.like_count += 1 # 取消点赞 else: # 判断是否点过赞 comment_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == g.user.id).first() if comment_like: db.session.delete(comment_like) # 将点赞数量-1操作 comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="保存数据库失败") else: return jsonify(errno=RET.OK, errmsg="操作成功")
def comment_like(): """ 点赞和取消点赞 :return: """ user = g.user current_app.logger.error(user.to_dict()) if not user: return jsonify(error=RET.SESSIONERR, errmsg="用户未登录") # 取请求参数 # news_id = request.json.get("news_id") comment_id = request.json.get("comment_id") action = request.json.get("action") current_app.logger.error(request.json) if not all([comment_id, action]): return jsonify(error=RET.PARAMERR, errmsg="参数错误") if action not in ["add", "remove"]: return jsonify(error=RET.PARAMERR, errmsg="参数错误") try: comments_id = int(comment_id) # news_id=int(news_id) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.PARAMERR, errmsg="参数错误") try: comment = Comment.query.get(comments_id) except Exception as e: current_app.logger.error(e) return jsonify(error=RET.DBERR, errmsg="数据查询错误") if not comment: return jsonify(error=RET.NODATA, errmsg="评论不存在") if action == "add": commentss_like = CommentLike.query.filter( CommentLike.comment_id == comments_id, CommentLike.user_id == user.id).first() if not commentss_like: commentss_like = CommentLike() commentss_like.comment_id = comments_id commentss_like.user_id = g.user.id db.session.add(commentss_like) comment.like_count += 1 current_app.logger.error([commentss_like.comment_id]) else: comment_likess = CommentLike.query.filter( CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if comment_likess: db.session.delete(comment_likess) comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库操作失败") return jsonify(errno=RET.OK, errmsg="OK")
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登陆") params_dict = request.json comment_id = params_dict.get("comment_id") # news_id = params_dict.get("news_id") action = params_dict.get("action") if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") if action not in ["add", "remove"]: return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comment_id = int(comment_id) # news_id = int(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据查询错误") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论不存在") if action == "add": com_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() if not com_like: com_like = CommentLike() com_like.user_id = user.id com_like.comment_id = comment_id comment.like_count += 1 db.session.add(com_like) else: com_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() if com_like: db.session.delete(com_like) comment.like_count -= 1 # try: # db.session.commit() # except Exception as e: # db.session.rollback() # current_app.logger.error(e) # return jsonify(errno=RET.DBERR, errmsg="数据库操作失败") return jsonify(errno=RET.OK, errmsg="操作成功")
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") comment_id = request.json.get('comment_id') action = request.json.get('action') if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数不完整") if action not in ["add", "remove"]: return jsonify(errno=RET.DATAERR, errmsg="请求参数格式错误") try: comment_id = int(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DATAERR, errmsg="请求参数格式错误") try: comments = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg='保存数据失败') if not comments: return jsonify(errno=RET.NODATA, errmsg='评论不存在') if action == 'add': comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) comments.like_count += 1 else: try: comment_like = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库查询失败") if comment_like: db.session.delete(comment_like) comments.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据库操作失败") return jsonify(errno=RET.OK, errmsg="点赞成功")
def set_comment_like(): """ 评论点赞 :return: """ # 用户是否登陆 user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg='用户未登录') # 取到请求参数 comment_id = request.json.get('comment_id') action = request.json.get('action') # 判断参数 if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg='参数错误') if action not in ['add', 'remove']: return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 获取到要被点赞的评论模型 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据失败") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论数据不存在") # action的状态,如果点赞,则查询后将用户id和评论id添加到数据库 if action == 'add': commentlike = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() if not commentlike: # 点赞评论 commentlike = CommentLike() commentlike.comment_id = comment_id commentlike.user_id = user.id db.session.add(commentlike) # 更新点赞次数 comment.like_count += 1 else: # 取消点赞评论,查询数据库,如果已存在,则删除点赞信息 commentlike = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() if commentlike: db.session.delete(commentlike) # 减少点赞次数 comment.like_count -= 1 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="操作失败") # 返回结果 return jsonify(errno=RET.OK, errmsg='操作成功')
def comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登陆") news_id = request.json.get("news_id") comments_id = request.json.get("comment_id") action = request.json.get("action") if not all([news_id, comments_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") if action not in ["add", "remove"]: return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comments_id = int(comments_id) news_id = int(news_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.PARAMERR, errmsg="参数错误") try: comment = Comment.query.get(comments_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="数据查询错误") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论不存在") if action == "add": comment_like_model = CommentLike.query.filter( CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if not comment_like_model: comment_like_model = CommentLike() comment_like_model.user_id = user.id comment_like_model.comment_id = comment.id try: db.session.add(comment_like_model) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() else: comment_like_model = CommentLike.query.filter( CommentLike.user_id == user.id, CommentLike.comment_id == comment.id).first() if comment_like_model: db.session.delete(comment_like_model) db.session.commit() return jsonify(errno=RET.OK, errmsg="OK")
def set_comment_like(): '''评论点赞功能''' user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") # 获取参数 comment_id = request.json.get('comment_id') news_id = request.json.get('news_id') action = request.json.get('action') # 判断参数 if not all([comment_id, news_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") if action not in ('add', 'remove'): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 查询评论数据 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据失败") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论数据不存在") if action == 'add': comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() if not comment_like: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) # 增加点赞条数 comment.like_count += 1 else: # 删除点赞数据 comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() if comment_like: db.session.delete(comment_like) comment.like_count -= 1 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="操作失败") return jsonify(errno=RET.OK, errmsg="操作成功")
def set_comment_like(): # 1,获取参数 comment_id, (news_id)分析错误 user, action(是否点赞了) """ 为什么不加user,这样看不到是谁点赞 :return: """ user = g.user if not user: current_app.logger.error("用户未登录") return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") param = request.json comment_id = param.get("comment_id") action = param.get("action") # 2,校验参数 if not all([comment_id, action]): current_app.logger.error("参数不足") return jsonify(errno=RET.NODATA, errmsg="参数不足") if action not in ["add", "remove"]: current_app.logger.error("参数错误") return jsonify(errno=RET.PARAMERR, errmsg="参数错误") # 3,逻辑处理 try: # comment_like = CommentLike.query.filter(CommentLike.comment_id == comment_id).first() # comment = Comment.query.filter(comment_id).first() comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询错误") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论不存在") try: comment_like = CommentLike.query.filter(CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询错误") if action == "add": if not comment_like: comment_like_obj = CommentLike() comment_like_obj.user_id = user.id comment_like_obj.comment_id = comment_id comment.like_count += 1 db.session.add(comment_like_obj) else: if comment_like: db.session.delete(comment_like) comment.like_count -= 1 try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询错误") # 4,返回值 return jsonify(errno=RET.OK, errmsg="点赞功能完成")
def comment_like(): #1.判断用户登陆 user = g.user if not user: return jsonify(errno=response_code.RET.SESSIONERR, errmsg='用户未登录') #2.接受参数,news_id,comment_id,action json_dict = request.json comment_id = json_dict.get('comment_id') news_id = json_dict.get('news_id') action = json_dict.get('action') #3.校验参数齐全,action范围 if not all([comment_id, action]): return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数不全') if action not in ['remove', 'add']: return jsonify(errno=response_code.RET.PARAMERR, errmsg='参数错误') #4.查询评论是否存在 try: comment = Comment.query.get(comment_id) except Exception as e: logging.error(e) return jsonify(errno=response_code.RET.DBERR, errmsg='查询评论失败') if not comment: return jsonify(errno=response_code.RET.DBERR, errmsg='该评论不存在') #5.更具action实现点赞动作 comment_like_mode = None comment_like_mode = CommentLike.query.filter( CommentLike.comment_id == comment.id, CommentLike.user_id == user.id).first() if action == 'add': if not comment_like_mode: comment_like_mode = CommentLike() comment_like_mode.user_id = user.id comment_like_mode.comment_id = comment_id comment.like_count += 1 db.session.add(comment_like_mode) else: if comment_like_mode: db.session.delete(comment_like_mode) # 6.同步数据库 try: db.session.commit() except Exception as e: db.session.rollback() logging.error(e) return jsonify(errno=response_code.RET.DBERR, errmsg='操作失败') # 7.返回会响应 return jsonify(errno=response_code.RET.OK, errmsg='操作成功')
def set_comment_like(): user = g.user if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") comment_id = request.json.get("comment_id") action = request.json.get("action") if not all([comment_id, action]): return jsonify(errno=RET.PARAMERR, errmsg="参数不足") if action not in ("add", "remove"): return jsonify(errno=RET.PARAMERR, errmsg="参数错误") if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据失败") if not comment: return jsonify(errno=RET.NODATA, errmsg="评论数据不存在") if action == "add": try: comment_like_obj = CommentLike.query.filter_by( comment_id=comment_id, user_id=user.id).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询评论点赞对象异常") if not comment_like_obj: comment_like = CommentLike() comment_like.comment_id = comment_id comment_like.user_id = user.id db.session.add(comment_like) comment.like_count += 1 else: comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() try: comment_like_obj = CommentLike.query.filter( CommentLike.comment_id == comment_id, CommentLike.user_id == user.id).first() except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询评论点赞对象异常") if comment_like_obj: db.session.delete(comment_like_obj) comment.like_count -= 1 try: db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="保存评论对象异常") return jsonify(errno=RET.OK, errmsg="操作成功")
def comment_like(): # - 1.判断用户是否登陆 if not g.user: return jsonify(errno=RET.NODATA,errmsg="用户未登录") # - 2.获取参数 comment_id =request.json.get("comment_id") action = request.json.get("action") # - 3.校验参数,为空校验 if not all([comment_id,action]): return jsonify(errno=RET.PARAMERR, errmsg="参数不全") # - 4.操作类型校验 if not action in ["add","remove"]: return jsonify(errno=RET.DATAERR, errmsg="操作类型有误") # - 5.根据评论编号取出,评论对象 try: comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="获取评论失败") # - 6.判断评论对象是否存在 if not comment: return jsonify(errno=RET.NODATA, errmsg="评论不存在") try: # - 7.根据操作类型,点赞,取消点赞 if action =="add": #判断用户是否点过赞 comment_like = CommentLike.query.filter(CommentLike.user_id == g.user.id,CommentLike.comment_id ==comment_id).first() if not comment_like: #创建点赞对象 comment_like = CommentLike() comment_like.user_id = g.user.id comment_like.comment_id = comment_id #保存点赞对象到数据库 db.session.add(comment_like) db.session.commit() comment.like_count +=1 else: # 判断用户是否点过赞 comment_like = CommentLike.query.filter(CommentLike.user_id == g.user.id,CommentLike.comment_id == comment_id).first() if not comment_like: # 移除点赞对象 db.session.delete(comment_like) db.session.commit() if comment.like_count > 0: comment.like_count -= 1 except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="操作失败") # - 8.返回响应 return jsonify(errno=RET.OK, errmsg="操作成功")
def comment_like(): """ 点赞/取消点赞 :return: """ # 获取登陆用户信息 user = g.user # 如果没有登陆 if not user: return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录") # 获取请求体 dict_data = request.json # 获取评论ID comment_id = dict_data.get("comment_id") # 判断请求体参数是否都有数据 if not all([comment_id]): return jsonify(errno=RET.PARAMERR, errmsg="参数不齐") try: # 查询评论详情 comment = Comment.query.get(comment_id) except Exception as e: current_app.logger.error(e) return jsonify(errno=RET.DBERR, errmsg="查询数据失败") # 如果没有评论数据 if not comment: return jsonify(errno=RET.NODATA, errmsg="评论数据不存在") # 查询当前评论当前用户点赞数据 comment_like = CommentLike.query.filter_by(comment_id=comment_id, user_id=user.id).first() # 如果没有数据 -- 可以进行点赞 if not comment_like: # 实例化 comment_like = CommentLike() # 评论ID comment_like.comment_id = comment_id # 用户ID comment_like.user_id = user.id # 插入数据库 mysql_db.session.add(comment_like) # 增加点赞条数 comment.like_count += 1 else: # 删除点赞数据 mysql_db.session.delete(comment_like) # 减小点赞条数 comment.like_count -= 1 try: # 事务提交 mysql_db.session.commit() except Exception as e: current_app.logger.error(e) mysql_db.session.rollback() return jsonify(errno=RET.DBERR, errmsg="操作失败") # 返回响应 return jsonify(errno=RET.OK, errmsg="操作成功")