Ejemplo n.º 1
0
def news_comment(request, news_id):
    comment_news = News.objects.filter(pk=news_id)[0]
    user = request.user
    content = request.POST.get('comment')
    comment = Comment()
    comment.news = comment_news
    comment.user = user
    comment.content = content
    comment.save()
    return redirect(reverse('news:news_detail', kwargs={'news_id': news_id}))
Ejemplo n.º 2
0
def news_comment():

    if not g.user:  #判断用户是否登录
        return jsonify(errno=Code.NODATA, errmsg="用户未登录!")

    news_id = request.json.get("news_id")
    content = request.json.get("comment")
    parent_id = request.json.get("parent_id")
    # print(news_id, content)
    if not all([news_id, content]):
        return jsonify(errno=Code.PARAMERR, errmsg="参数不全")

    # 取出新闻对象
    try:
        news = News.query.get(news_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=Code.DBERR, errmsg="获取新闻失败")

    if not news: return jsonify(errno=Code.NODATA, errmsg="新闻不存在")

    # 创建评论对象,设置属性
    comment = Comment()
    comment.user_id = g.user.id
    comment.news_id = news_id
    comment.content = content
    if parent_id:
        comment.parent_id = parent_id
    # 保存评论
    try:
        db.session.add(comment)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=Code.DBERR, errmsg="评论失败")
    return jsonify(errno=Code.OK, errmsg="评论成功", data=comment.to_dict())