-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_page.py
159 lines (110 loc) · 4.4 KB
/
index_page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from flask import *
import logging
# from flask_cors import *
import databaseModel
import UserPage
import AdminPage
import StaffPage
from flask_sqlalchemy import SQLAlchemy
from functools import wraps
import ReadConfig
def CreateApp():
app = Flask(__name__, static_url_path="")
app.register_blueprint(UserPage.bp)
app.register_blueprint(StaffPage.bp)
app.register_blueprint(AdminPage.bp)
config = ReadConfig.readconfig("./config.json")
app.secret_key = config['key_secret']
app.config['SQLALCHEMY_DATABASE_URI'] = config["databaseAddr"]
app.config['SQLALCHEMY_COMMIT_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
return app
app= CreateApp()
db = SQLAlchemy(app)
@app.route('/index',methods=['GET'])
def index():
return render_template("index.html")
@app.route('/',methods=['GET'])
def index2():
return redirect(url_for("index"))
@app.route('/Login', methods=['POST'])
def Login():
username,pwd,role = request.form['username'],request.form['pwd'],request.form['role']
if(role == 'user'):
try:
pwdInData = databaseModel.Users.query.filter_by(UserName=username).first()
if(pwdInData == None): #用户不存在
# current_app.logger.info("Logerror")
return redirect(url_for("LoginError"))
if(pwdInData.PassWord == pwd): #登录成功
current_app.logger.debug("Log success")
session['UserName'] = username
return redirect(url_for("UserPage"))
else:
return redirect(url_for("LoginError")) #密码错误
except Exception as e: ## 数据库错误
current_app.logger.debug(e)
if(role == 'admin'):
try:
pwdInData = databaseModel.Admins.query.filter_by(UserName=username).first()
if(pwdInData == None): #用户不存在
# current_app.logger.info("Logerror")
return redirect(url_for("LoginError"))
if(pwdInData.PassWord == pwd): #登录成功
current_app.logger.debug("Log success")
session['UserName'] = username
return redirect(url_for("AdminPage"))
else:
return redirect(url_for("LoginError")) #密码错误
except Exception as e: ## 数据库错误
current_app.logger.debug(e)
if(role == 'staff'):
try:
pwdInData = databaseModel.Staffs.query.filter_by(UserName=username).first()
if(pwdInData == None): #用户不存在
# current_app.logger.info("Logerror")
return redirect(url_for("LoginError"))
if(pwdInData.PassWord == pwd): #登录成功
current_app.logger.debug("Log success")
session['UserName'] = username
return redirect(url_for("StaffPage"))
else:
return redirect(url_for("LoginError")) #密码错误
except Exception as e: ## 数据库错误
current_app.logger.debug(e)
@app.route('/LogOut')
def LogOut():
session.pop('UserName', None)
# return redirect("/index")
return "200"
def login_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
UserName = session.get('UserName')
if(UserName is not None):
g.UserName = UserName
return func(*args, **kwargs)
else:
return redirect(url_for("index"))
return wrapper
@app.route('/UserPage',methods=["GET","POST"])
@login_required
def UserPage():
return render_template("userpage.html")
@app.route('/StaffPage',methods=["GET","POST"])
@login_required
def StaffPage():
return render_template("staffview.html")
@app.route("/AdminPage", methods=["GET","POST"])
@login_required
def AdminPage():
return render_template("adminview.html")
@app.route("/LoginError")
def LoginError():
return render_template("LoginError.html")
@app.errorhandler(500)
def error(e):
return render_template('index.html'), 500
if __name__ == '__main__':
app.run(host='0.0.0.0',port='80',debug=True)