예제 #1
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())
예제 #2
0
def addcomment(request, id):
    url = request.META.get('HTTP_REFERER')
    if request.method == 'POST':
        form = CommentForm(request.POST)

        if form.is_valid():
            current_user = request.user
            data = Comment()
            data.user_id = current_user.id
            data.news_id = id
            data.subject = form.cleaned_data['subject']
            data.comment = form.cleaned_data['comment']
            data.rate = form.cleaned_data['rate']
            data.ip = request.META.get('REMOTE_ADDR')
            data.save()
            messages.success(
                request, "Yorumunuz başarı ile eklenmiştir. Teşekkür ederiz !")

            return HttpResponseRedirect(url)

    messages.warning(
        request,
        "Yorumunuz Gönderilememiştir, Lütfen Bilgilerinizi Kontrol Ediniz!")
    return HttpResponseRedirect(url)