18 lines
605 B
Python
18 lines
605 B
Python
from flask import Blueprint, request, redirect, url_for
|
|
from flask_login import login_required, current_user
|
|
from ..extensions import db
|
|
from ..models import Comment, Post
|
|
|
|
bp = Blueprint("comments", __name__, url_prefix="/comments")
|
|
|
|
@bp.route("/create/<int:post_id>", methods=["POST"])
|
|
@login_required
|
|
def create(post_id):
|
|
post = Post.query.get_or_404(post_id)
|
|
body = request.form.get("body")
|
|
if body:
|
|
c = Comment(post_id=post.id, user_id=current_user.id, body=body)
|
|
db.session.add(c)
|
|
db.session.commit()
|
|
return redirect(url_for("posts.detail", post_id=post.id))
|