1.查出该新闻的所有评论
2.查出该用户所有点赞的评论
3.二者交集显示被用户点赞
@news_blu.route("/<int:news_id>")
@user_login_data
@click_list_data
def news_detail(news_id):
# 显示新闻
try:
news = News.query.get(news_id)
except Exception as e:
current_app.logger.error(e)
abort(404)
if not news:
abort(404)
news.clicks +=1
# 是否收藏
iscollected = False
if g.user:
if news in g.user.collection_news:
iscollected = True
# 显示评论
try:
comments = Comment.query.filter(Comment.news_id == news_id).order_by(Comment.create_time.desc()).all()
except Exception as e:
current_app.logger.error(e)
comments = []
# 点赞功能显示------------------------------
# 找出用户所有点赞的评论的id
if g.user:
comment_like_ids = [commentlike.comment_id for commentlike in CommentLike.query.filter(CommentLike.user_id == g.user.id).all()]
else:
comment_like_ids = []
# 如果评论在用户点赞的评论中,那么显示点赞
comment_list = []
for comment in comments:
com = comment.to_dict()
com["is_like"] = False
if g.user and comment.id in comment_like_ids:
com["is_like"] = True
comment_list.append(com)
# 按接口返回数据
data = {"news":news.to_dict(),
"user_info":g.user.to_dict() if g.user else None,
"clicks_list":g.clicks,
"is_collected": iscollected,
"comments": comment_list
}
return render_template("/news/detail.html", data=data)
comment_like_ids = [commentlike.comment_id for commentlike in CommentLike.query.filter(CommentLike.user_id == g.user.id).all()]
找出用户点赞过的所有评论的id