Beispiel #1
0
def article_comments(id):
    form = CommentWriteForm()

    # 댓글 쓰기 요청 처리
    if form.validate_on_submit():
        # 글의 댓글 수 증가
        db.session.query(Article).filter_by(id=id).update({
            Article.comment: Article.comment + 1
        })

        # 답글이 아닐 때 빈 문자열("")로 정보가 들어오면 SQL 오류가 발생하므로 None으로 바꿔줍니다.
        if not form.parent_id.data:
            form.parent_id.data = None

        # 댓글 추가
        comment = Comment(article_id=id, user_id=g.user.id, content=form.content.data, parent_id=form.parent_id.data)
        db.session.add(comment)
        db.session.commit()

        comment.calculate_template_parameters()
        server.send_message_to_all(render_template_string("comment.html", comment=comment))

    return redirect(url_for("article", id=id))