33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from flask import Blueprint, redirect, url_for, flash, request
|
|
from flask_login import login_required, current_user
|
|
from app import db
|
|
from app.models import Comment
|
|
from app.forms import CommentForm
|
|
|
|
comments_bp = Blueprint('comments', __name__)
|
|
|
|
# 添加评论
|
|
@comments_bp.route('/add', methods=['POST'])
|
|
@login_required
|
|
def add_comment():
|
|
content = request.form['content']
|
|
article_id = request.form['article_id']
|
|
comment = Comment(content=content, article_id=article_id, user_id=current_user.id)
|
|
db.session.add(comment)
|
|
db.session.commit()
|
|
flash('评论已提交', 'success')
|
|
return redirect(url_for('articles.article_detail', article_id=article_id))
|
|
|
|
# 删除评论
|
|
@comments_bp.route('/delete/<int:comment_id>', methods=['POST'])
|
|
@login_required
|
|
def delete_comment(comment_id):
|
|
comment = Comment.query.get_or_404(comment_id)
|
|
if comment.user_id == current_user.id or current_user.is_admin:
|
|
db.session.delete(comment)
|
|
db.session.commit()
|
|
flash('评论已删除', 'success')
|
|
else:
|
|
flash('您没有权限删除此评论', 'danger')
|
|
return redirect(url_for('articles.article_detail', article_id=comment.article_id))
|