Exemplo n.º 1
0
def comments(id):
    post = Post.query.get(int(id))
    comments = (
        Comment.query.filter_by(post_id=id, depth=0).order_by(Comment.created_at).all()
    )
    form = CommentForm()
    if form.validate_on_submit():
        if current_user.is_authenticated:
            c = Comment(messages=form.messages.data)
            c.post_id = post.id
            c.user_id = current_user.id
            c.created_at = datetime.utcnow()
            db.session.add(c)
            db.session.commit()
            return redirect(url_for("comment.comments", id=post.id))
        return redirect(url_for("auth.login"))
    if request.method == "GET":
        form.messages.data = ""
    return render_template(
        "/comment/comment.html",
        title="comment",
        form=form,
        post=post,
        comments=comments,
    )
Exemplo n.º 2
0
def song_comment(id):
    form = CommentForm()
    form['csrf_token'].data = request.cookies['csrf_token']
    if form.validate_on_submit():
        comment = Comment(description=form.data["description"],
                          user_id=form.data["user_id"],
                          song_id=id,
                          created_at=form.data["created_at"])
        print(comment)
        db.session.add(comment)
        db.session.commit()
    return comment.to_dict()
Exemplo n.º 3
0
def comments(id):
    post = Post.query.get(int(id))
    comments = Comment.query.filter_by(post_id=id, is_spam=False, depth=0).all()
    form = CommentForm()
    if form.validate_on_submit():
        if current_user.is_authenticated:
            c = Comment(messages=form.messages.data)
            c.post_id = post.id
            c.user_id = current_user.id
            db.session.add(c)
            db.session.commit()
            return redirect(url_for('comment.comments', id=post.id))
        return redirect(url_for('auth.login'))
    if request.method == 'GET':
        form.messages.data = ''
    return render_template('/comment/comment.html', title='comment', form=form, post=post, comments=comments)
        
Exemplo n.º 4
0
def reply(id):
    parent = Comment.query.get(int(id))
    max_depth = app.config.get('MAX_DEPTH')
    current_depth = parent.depth + 1
    form = CommentForm()
    if form.validate_on_submit():
        r = Comment(messages=form.messages.data)
        r.parent_id = id
        r.post_id = parent.post_id
        r.user_id = current_user.id
        r.depth = current_depth if current_depth <= max_depth else max_depth
        db.session.add(r)
        db.session.commit()
        return redirect(url_for('comment.comments', id=parent.post_id))
    if request.method == 'GET':
        form.messages.data = ''
    return render_template('/comment/replies.html', title='reply', form=form, parent=parent)
Exemplo n.º 5
0
def reply(id):
    parent = Comment.query.get(int(id))
    max_depth = app.config.get("MAX_DEPTH")
    current_depth = parent.depth + 1
    form = CommentForm()
    if form.validate_on_submit():
        r = Comment(messages=form.messages.data)
        r.parent_id = id
        r.post_id = parent.post_id
        r.user_id = current_user.id
        r.created_at = datetime.utcnow()
        r.depth = current_depth if current_depth <= max_depth else max_depth
        db.session.add(r)
        db.session.commit()
        return redirect(url_for("comment.comments", id=parent.post_id))
    if request.method == "GET":
        form.messages.data = ""
    return render_template(
        "/comment/replies.html", title="reply", form=form, parent=parent
    )