Skip to content

Commit

Permalink
添加登录页面功能
Browse files Browse the repository at this point in the history
  • Loading branch information
v_dufyang committed Mar 29, 2020
1 parent b26b103 commit 8e596b9
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 32 deletions.
9 changes: 5 additions & 4 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
# coding:utf8
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from app.admin import admin as admin_blueprint
from app.home import home as home_blueprint
# from app import db

app = Flask(__name__)
app.debug = True

# DB init
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123456@192.168.168.100:30006/movie"
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123456@10.71.70.129:3306/movie"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SECRET_KEY'] = 'cb34xxxxxxxxxxxxxxxxxxbae30d90f6'
db = SQLAlchemy(app)

# 导入蓝图
from app.admin import admin as admin_blueprint
from app.home import home as home_blueprint

app.register_blueprint(admin_blueprint, url_prefix="/admin")
app.register_blueprint(home_blueprint)

Expand Down
48 changes: 48 additions & 0 deletions app/admin/forms.py
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
# -*- coding: utf8 -*-

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, ValidationError
from app.models import Admin # 导入数据库管理员表的那个类
import sys
reload(sys)
sys.setdefaultencoding('utf8')


class LoginForm(FlaskForm):
"""管理员登录表单"""
account = StringField(
label="账号",
validators=[
DataRequired("请输入账号!")
],
description="账号",
render_kw={
"class": "form-control",
"placeholder": "请输入账号!",
# "required": "required"
}
)
pwd = PasswordField(
label="密码",
validators=[
DataRequired("请输入密码!")
],
description="密码",
render_kw={
"class": "form-control",
"placeholder": "请输入密码!",
# "required": "required"
}
)
submit = SubmitField(
'登录',
render_kw={
"class": "btn btn-primary btn-block btn-flat",
}
)

def validate_account(self, field):
account = field.data
admin = Admin.query.filter_by(name=account).count()
if admin == 0:
raise ValidationError("账号不存在!")
132 changes: 129 additions & 3 deletions app/admin/views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,65 @@
# -*- coding: utf8 -*-
import datetime
from . import admin
from flask import render_template, redirect, url_for
from flask import render_template, redirect, url_for, flash, request, session
from app.models import Admin
from forms import LoginForm
from functools import wraps


