Study/routes/admin.py

63 lines
1.9 KiB
Python
Raw Normal View History

2024-11-20 12:37:58 +08:00
from flask import Blueprint, render_template, redirect, url_for, flash
from flask_login import login_required
from app import db
from app.models import User, Article, Comment
admin_bp = Blueprint('admin', __name__)
# 管理员页面,查看所有用户
@admin_bp.route('/users')
@login_required
def manage_users():
if not current_user.is_admin:
flash('您没有权限访问此页面', 'danger')
return redirect(url_for('index'))
users = User.query.all()
return render_template('admin/users.html', users=users)
# 管理员删除用户
@admin_bp.route('/delete_user/<int:user_id>', methods=['POST'])
@login_required
def delete_user(user_id):
if not current_user.is_admin:
flash('您没有权限删除用户', 'danger')
return redirect(url_for('admin.manage_users'))
user = User.query.get_or_404(user_id)
db.session.delete(user)
db.session.commit()
flash('用户已删除', 'success')
return redirect(url_for('admin.manage_users'))
# 管理员删除文章
@admin_bp.route('/delete_article/<int:article_id>', methods=['POST'])
@login_required
def delete_article(article_id):
if not current_user.is_admin:
flash('您没有权限删除文章', 'danger')
return redirect(url_for('index'))
article = Article.query.get_or_404(article_id)
db.session.delete(article)
db.session.commit()
flash('文章已删除', 'success')
return redirect(url_for('articles.articles'))
# 管理员删除评论
@admin_bp.route('/delete_comment/<int:comment_id>', methods=['POST'])
@login_required
def delete_comment_admin(comment_id):
if not current_user.is_admin:
flash('您没有权限删除评论', 'danger')
return redirect(url_for('index'))
comment = Comment.query.get_or_404(comment_id)
db.session.delete(comment)
db.session.commit()
flash('评论已删除', 'success')
return redirect(url_for('articles.article_detail', article_id=comment.article_id))