Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
168 lines (156 loc) · 6.02 KB
/
Copy pathmain.py
File metadata and controls
168 lines (156 loc) · 6.02 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#-*- coding : utf-8-*-
from flask import Flask,render_template,make_response,request,redirect
import source.user as database_user
import source.book as database_book
import traceback
app = Flask(__name__)
@app.route("/")
def mainpage():
acc="1"
try:
username=request.cookies.get('username')
password=request.cookies.get('password')
status=database_user.check_user(username,password)
if status==1:
pass
elif status==0:
raise Exception("Wrong User")
else:
raise Exception("UNKNOWN WRONG")
except:
acc="0"
username="EMPTY"
password="EMPTY"
res=make_response(render_template('mainpage.html',username=username,password=password,acc=acc))
res.set_cookie("acc",str(acc),max_age=3600)
return res
@app.route("/login/",methods=["GET","POST"])
def login():
acc=request.cookies.get('acc')
warning=""
if request.method=="POST":
try:
username=request.form['name']
password=request.form['password']
status=database_user.check_user(username,password)
if status==1:
res=make_response('''登录成功,<a href="/">点我返回主页</a>。''')
res.set_cookie("username",username,max_age=3600)
res.set_cookie("password",password,max_age=3600)
res.set_cookie("acc","1",max_age=3600)
return res
elif status==0:
warning="用户名或密码错误"
else:
raise Exception("UNKNOWN WRONG")
except:
warning="未知错误,请稍后重试或联系管理员"
traceback.print_exc()
return render_template('login.html',warning=warning,acc=acc)
@app.route("/register/",methods=["GET","POST"])
def register():
acc=request.cookies.get('acc')
warning=""
if request.method=="POST":
try:
username=request.form['name']
password=request.form['password']
status=database_user.add_user(username,password)
if status==1:
return '''注册成功,请<a href="/login/">跳转登录</a>。'''
elif status==2:
warning="该用户名已被注册"
else:
raise Exception("UNKNOWN WRONG")
except:
warning="未知错误,请稍后重试或联系管理员"
traceback.print_exc()
return render_template('register.html',warning=warning,acc=acc)
@app.route("/logout/")
def logout():
res=make_response('''退出登录成功,<a href="/">点我返回主页</a>''')
res.set_cookie("acc","0",max_age=3600)
res.delete_cookie("username")
res.delete_cookie("password")
return res
@app.route("/book/")
def book():
acc=request.cookies.get('acc')
book_list=database_book.list_all()
return render_template('book.html',book_list=book_list,acc=acc)
@app.route("/book/add/")
def book_add():
acc=request.cookies.get('acc')
if acc=="1":
username=request.cookies.get('username')
bookname=request.values.get("bookname")
place=request.values.get("place")
if (not bookname) or (not place):
return render_template('book_add.html')
if database_book.add_book(bookname,place,username):
return '''添加成功, <a href="/book/add/">点我继续添加</a>, <a href="/book/">点我返回书籍页面</a>。'''
else:
return '''未知错误, <a href="javascript:location.reload()">点我重试</a>, <a href="/book/">点我返回书籍页面</a>。'''
else:
return '''请先<a href="/login/">登录</a>。'''
@app.route("/book/delete/")
def book_delete():
acc="1"
book_id=request.values.get("id")
if not book_id:
return '''未选择删除书籍,<a href="/">返回首页</a>。'''
try:
username=request.cookies.get('username')
password=request.cookies.get('password')
status=database_user.check_user(username,password)
print(status)
if status==1:
if database_book.delete_book(book_id,username):
return '''删除成功,请关闭此标签页'''
else:
return '''删除失败,可能原因:<br/>1、您尝试删除他人书籍<br/>2、书籍已经删除<br/>3、未知原因,请联系管理员'''
elif status==0:
raise Exception("Wrong User")
else:
raise Exception("UNKNOWN WRONG")
except:
traceback.print_exc()
return '''请重新<a href="/login/">登录</a>。'''
@app.route("/book/clean/")
def book_clean():
acc="1"
book_id=request.values.get("id")
clean=request.values.get("to")
if not book_id:
return '''未选择书籍,<a href="/">返回首页</a>。'''
if not clean:
return '''未选择更改状态,<a href="/">返回首页</a>。'''
try:
username=request.cookies.get('username')
password=request.cookies.get('password')
status=database_user.check_user(username,password)
print(status)
if status==1:
if database_book.clean_change(book_id,clean,username):
return '''消毒状态更改成功,请关闭此标签页'''
else:
return '''状态更改失败,未知原因,请联系管理员'''
elif status==0:
raise Exception("Wrong User")
else:
raise Exception("UNKNOWN WRONG")
except:
traceback.print_exc()
return '''请重新<a href="/login/">登录</a>。'''
@app.route("/person/")
def person():
acc=request.cookies.get('acc')
if acc=="1":
username=request.cookies.get('username')
book_list=database_book.list_all(user=username)
return render_template('person.html',username=username,book_list=book_list)
else:
return '''请先<a href="/login/">登录</a>。'''
if __name__ == "__main__":
app.run(host="0.0.0.0")#添加参数以共享至局域网内
#app.run(debug=True,use_reloader=False)