-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
254 lines (194 loc) · 6.31 KB
/
Copy pathapp.py
File metadata and controls
254 lines (194 loc) · 6.31 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import psycopg2
from flask import Flask, render_template, request, redirect, session
from flask_cors import CORS
############ flask setting ############
app = Flask(__name__)
app.secret_key = "COSECOSECOSE"
CORS(app)
connect = psycopg2.connect(dbname="termproject", user="postgres", password="1234")
cur = connect.cursor() # create cursor
############ no cache setting ############
@app.after_request
def set_response_headers(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
return r
############ main (login) page ############
@app.route("/", methods=["GET", "POST"])
def main():
print(request.form)
return render_template("main.html")
def validation_id(id):
if len(id) == 0:
return "id can't be empty"
if len(id) >= 20:
return f"id({id}) is so long (id's length should be shorter than 20)"
return ""
def validation_password(password):
if len(password) == 0:
return "password can't be empty"
if len(password) > 20:
return f"password({password}) is so long (password's length should be shorter than 20)"
return ""
def login(id: str, password: str):
try:
cur.execute("SELECT count(*) from users where id = %s and password = %s;", (id, password))
result = cur.fetchall()
if result[0][0] == 1:
session["id"] = id
return redirect("/action", code=302)
else:
return redirect("/login_failed", code=302)
except Exception as err:
return f"{err}"
def sign_up(id, password):
try:
print(id, password)
cur.execute("SELECT count(*) from users where id = %s;", (id,))
print(id, password)
result = cur.fetchall()
print(id, password)
if result[0][0] == 1:
return f"id {id} already exists try again"
cur.execute("INSERT INTO users VALUES(%s, %s);", (id, password))
cur.execute("INSERT INTO account VALUES(%s, %s, %s);", (id, 10000, "beginner"))
return f"completely sign up id = {id} and password = {password}"
except Exception as err:
print(err)
return f"{err}"
@app.route("/register", methods=["GET", "POST"])
def register():
id = request.form["id"]
password = request.form["password"]
send = request.form["send"]
err_msg = validation_id(id)
if err_msg != "":
return err_msg
err_msg = validation_password(password)
if err_msg != "":
return err_msg
if send == "login":
return login(id=id, password=password)
if send == "sign up":
return sign_up(id=id, password=password)
return "invalid request try again please"
@app.route("/login_failed", methods=["GET", "POST"])
def login_failed():
return render_template("login_failed.html")
@app.route("/return", methods=["GET", "POST"])
def re_turn():
print(request.form)
return redirect("/", code=302)
############ main page ############
def users_info():
cur.execute("SELECT * FROM users;")
result = cur.fetchall()
return render_template("users_info.html", users=result)
def trades_info():
cur.execute("SELECT * FROM trade;")
result = cur.fetchall()
return render_template("trades_info.html", trades=result)
@app.route("/admin_func", methods=["GET", "POST"])
def admin_func():
print(request.form)
send = request.form["send"]
if send == "users_info":
return users_info()
if send == "trades_info":
return trades_info()
return "invalid request try again please"
@app.route("/logout", methods=["GET", "POST"])
def logout():
session["id"] = ""
print(request.form)
return redirect("/", code=302)
def get_popular_category():
sql = """
select c.type
from category as c, (
SELECT code, COUNT(code) as cnt
FROM trade
GROUP BY code) as G
where c.code = G.code
order by G.cnt DESC
limit 1;
"""
cur.execute(sql)
result = cur.fetchall()
if len(result) == 0:
return "there is no item"
return result[0][0]
def get_best_people(type):
sql = f"""
select {type}, sum(trade_price) as S
from trade
GROUP BY {type}
ORDER BY S DESC
limit 1;
"""
cur.execute(sql)
result = cur.fetchall()
if len(result) == 0:
return f"there is no {type}"
return result[0][0]
def get_user_info(id):
sql = """
SELECT * FROM account where id = %s;
"""
print(id)
cur.execute(sql, (id,))
result = cur.fetchall()
return {
"id": result[0][0],
"balance": result[0][1],
"rating": result[0][2],
}
def get_all_items():
sql = """
SELECT * FROM items;
"""
cur.execute(sql)
result = cur.fetchall()
return result
@app.route("/action")
def action():
print(request.form)
id = session["id"]
popular_category = get_popular_category()
best_buyer = get_best_people("buyer")
best_seller = get_best_people("seller")
items = get_all_items()
user_info = get_user_info(id)
return render_template(
"action.html",
admin_display="show" if id == "admin" else "none",
id=id,
balance=user_info["balance"],
rating=user_info["rating"],
popular_category=popular_category,
best_buyer=best_buyer,
best_seller=best_seller,
items=items,
)
############ item add page ############
@app.route("/item_add")
def item_add():
return render_template("main.html")
############ item buy page ############
@app.route("/item_buy")
def item_buy():
return render_template("main.html")
############ item buying page ############
@app.route("/item_buying")
def item_buying():
return render_template("main.html")
############ etc ############
@app.route("/favicon.ico")
def favicon():
return send_from_directory(
os.path.join(app.root_path, "static"), "favicon.ico", mimetype="image/vnd.microsoft.icon"
)
############ main ############
if __name__ == "__main__":
app.run()