# 装饰器用来进行访问控制
def admin_login_req(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if "admin" not in session:
return redirect(url_for("admin.login", next=request.url))
return f(*args, **kwargs)

return decorated_function

# 后端首页
@admin.route("/")
def index():
return render_template("admin/index.html")

# 后端登录
@admin.route("/login/")
@admin.route("/login/", methods=['GET', 'POST'])
def login():
return render_template("admin/login.html")
form = LoginForm()
# print form.account.data
if form.validate_on_submit():
data = form.data
print data
admin = Admin.query.filter_by(name=data['account']).first()
print admin
if not admin.check_pwd(data['pwd']):
print "111111"
# flash("密码错误!", 'err')
flash("账号或密码错误! ",'err')
return redirect(url_for('admin.login'))
print 2222
session['admin'] = data['account']
return redirect(request.args.get('next') or url_for('admin.index'))

return render_template("admin/login.html", form=form)


# form = LoginForm()
# if form.validate_on_submit():
# # data = form.data
# # admin = Admin.query.filter_by(name=data['account']).first()
# if not admin.check_pwd(data['pwd']):
# flash("账号或密码错误! ")
# return redirect(url_for('admin.login'))
# session['admin'] = data['account']
# return redirect((request.args.get('next') or url_for('admin.index')))
# return render_template('admin/login.html', form=form)

# 后端退出
@admin.route("/logout/")
@admin_login_req
def logout():
session.clear()
return redirect(url_for("admin.login"))

# 后端修改密码
Expand All @@ -31,3 +76,84 @@ def tag_add():
@admin.route("/tag/list/")
def tag_list():
return render_template("admin/tag_list.html")


# 后端添加电影
@admin.route("/movie/add/")
def movie_add():
return render_template("admin/movie_add.html")

# 后端添加电影
@admin.route("/movie/list/")
def movie_list():
return render_template("admin/movie_list.html")

# 后端添加预告
@admin.route("/preview/add/")
def preview_add():
return render_template("admin/preview_add.html")

# 后端预告列表
@admin.route("/preview/list/")
def preview_list():
return render_template("admin/preview_list.html")

# 后端会员列表
@admin.route("/user/list/")
def user_list():
return render_template("admin/user_list.html")

# 后端评论列表
@admin.route("/comment/list/")
def comment_list():
return render_template("admin/comment_list.html")

# 后端收藏列表
@admin.route("/moviecol/list/")
def moviecol_list():
return render_template("admin/moviecol_list.html")

# 后端操作日志列表
@admin.route("/oplog/list/")
def oplog_list():
return render_template("admin/oplog_list.html")

# 后端管理员登录日志列表
@admin.route("/adminloginlog/list/")
def adminloginlog_list():
return render_template("admin/adminloginlog_list.html")

# 后端管理员登录日志列表
@admin.route("/userloginlog/list/")
def userloginlog_list():
return render_template("admin/userloginlog_list.html")

# 后端添加权限
@admin.route("/auth/add/")
def auth_add():
return render_template("admin/auth_add.html")

# 后端权限列表
@admin.route("/auth/list/")
def auth_list():
return render_template("admin/auth_list.html")

# 后端添加角色
@admin.route("/role/add/")
def role_add():
return render_template("admin/role_add.html")

# 后端角色列表
@admin.route("/role/list/")
def role_list():
return render_template("admin/role_list.html")

# 后端添加管理员
@admin.route("/admin/add/")
def admin_add():
return render_template("admin/admin_add.html")

# 后端管理员列表
@admin.route("/admin/list/")
def admin_list():
return render_template("admin/admin_list.html")
37 changes: 20 additions & 17 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from app import db


app = Flask(__name__)
# 连接数据库
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:[email protected]:30006/movie"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True

db = SQLAlchemy(app)
# app = Flask(__name__)
# # 连接数据库
# app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:[email protected]:3306/movie"
# app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
# db = SQLAlchemy(app)


# 定义数据库表
Expand All @@ -26,9 +25,9 @@ class User(db.Model):
face = db.Column(db.String(255), unique=True)
addtim = db.Column(db.DateTime, index=True, default=datetime.now())
uuid = db.Column(db.String(255), unique=True)
userlogs = db.relationship('Userlog', backerf='user') # 会员日志外键关系
comments = db.relationship('Comment', backerf='user') # 评论外键关系
moviecols = db.relationship('Moviecol', backerf='user') # 评论外键关系
userlogs = db.relationship('Userlog', backref='user') # 会员日志外键关系
comments = db.relationship('Comment', backref='user') # 评论外键关系
moviecols = db.relationship('Moviecol', backref='user') # 评论外键关系

def __repr__(self):
return "<User %r>" % self.name
Expand Down Expand Up @@ -74,8 +73,8 @@ class Movie(db.Model):
release_time = db.Column(db.Date) # 上映时间
length = db.Column(db.String(100)) # 播放时间
addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间
comments = db.relationship('Comment', backerf='movie') # 评论外键关系
moviecols = db.relationship('Moviecol', backerf='movie') # 电影收藏外键关系
comments = db.relationship('Comment', backref='movie') # 评论外键关系
moviecols = db.relationship('Moviecol', backref='movie') # 电影收藏外键关系

def __repr__(self):
return "<Movie %r>" % self.title
Expand Down Expand Up @@ -137,7 +136,7 @@ class Role(db.Model):
name = db.Column(db.String(100), unique=True) # 名称
auths = db.Column(db.String(600)) # 角色
addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间
admins = db.relationship('Admin', backerf='role') # 管理员外键关系
admins = db.relationship('Admin', backref='role') # 管理员外键关系

def __repr__(self):
return "<Role %r>" % self.name
Expand All @@ -152,12 +151,16 @@ class Admin(db.Model):
is_super = db.Column(db.SmallInteger) # 是否为超级管理员 0为超级管理员
role_id = db.Column(db.Integer, db.ForeignKey('role.id')) # 所属角色
addtime = db.Column(db.DateTime, index=True, default=datetime.now) # 添加时间
Adminlogs = db.relationship('Adminlog', backerf='admin') # 管理员登陆日志外键关系
oplogs = db.relationship('Oplog', backerf='admin') # 管理员登陆日志外键关系
Adminlogs = db.relationship('Adminlog', backref='admin') # 管理员登陆日志外键关系
oplogs = db.relationship('Oplog', backref='admin') # 管理员登陆日志外键关系

def __repr__(self):
return "<Admin %r>" % self.name

def check_pwd(self, pwd):
from werkzeug.security import check_password_hash
return check_password_hash(self.pwd, pwd)


# 管理员登陆日志
class Adminlog(db.Model):
Expand Down Expand Up @@ -191,7 +194,7 @@ def __repr__(self):
# 2.插入数据
# 这里插入一条角色的数据
"""
role = Role(
role = Role(
name="超级管理员",
auths="0")
db.session.add(role)
Expand All @@ -202,7 +205,7 @@ def __repr__(self):
from werkzeug.security import generate_password_hash # 导入生成密码的工具

admin = Admin(
namr="admin",
name="admin",
pwd=generate_password_hash("123456"),
is_super=0,
role_id=1
Expand Down
26 changes: 18 additions & 8 deletions app/templates/admin/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,33 @@
<a href=""><b>飞飞电影管理系统</b></a>
</div>
<div class="login-box-body">
<p class="login-box-msg"></p>
{% for message in get_flashed_messages() %}
<p class="login-box-msg" style="color: red">{{ message }}</p>
{% endfor %}
<form action="" method="post" id="form-data">
<div class="form-group has-feedback">
<input name="user" type="text" class="form-control" placeholder="请输入账号!">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
<div class="col-md-12" id="input_user"></div>
{{ form.account }}
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
{% for err in form.account.errors %}
<div class="col-md-12">
<span style="color:red">{{ err }}</span>
</div>
{% endfor %}

</div>
<div class="form-group has-feedback">
<input name="pwd" type="password" class="form-control" placeholder="请输入密码!">
{{ form.pwd }}
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
<div class="col-md-12" id="input_pwd"></div>
{% for err in form.pwd.errors %}
<div class="col-md-12" id="input_pwd" style="color: red">{{ err }}</div>
{% endfor %}
</div>
<div class="row">
<div class="col-xs-8">
</div>
<div class="col-xs-4">
<a id="btn-sub" type="submit" class="btn btn-primary btn-block btn-flat">登录</a>
{{ form.submit }}
{{ form.csrf_token }}
</div>
</div>
</form>
Expand All @@ -44,4 +54,4 @@
<script src="{{ url_for('static',filename='admin/bootstrap/js/bootstrap.min.js') }}"></script>
<script src="{{ url_for('static',filename='admin/plugins/iCheck/icheck.min.js') }}"></script>
</body>
</html>
</html>

0 comments on commit 8e596b9

Please sign in to comment.