{{ item }}
{% endfor %}
{# 参考 路径导航: https://v3.bootcss.com/components/#breadcrumbs #}
.
|-- app
| |-- admin
| | |-- forms
| | | |-- admin.py
| | | |-- auth.py
| | | |-- __init__.py
| | | |-- main.py
| | | |-- movie.py
| | | |-- preview.py
| | | |-- role.py
| | | `-- tag.py
| | |-- __init__.py
| | |-- utils.py
| | `-- views
| | |-- admin.py
| | |-- auth.py
| | |-- collect.py
| | |-- comment.py
| | |-- __init__.py
| | |-- logs.py
| | |-- main.py
| | |-- movie.py
| | |-- preview.py
| | |-- role.py
| | |-- tag.py
| | `-- user.py
| |-- home
| | |-- forms.py
| | |-- __init__.py
| | |-- utils.py
| | `-- views.py
| |-- __init__.py
| |-- models.py
| |-- static
| | |-- asset
| | |-- css
| | |-- js
| | |-- fonts
| | |-- img
| | `-- upload
| | |-- movieImg
| | |-- previewImg
| | `-- userFaceImg
| `-- templates
| |-- admin
| | |-- admin
| | | |-- add.html
| | | `-- list.html
| | |-- aside.html
| | |-- auth
| | | |-- add.html
| | | |-- edit.html
| | | `-- list.html
| | |-- base.html
| | |-- collect
| | | `-- list.html
| | |-- comment
| | | `-- list.html
| | |-- login.html
| | |-- logs
| | | |-- admin_log.html
| | | |-- operate_log.html
| | | `-- user_log.html
| | |-- movie
| | | |-- add.html
| | | |-- edit.html
| | | `-- list.html
| | |-- preview
| | | |-- add.html
| | | |-- edit.html
| | | `-- list.html
| | |-- pwd.html
| | |-- role
| | | |-- add.html
| | | |-- edit.html
| | | `-- list.html
| | |-- tag
| | | |-- add.html
| | | |-- edit.html
| | | `-- list.html
| | `-- user
| | |-- list.html
| | `-- view.html
| |-- home
| | |-- base.html
| | |-- comments.html
| | |-- index.html
| | |-- login.html
| | |-- moviecollect.html
| | |-- play.html
| | |-- pwd.html
| | |-- register.html
| | |-- user.html
| | |-- userlog.html
| | `-- usermenu.html
| `-- macro
| `-- pages.html
|-- config.py
`-- manage.py
在admin的模块包__init__.py中,创建蓝图连接,导出所有的视图函数
from flask import Blueprint
admin=Blueprint('admin',__name__)
from app.admin.views.main import *
from app.admin.views.tag import *
from app.admin.views.movie import *
from app.admin.views.preview import *
from app.admin.views.user import *
from app.admin.views.comment import *
from app.admin.views.collect import *
from app.admin.views.logs import *
from app.admin.views.auth import *
from app.admin.views.role import *
from app.admin.views.admin import *
在app中的__init__中,添加注册后台的蓝图
from app.admin import admin as admin_blueprint
# 注册admin蓝图, url_prefix='/admin'添加前缀/admin
app.register_blueprint(admin_blueprint, url_prefix='/admin')
{% extends 'bootstrap/base.html' %}
{% block doc %}
微电影-{% block title %} {% endblock %}
微电影后台管理系统
{% include 'admin/aside.html' %}
{#让每个页面都可以获取闪现信息闪现#}
{% for item in get_flashed_messages() %}
{{ item }}
{% endfor %}
{# 参考 路径导航: https://v3.bootcss.com/components/#breadcrumbs #}
微电影管理系统
{% block content %}
Page content goes here
{% endblock %}
{% endblock %}
{% macro paginate(PageObj, viewFun) %}
{% endmacro %}
from functools import wraps
from flask import session, flash, redirect, url_for, request, abort
from app import db
from app.models import AdminOplog, Admin, Auth
def is_admin_login(f):
"""用来判断用户是否登录成功"""
@wraps(f)
def wrapper(*args, **kwargs):
# 判断session对象中是否有seesion['admin'],
# 如果包含信息, 则登录成功, 可以访问主页;
# 如果不包含信息, 则未登录成功, 跳转到登录界面;;
if session.get('admin', None):
return f(*args, **kwargs)
else:
flash("管理员必须登录才能访问%s" % (f.__name__))
return redirect(url_for('admin.login'))
return wrapper
def write_adminlog(content):
"""将操作日志写入数据库中"""
adminOplog = AdminOplog(
admin_id=session.get('admin_id'),
content=content,
ip=request.remote_addr
)
db.session.add(adminOplog)
db.session.commit()
def permission_control(f):
"""判断管理员是否有权限操作,(如果是超级管理员, 则全部可以操作) 如果没有权限抛出403"""
@wraps(f)
def wrapper(*args, **kwargs):
admin = Admin.query.get_or_404(session.get('admin_id'))
if not admin.is_super: # 如果不是超级用户, xxxx
# 获取当前用户拥有的权限, 默认是字符串'1,2,3'
auths = admin.role.auths
# 获取所有的权限列表;
all_auth = Auth.query.all()
# 获取管理员可以访问的路由地址
admin_urls = []
auths = map(int, auths.split(',')) # 权限id号
for auth in all_auth:
for auth_id in auths:
# 若管理员有全部权限中的某一项,则添加到该管理员的可访问路由地址中
if auth_id == auth.id:
admin_urls.append(auth.url)
print("管理员可以访问的路由地址:", admin_urls)
print("管理员正在访问的url路由地址:", request.url_rule)
if str(request.url_rule) not in admin_urls:
abort(404)
return f(*args, **kwargs)
return wrapper
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, FileField, TextAreaField
from wtforms.validators import DataRequired, Length, EqualTo, Email, Regexp
from flask_wtf.file import FileAllowed
class BaseForm(FlaskForm):
username = StringField(
label="用户名",
validators=[
DataRequired()
]
)
password = PasswordField(
label="密码",
validators=[
DataRequired(),
# Length(6, 12, message="密码长度必须为6-12")
]
)
class LoginForm(BaseForm):
submit = SubmitField(
label="登录"
)
class PwdForm(FlaskForm):
old_pwd = PasswordField(
label="旧密码",
validators=[
DataRequired()
],
#
render_kw = {
'placeholder' : "请输入旧密码"
}
)
new_pwd = PasswordField(
label="新密码",
validators=[
DataRequired()
],
render_kw={
'placeholder': "请输入新密码"
}
)
submit = SubmitField(
label="修改密码"
)
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)
登录前必须创建一个超级用户,页面是不能创建用户的
在数据库的操作的时候,已经添加该命令,查看基于flask的微电影的数据库创建那节
{% extends 'bootstrap/base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block title %}
登录
{% endblock %}
{% block content %}
登录
{{ wtf.quick_form(form) }}
{% endblock %}
{% extends 'admin/base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block title %}
修改密码
{% endblock %}
{% block content %}
{# 右侧用户中心 #}
{# 面板中心 #}
{# 面板头部 #}
修改密码
{# 面板主体 #}
{{ wtf.quick_form(form) }}
{% endblock %}