管理员端因为路由函数过多,所以我们创建views包。
管理员操作
在app/admin/views/admin.py
中:
from app import app, db
from flask import render_template, flash, redirect, url_for
from app.admin import admin
from app.admin.forms.admin import AdminForm
from app.admin.utils import is_admin_login
# *************************************管理员操作**************************************
from app.models import Admin
@admin.route("/admin/add/", methods=['GET', 'POST'])
@is_admin_login
def admin_add():
form = AdminForm()
from werkzeug.security import generate_password_hash
if form.validate_on_submit():
data = form.data
if Admin.query.filter_by(name=data['name']).count() == 1:
flash('管理员已存在!', category='err')
return redirect(url_for('admin.admin_add'))
if not data['is_super']:
data['is_super'] = 0
add_admin = Admin(
name=data['name'],
password=generate_password_hash(data['pwd']),
role_id=data['role_id'],
is_super=data['is_super']
)
db.session.add(add_admin)
db.session.commit()
flash('管理员添加成功', category='ok')
return render_template('admin/admin/add.html', form=form)
@admin.route("/admin/list/")
@admin.route("/admin/list/<int:page>/")
@is_admin_login
def admin_list(page=1):
adminsPageObj = Admin.query.order_by(
Admin.addtime.desc()
).paginate(page=page, per_page=app.config['PER_PAGE'])
return render_template('admin/admin/list.html', adminsPageObj=adminsPageObj)
权限管理操作
在app/admin/views/auth.py
中:
from app import app, db
from flask import render_template, flash, redirect, url_for
from app.admin import admin
from app.admin.forms.auth import AuthForm
from app.admin.utils import write_adminlog, is_admin_login
# ***********************************权限管理操作*****************************
from app.models import Auth
@admin.route("/auth/add/", methods=['GET', 'POST'])
@is_admin_login
def auth_add():
form = AuthForm()
if form.validate_on_submit():
data = form.data
if Auth.query.filter_by(url=data['url']).count() == 1:
flash('权限链接地址已存在!', category='err')
return redirect(url_for('admin.auth_add'))
auth = Auth(
name=data['name'],
url=data['url']
)
db.session.add(auth)
db.session.commit()
flash('权限地址%s添加成功!' % (auth.name), category='ok')
write_adminlog('权限地址%s添加成功!' % (auth.name))
return render_template('admin/auth/add.html', form=form)
@admin.route("/auth/list/")
@admin.route("/auth/list/<int:page>/")
@is_admin_login
def auth_list(page=1):
authsPageObj = Auth.query.order_by(Auth.addtime.desc()).paginate(page=page, per_page=app.config['PER_PAGE'])
return render_template('admin/auth/list.html', authsPageObj=authsPageObj)
@admin.route("/auth/delete/<int:id>/")
@is_admin_login
def auth_del(id=None):
if id:
auth = Auth.query.get_or_404(id)
db.session.delete(auth)
db.session.commit()
flash('删除权限地址%s成功' % (auth.name), category='ok')
write_adminlog('删除权限地址%s成功' % (auth.name))
return redirect(url_for('admin.auth_list', page=1))
@admin.route("/auth/edit/<int:id>/", methods=['GET', 'POST'])
@is_admin_login
def auth_edit(id=None):
auth = Auth.query.get_or_404(id)
form = AuthForm(
name=auth.name,
url=auth.url
)
if form.validate_on_submit():
data = form.data
if Auth.query.filter_by(url=data['url']).count() == 1 and auth.url != data['url']:
flash('权限链接地址已存在!', category='err')
return redirect(url_for('admin.auth_edit', id=id))
auth.name = data['name']
auth.url = data['url']
db.session.commit()
flash('权限地址<%s>修改成功!' % (auth.url), category='ok')
write_adminlog('权限地址<%s>修改成功!' % (auth.url))
return render_template('admin/auth/edit.html', form=form)
用户收藏管理
在app/admin/views/collect.py
中:
from app import app, db
from flask import render_template, flash, redirect, url_for, request
from app.admin import admin
from app.admin.utils import write_adminlog, is_admin_login
from app.models import MovieCollect
# ********************************用户收藏管理***********************
@admin.route("/collect/list/")
@admin.route("/collect/list/<int:page>/")
@is_admin_login
def collect_list(page=1):
moviecollectsPageObj = MovieCollect.query.paginate(page=page, per_page=app.config['PER_PAGE'])
return render_template('admin/collect/list.html', moviecollectsPageObj=moviecollectsPageObj, app=app)
@admin.route("/collect/delete/<int:id>")
@is_admin_login
def collect_del(id=None):
if id:
moviecollect = MovieCollect.query.get_or_404(id)
db.session.delete(moviecollect)
db.session.commit()
flash('删除%s用户对%s电影收藏成功!' % (moviecollect.user.name, moviecollect.movie.name), category='ok')
write_adminlog('删除%s用户对%s电影收藏成功')
return redirect(url_for('admin.comment_list', page=1))
评论管理
在app/admin/views/comment.py
中:
from app import app, db
from flask import render_template, flash, redirect, url_for
from app.admin import admin
from app.admin.utils import write_adminlog, is_admin_login
from app.models import Comment
# ***************************评论管理*****************************
@admin.route("/comment/list/")
@admin.route("/comment/list/<int:page>/")
@is_admin_login
def comment_list(page=1):
commentsPageObj = Comment.query.paginate(page, per_page=app.config['PER_PAGE'])
return render_template('admin/comment/list.html', commentsPageObj=commentsPageObj)
@admin.route("/comment/delete/<int:id>/")
@is_admin_login
def comment_delete(id=None):
if id:
comment = Comment.query.get_or_404(id)
db.session.delete(comment)
db.session.commit()
flash('删除%s用户%s电影评论成功!' % (comment.user.name, comment.movie.name), category='ok')
write_adminlog('删除%s用户%s电影评论成功!' % (comment.user.name, comment.movie.name))
return redirect(url_for('admin.comment_list', page=1))
管理员操作日志
在app/admin/views/logs.py
中:
from app import app
from flask import render_template, session
from app.admin import admin
from app.admin.utils import is_admin_login
from app.models import Admin, AdminOplog, Adminlog, Userlog
# **************************管理员操作日志*****************************
@admin.route("/logs/operate_log/")
@admin.route("/logs/operate_log/<int:page>/")
@is_admin_login
def logs_operate_log(page=1):
admin_id = session.get('admin_id')
admin = Admin.query.get_or_404(admin_id)
if admin.is_super:
adminOplogPageObj = AdminOplog.query.paginate(page, per_page=app.config['PER_PAGE'])
else:
adminOplogPageObj = AdminOplog.query.filter_by(admin_id=admin_id
).paginate(page,
per_page=app.config['PER_PAGE'])
return render_template('admin/logs/operate_log.html',
adminOplogPageObj=adminOplogPageObj)
# 管理员登录日志
@admin.route("/logs/admin_log/")
@admin.route("/logs/admin_log/<int:page>/")
@is_admin_login
def logs_admin_log(page=1):
admin_id = session.get('admin_id')
admin = Admin.query.get_or_404(admin_id)
if admin.is_super:
adminlogPageObj = Adminlog.query.paginate(page, per_page=app.config['PER_PAGE'])
else:
adminlogPageObj = Adminlog.query.filter_by(admin_id=admin_id
).paginate(page,
per_page=app.config['PER_PAGE'])
return render_template('admin/logs/admin_log.html',
adminlogPageObj=adminlogPageObj)
@admin.route("/logs/user_log/")
@admin.route("/logs/user_log/<int:page>/")
@is_admin_login
def logs_user_log(page=1):
userlogPageObj = Userlog.query.paginate(page,
per_page=app.config['PER_PAGE'])
return render_template('admin/logs/user_log.html',
userlogPageObj=userlogPageObj)
管理员登录登出修改密码
在app/admin/views/main.py
中:
from flask import session, flash, request, redirect, url_for, render_template
from werkzeug.security import generate_password_hash
from app import db
from app.admin import admin
from app.admin.forms.main import LoginForm, PwdForm
from app.admin.utils import is_admin_login
from app.models import Admin, Adminlog
@admin.route('/')
def index():
return render_template('admin/base.html')
@admin.route('/login/', methods=['POST', 'GET'])
def login():
form = LoginForm()
if form.validate_on_submit():
name = form.username.data
password = form.password.data
admin = Admin.query.filter_by(name=name).first()
if admin and admin.verify_password(password):
# session信息的保存
session['admin_id'] = admin.id
session['admin'] = admin.name
flash("管理员%s登录成功" % (admin.name))
remote_ip = request.remote_addr
# 将登录信息写到日志中;
adminlog = Adminlog(admin_id=admin.id,
ip=remote_ip,
area='xxx内网IP')
db.session.add(adminlog)
db.session.commit()
# 从index蓝图里面寻找index函数;
return redirect(url_for('admin.index'))
else:
flash("管理员登录失败")
return redirect(url_for('admin.login'))
return render_template('admin/login.html',
form=form)
@admin.route('/logout/')
@is_admin_login
def logout():
session.pop('admin_id', None)
session.pop('admin', None)
return redirect(url_for('admin.login'))
# 修改密码
@admin.route('/pwd/', methods=['GET', 'POST'])
def pwd():
form = PwdForm()
if form.validate_on_submit():
# 获取当前登录用户的密码
admin = Admin.query.filter_by(name=session.get('admin')).first()
# 判断用户的旧密码是否正确
if admin.verify_password(form.old_pwd.data):
# ********数据库里面的是password
admin.password = generate_password_hash(form.new_pwd.data)
db.session.add(admin)
db.session.commit()
flash("密码更新成功")
else:
flash("旧密码错误, 请重新输入")
return redirect(url_for('admin.pwd'))
return render_template('admin/pwd.html', form=form)
管理员查看视频
在app/admin/views/movie.py
中:
import os
from werkzeug.utils import secure_filename
from app import app, db
from flask import render_template, flash, redirect, url_for
from app.admin import admin
from app.admin.forms.movie import MovieForm
from app.admin.utils import write_adminlog, is_admin_login
from app.home import change_filename
from app.models import Movie
@admin.route('/movie/list/')
@admin.route('/movie/list/<int:page>/')
def movie_list(page=1):
moviesPageObj = Movie.query.paginate(page, per_page=app.config['PER_PAGE'])
return render_template('admin/movie/list.html',
moviesPageObj=moviesPageObj,
app=app)
@admin.route('/movie/add/', methods=['POST', 'GET'])
def movie_add():
form = MovieForm()
if form.validate_on_submit():
# ****难点: 电影文件和电影封面要保存到服务器的static/upload目录中;
# 1). 获取上传文件的名称
url = form.url.data.filename
logo = form.logo.data.filename
# 2). 获取文件保存的路径
file_save_path = app.config['MOVIE_UP_DIR']
if not os.path.exists(file_save_path):
os.makedirs(file_save_path)
# 3). 对于上传的文件进行重命名
from app.home import change_filename
url = change_filename(url)
logo = change_filename(logo)
# 4). 保存文件到本地
form.url.data.save(os.path.join(file_save_path, url))
form.logo.data.save(os.path.join(file_save_path, logo))
movie = Movie(
name=form.name.data,
url=url, # 只存储文件的名称
info=form.info.data,
logo=logo,
star=form.star.data,
tag_id=form.tag_id.data,
area=form.area.data,
length=form.length.data,
release_time=form.release_time.data
)
db.session.add(movie)
db.session.commit()
flash("添加电影%s成功" % (form.name.data))
from app.admin.utils import write_adminlog
write_adminlog("添加电影%s成功" % (form.name.data))
return redirect(url_for('admin.movie_add'))
return render_template('admin/movie/add.html',
form=form,
)
@admin.route('/movie/edit/<int:id>/', methods=['GET', 'POST'])
@is_admin_login
def movie_edit(id):
movie = Movie.query.get_or_404(id)
# 给表单赋初始值,文件表单不处理
form = MovieForm(
name=movie.name,
# url=movie.url, # 上传文件,这样赋初始值无效,在前端可以通过上传路径+movie.url来获取文件的保存路径,显示在页面上
info=movie.info,
# logo=movie.logo, # 上传图片和文件类似
star=movie.star,
tag_id=movie.tag_id,
area=movie.area,
release_time=movie.release_time,
length=movie.length,
)
# 对于修改数据,电影文件和封面图已存在,可以非必填:
# 按照教程上测试了validators参数,但始终不行,
# 最终修改required的值就可以了
form.url.validators = []
form.logo.validators = [] # 验证列表为空
if form.validate_on_submit():
data = form.data
# 提交的片名在数据库中已存在,且不是当前的电影名称
if Movie.query.filter_by(name=data['name']).count() == 1 and movie.name != data['name']:
flash('电影片名已存在,请检查', category='err')
return redirect(url_for('admin.movie_edit', edit_id=id))
# 以下和直接修改的数据
movie.name = data['name']
movie.info = data['info']
movie.star = data['star']
movie.tag_id = data['tag_id']
movie.area = data['area']
movie.release_time = data['release_time']
movie.length = data['length']
# 文件保存路径操作
file_save_path = app.config['MOVIE_UP_DIR'] # 文件上传保存路径
if not os.path.exists(file_save_path):
os.makedirs(file_save_path) # 如果文件保存路径不存在,则创建一个多级目录
# 处理电影文件逻辑:先从磁盘中删除旧文件,然后保存新文件
if form.url.data: # 上传文件不为空,才进行保存
# 删除以前的文件
if os.path.exists(os.path.join(file_save_path, movie.url)):
os.remove(os.path.join(file_save_path, movie.url))
# 获取上传文件的名称
file_url = secure_filename(form.url.data.filename)
# 对上传的文件进行重命名
movie.url = change_filename(file_url)
# 保存文件,需要给文件的保存路径+文件名
form.url.data.save(file_save_path + movie.url)
# 处理封面图
if form.logo.data:
if os.path.exists(os.path.join(file_save_path, movie.logo)):
os.remove(os.path.join(file_save_path, movie.logo))
file_logo = secure_filename(form.logo.data.filename)
movie.logo = change_filename(file_logo)
form.logo.data.save(file_save_path + movie.logo)
# 保存到数据库中;
db.session.add(movie)
db.session.commit()
flash('修改电影%s成功' % (movie.name), 'ok')
write_adminlog('修改电影%s成功' % (movie.name))
return redirect(url_for('admin.movie_edit', id=id))
return render_template('admin/movie/edit.html',
form=form,
movie=movie)
@admin.route('/movie/delete/<int:id>/')
@is_admin_login
def movie_del(id=None):
if id:
movie = Movie.query.filter_by(id=id).first_or_404()
# 删除电影同时要从磁盘中删除电影的文件和封面文件
file_save_path = app.config['MOVIE_UP_DIR'] # 文件上传保存路径
# 如果存在将进行删除,不判断,如果文件不存在,删除会报错
if os.path.exists(os.path.join(file_save_path, movie.url)):
os.remove(os.path.join(file_save_path, movie.url))
if os.path.exists(os.path.join(file_save_path, movie.logo)):
os.remove(os.path.join(file_save_path, movie.logo))
# 删除数据库,提交修改,注意后面要把与电影有关的评论都要删除
db.session.delete(movie)
db.session.commit()
# 删除后闪现消息
flash('删除电影%s成功!' % (movie.name), category='ok')
write_adminlog('删除电影%s成功!' % (movie.name))
return redirect(url_for('admin.movie_list', page=1))
预告管理
在app/admin/views/preview.py
中:
import os
from werkzeug.utils import secure_filename
from app import app, db
from flask import render_template, flash, redirect, url_for
from app.admin import admin
from app.admin.forms.preview import PreviewForm
from app.admin.utils import write_adminlog, is_admin_login
from app.home import change_filename
from app.models import Preview
@admin.route('/preview/add/', methods=['POST', 'GET'])
@is_admin_login
def preview_add():
form = PreviewForm()
if form.validate_on_submit():
data = form.data
# 判断预告标题是否已经存在?
if Preview.query.filter_by(name=data['name']).count() == 1:
flash('预告标题已存在,请检查!', category='err')
return redirect(url_for('admin.preview_add'))
# 存储预告封面;
file_logo = secure_filename(form.logo.data.filename) # 获取上传文件名字
file_save_path = app.config['PREVIEW_UP_DIR'] # 文件上传保存路径
if not os.path.exists(file_save_path):
os.makedirs(file_save_path) # 如果文件保存路径不存在,则创建一个多级目录
logo = change_filename(file_logo) # 文件重命名
form.logo.data.save(os.path.join(file_save_path, logo)) # 保存文件到磁盘中
# 写入数据库;
preview = Preview(
name=data['name'],
logo=logo # 只在数据库中保存文件名
)
db.session.add(preview)
db.session.commit()
flash('添加预告%s成功' % (preview.name), 'ok')
write_adminlog('添加预告%s成功' % (preview.name))
return redirect(url_for('admin.preview_add'))
return render_template('admin/preview/add.html', form=form)
@admin.route('/preview/list/')
@admin.route('/preview/list/<int:page>/')
@is_admin_login
def preview_list(page=1):
previewsPageObj = Preview.query.paginate(page=page, per_page=app.config['PER_PAGE'])
return render_template('admin/preview/list.html', previewsPageObj=previewsPageObj, app=app)
@admin.route('/preview/delete/<int:id>/')
@is_admin_login
def preview_del(id=None):
if id:
preview = Preview.query.filter_by(id=id).first_or_404()
# 删除电影同时要从磁盘中删除电影的文件和封面文件
file_save_path = app.config['PREVIEW_UP_DIR'] # 文件上传保存路径
# 如果存在将进行删除,不判断,如果文件不存在,删除会报错
if os.path.exists(os.path.join(file_save_path, preview.logo)):
os.remove(os.path.join(file_save_path, preview.logo))
# 删除数据库,提交修改,注意后面要把与电影有关的评论都要删除
db.session.delete(preview)
db.session.commit()
# 删除后闪现消息
flash('删除预告%s成功!' % (preview.name), category='ok')
write_adminlog('删除预告%s成功!' % (preview.name))
return redirect(url_for('admin.preview_list', page=1))
@admin.route('/preview/edit/<int:id>/', methods=['POST', 'GET'])
@is_admin_login
def preview_edit(id=None):
preview = Preview.query.get_or_404(id)
form = PreviewForm(
name=preview.name,
)
# 不验证上传文件
form.logo.validators = []
if form.validate_on_submit():
data = form.data
if Preview.query.filter_by(name=data['name']).count() == 1 and preview.title != data['title']:
flash('预告标题已存在,请重新输入', category='err')
return redirect(url_for('admin.preview_edit', id=id))
preview.name = data['name']
print(data['logo'], type(data['logo']), form.logo.data, type(form.logo.data))
# <FileStorage: 'ssh.jpg' ('image/jpeg')> <class 'werkzeug.datastructures.FileStorage'>
# <FileStorage: 'ssh.jpg' ('image/jpeg')> <class 'werkzeug.datastructures.FileStorage'>
# 上面两种方式结果一样
# 文件保存路径操作
file_save_path = app.config['PREVIEW_UP_DIR'] # 文件上传保存路径
if not os.path.exists(file_save_path):
os.makedirs(file_save_path) # 如果文件保存路径不存在,则创建一个多级目录
if form.logo.data: # 当有上传新的图片
if os.path.exists(os.path.join(file_save_path, preview.logo)):
os.remove(os.path.join(file_save_path, preview.logo)) # 删除旧图片
file_logo_name = form.logo.data.filename
preview.logo = change_filename(file_logo_name) # 得到新的文件名,保存到输入局
form.logo.data.save(file_save_path + preview.logo)
db.session.commit()
flash('预告%s信息修改成功' % (preview.name), category='ok')
write_adminlog('预告%s信息修改成功' % (preview.name))
return redirect(url_for('admin.preview_edit', id=id))
return render_template('admin/preview/edit.html', form=form, preview=preview)
管理员角色操作
在app/admin/views/role.py
中:
from app import app, db
from flask import render_template, flash, redirect, url_for
from app.admin import admin
from app.admin.forms.role import RoleForm, EditRoleForm
from app.admin.utils import write_adminlog, is_admin_login
# **************************管理员角色操作********************************
from app.models import Role
@admin.route("/role/add/", methods=['GET', 'POST'])
@is_admin_login
def role_add():
form = RoleForm()
if form.validate_on_submit():
data = form.data
if Role.query.filter_by(name=data['name']).count() == 1:
flash('管理员角色已存在!', category='err')
return redirect(url_for('admin.role_add'))
print(data['auths'])
print(",".join(list(map(str, data['auths']))))
role = Role(
name=data['name'],
# 默认多选返回的是一个列表, 里面存储的是权限的id号;
auths=",".join(list(map(str, data['auths'])))
)
db.session.add(role)
db.session.commit()
flash('角色地址%s添加成功!' % (role.name), category='ok')
write_adminlog('角色地址%s添加成功!' % (role.name))
return render_template('admin/role/add.html', form=form)
@admin.route("/role/list/")
@admin.route("/role/list/<int:page>/")
@is_admin_login
def role_list(page=1):
rolesPageObj = Role.query.order_by(Role.addtime.desc()).paginate(page=page, per_page=app.config['PER_PAGE'])
return render_template('admin/role/list.html', rolesPageObj=rolesPageObj)
@admin.route("/role/delete/<int:id>/")
@is_admin_login
def role_del(id=None):
if id:
role = Role.query.get_or_404(id)
db.session.delete(role)
db.session.commit()
flash('删除角色地址%s成功' % (role.name), category='ok')
write_adminlog('删除角色地址%s成功' % (role.name))
return redirect(url_for('admin.role_list', page=1))
@admin.route("/role/edit/<int:id>/", methods=['GET', 'POST'])
@is_admin_login
def role_edit(id=None):
role = Role.query.get_or_404(id)
form = EditRoleForm(
name=role.name,
auths=list(map(lambda item: int(item), role.auths.split(','))) if role.auths else '' # 换回int型列表
)
if form.validate_on_submit():
data = form.data
if Role.query.filter_by(name=data['name']).count() == 1 and role.name != data['name']:
flash('角色名已存在!', category='err')
return redirect(url_for('admin.role_edit', id=id))
role.name = data['name']
role.auths = ",".join(map(str, data['auths']))
db.session.commit()
flash('角色<%s>修改成功!' % (role.name), category='ok')
write_adminlog('角色地址<%s>修改成功!' % (role.name))
return render_template('admin/role/edit.html', form=form)
管理员标签管理
在app/admin/views/tag.py
中:
from app import app, db
from flask import render_template, flash, redirect, url_for, request
from app.admin import admin
from app.admin.forms.tag import TagForm, EditTagForm
from app.admin.utils import write_adminlog, permission_control
from app.models import Tag
@admin.route('/tag/list/')
@admin.route('/tag/list/<int:page>/')
@permission_control
def tag_list(page=1):
tagsPageObj = Tag.query.paginate(page, per_page=app.config['PER_PAGE'])
return render_template('admin/tag/list.html',
tagsPageObj=tagsPageObj)
@admin.route('/tag/add/', methods=['POST', 'GET'])
@permission_control
def tag_add():
form = TagForm()
if form.validate_on_submit():
name = form.name.data
# 判断添加的标签是否存在?
tag = Tag.query.filter_by(name=name).first()
if tag:
flash("标签%s已经存在" % (tag.name))
return redirect(url_for('admin.tag_add'))
tag = Tag(name=name)
db.session.add(tag)
db.session.commit()
flash("标签%s添加成功" % (tag.name))
# ******将添加标签的操作记录到数据库中
write_adminlog("标签%s添加成功" % (tag.name))
return redirect(url_for('admin.tag_add'))
return render_template('admin/tag/add.html',
form=form)
@admin.route('/tag/edit/<int:id>/', methods=['POST', 'GET'])
@permission_control
def tag_edit(id):
form = EditTagForm()
tag = Tag.query.filter_by(id=id).first_or_404()
old_tagname = tag.name
form.name.data = tag.name
if form.validate_on_submit():
name = request.form['name']
# 判断要更新的标签名是否已经存在?
if name != tag.name and Tag.query.filter_by(name=name).first():
flash("标签%s已经存在" % (name))
return redirect(url_for('admin.tag_list', page=1))
tag.name = name
db.session.add(tag)
db.session.commit()
flash("更新标签为%s成功!" % (name), category='ok')
write_adminlog("更新标签(%s)为%s成功!" % (old_tagname, name))
return redirect(url_for('admin.tag_list', page=1))
return render_template('admin/tag/edit.html',
form=form)
@admin.route('/tag/delete/<int:id>/')
@permission_control
def tag_del(id):
if id:
tag = Tag.query.filter_by(id=id).first_or_404()
db.session.delete(tag)
db.session.commit()
# 删除后闪现消息
flash('删除标签%s成功!' % (tag.name), category='ok')
write_adminlog('删除标签%s成功!' % (tag.name))
return redirect(url_for('admin.tag_list', page=1))
管理员用户管理
在app/admin/views/user.py
中:
import os
from app import app, db
from flask import render_template, flash, redirect, url_for
from app.admin import admin
from app.admin.utils import write_adminlog, is_admin_login
from app.models import User
@admin.route("/user/list/")
@admin.route("/user/list/<int:page>/")
@is_admin_login
def user_list(page=1):
usersPageObj = User.query.paginate(page=page, per_page=app.config['PER_PAGE'])
return render_template('admin/user/list.html', usersPageObj=usersPageObj, app=app)
@admin.route("/user/view/<int:user_id>/")
@is_admin_login
def user_view(user_id=None):
if user_id:
user = User.query.get_or_404(user_id)
return render_template('admin/user/view.html',
user=user,
app=app)
@admin.route("/user/delete/<int:id>/")
@is_admin_login
def user_del(id=None):
if id:
user = User.query.get_or_404(id)
# 删除同时要从磁盘中删除封面文件
file_save_path = app.config['FC_DIR'] # 头像上传保存路径
# 如果存在将进行删除,不判断,如果文件不存在删除会报错
if os.path.exists(os.path.join(file_save_path, user.face)):
os.remove(os.path.join(file_save_path, user.face))
# 删除数据库,提交修改
db.session.delete(user)
db.session.commit()
# 删除后闪现消息
flash('删除会员%s成功!' % (user.name), category='ok')
write_adminlog('删除会员%s成功!' % (user.name))
return redirect(url_for('admin.user_list', page=1))