コード例 #1
0
def add_comment(articleid):
    comment = request.get_json().get("comment")
    article_id = request.get_json().get("article_id")
    comment = Comment(comment=comment)
    user_id = session.get('user_id')
    user = User.query.filter_by(id=user_id).first()
    comment.author = user
    article = Article.query.filter_by(id=article_id).first()
    comment.article = article
    db.session.add(comment)
    db.session.commit()
    return jsonify({"result": "success to add comment"}), 200
コード例 #2
0
    def post(self, blog_id=1, page=1):
        data = request.json
        blog = Blog.query.get(blog_id)
        comment = Comment()
        comment.detail = data['detail']
        user_id = data['user_id']
        user = User.query.get(user_id)
        if user:
            comment.author = user
        else:
            guest = User('guest','unused')
            guest.id = 0
            comment.author = guest
        comment.article = blog
        comment.createAt = datetime.now()
        db.session.add(comment)
        db.session.commit()

        comments = Comment.query.filter_by(article=blog).paginate(page, PAGE_SIZE, False)
        return comments