Пример #1
0
def comments_create(post_id, comment_id):
    if request.method == 'GET':
        return redirect(
            f'{url_for("posts_details", post_id=post_id)}#{comment_id or ""}')

    form = CommentForm(request.form)

    if not form.validate():
        return redirect(url_for('posts_details', post_id=post_id))

    parent = Comment.query.get(comment_id) if comment_id else None

    if parent and (str(parent.post_id) != post_id or parent.deleted):
        return redirect(url_for('posts_details', post_id=post_id))

    comment = Comment(form.content.data)
    comment.account_id = current_user.id
    comment.post_id = post_id
    comment.parent_id = comment_id

    with session_scope() as session:
        session.add(comment)
        session.commit()

        return redirect(
            f'{url_for("posts_details", post_id=post_id)}#{comment.id}')
Пример #2
0
def new_comment(post_id):

    form = CommentForm(request.form)
    if not form.validate():
        return render_template("/posts/post.html",
                               form=PostForm(),
                               post=Post.query.get(post_id),
                               commentform=form)

    c = Comment(form.comment.data)
    c.account_id = current_user.id
    c.post_id = post_id

    db.session.add(c)
    db.session.commit()

    return redirect(url_for('post_specific', post_id=post_id))