-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
306 lines (246 loc) · 9.68 KB
/
main.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from http.client import REQUEST_URI_TOO_LONG
import re
from flask import Flask, session, render_template, redirect, url_for, request
# from ds import products
import sqlite3
from livereload import Server
import re
import time
app = Flask('app')
app.secret_key = "CHANGE ME, ok"
@app.route('/')
def main_page():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
args = request.args
args.to_dict()
if ("category" in args.keys()):
category = args["category"]
cursor.execute("SELECT * FROM product WHERE category=?", (category,))
else:
cursor.execute("SELECT * FROM product;")
products = cursor.fetchall()
cursor.execute("select * from category;")
categories = cursor.fetchall()
print("len: " + str(len(categories)))
return render_template("home.html", products=products, categories=categories)
@app.route('/product')
def product():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
args = request.args
args.to_dict()
if "name" in args.keys():
name = args["name"]
cursor.execute("SELECT * FROM product WHERE name=?", (name,))
product = cursor.fetchone()
elif "id" in args.keys():
id = args["id"]
cursor.execute("SELECT * FROM product WHERE id=?", (id,))
product = cursor.fetchone()
if product:
return render_template("product.html", product=product)
else:
return render_template("error.html")
@app.route('/add-to-cart', methods=["POST","GET"])
def add_to_cart():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
id = request.form["product-id"]
name = request.form["product-name"]
cart = session["cart"]
print(request.form)
if (id in cart.keys()):
cart[id]["quantity"] += 1
else:
cart[id] = {
"name": name,
"quantity": 1
}
session["cart"] = cart
return redirect("/cart")
@app.route('/login', methods=["GET", "POST"])
def login():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
if request.method == "POST":
email = request.form["email"]
password = request.form["password"]
cursor.execute("SELECT * FROM user WHERE email=? AND password=?", (email, password))
user = cursor.fetchone()
if user:
session["email"] = email
session["name"] = user["name"]
session["cart"] = {}
return redirect("/")
else:
error = "Invalid email or password!"
return render_template("login.html", error=error)
return render_template("login.html")
@app.route('/signup', methods=["GET", "POST"])
def signup():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("select user from user;")
users = cursor.fetchall()
if request.method == "POST":
email = request.form["email"]
password = request.form["password"]
name = request.form["name"]
pat = "^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\.[a-z]{1,3}$"
if not re.match(pat, email):
error = "Invalid email!"
return render_template("signup.html", error=error)
elif len(password) < 4:
error = "Password length must be at least 4"
return render_template("signup.html", error=error)
elif name == "":
error = "Name cannot be blank!"
return render_template("signup.html", error=error)
else:
cursor.execute("INSERT INTO user VALUES (?,?,?)", (email, name, password))
connection.commit()
error = "Account succesfully created!"
return render_template("login.html", error=error)
return render_template("signup.html")
@app.route('/cart', methods=["POST", "GET"])
def cart():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
msg = ""
if request.method == "GET":
if session.get("email"):
cart = session["cart"]
print(cart)
print(type(cart))
return render_template("cart.html", cart=cart)
else:
return redirect("login")
else: # POST
cart = session["cart"]
if request.form["action"] == "checkout":
total = 0
cur_time = time.strftime('%Y-%m-%d %H:%M:%S')
if (not cart.keys()):
msg = "Can't checkout empty cart!"
return render_template("cart.html", cart=cart, msg=msg)
for id in cart.keys():
cursor.execute("select * from product where id = ?", (id,))
product = cursor.fetchone()
new_qty = int(request.form["quantity-" + str(id)])
if new_qty > product["stock"]:
msg = f"There are {product['stock']} {product['name']} in stock! You specified {new_qty}"
return render_template("cart.html", cart=cart, msg=msg)
elif new_qty < 0:
msg = f"We don't know how to deliver {new_qty} {product['name']}"
return render_template("cart.html", cart=cart, msg=msg)
# if no error in quantities -> checkout
for id in cart.keys():
cursor.execute("select * from product where id = ?", (id,))
product = cursor.fetchone()
new_qty = int(request.form["quantity-" + str(id)])
total += new_qty * product["price"]
cursor.execute("update product set stock = ? where id = ?", (product['stock']-new_qty, id))
cursor.execute("insert into order_product values (?,?,?,?)", (session["email"],cur_time,id,new_qty))
connection.commit()
session["cart"] = {}
print(cur_time)
cursor.execute("insert into orders values (?,?,?)", (session["email"], cur_time, total))
connection.commit()
elif request.form["action"] == "update":
copy_cart = cart.copy()
for id in copy_cart:
cursor.execute("select * from product where id = ?", (id,))
product = cursor.fetchone()
new_qty = int(request.form["quantity-" + str(id)])
if (new_qty == 0):
cart.pop(id)
session["cart"] = cart
elif new_qty > product["stock"]:
msg = f"There are {product['stock']} {product['name']} in stock! You specified {new_qty}"
elif new_qty < 0:
msg = f"We don't know how to deliver {new_qty} {product['name']}"
else: # VALID INPUT
cart[id]["quantity"] = new_qty
session["cart"] = cart
elif request.form["action"] == "delete":
session["cart"] = {}
cart = session["cart"]
return render_template("cart.html", cart=cart, msg=msg)
@app.route('/orders', methods=["POST", "GET"])
def orders():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
args = request.args
args.to_dict()
product = ""
if ("product" in args):
product = args["product"]
# cursor.execute("SELECT * FROM orders JOIN order_product ON orders.order_date = order_product.date_of_order JOIN product ON order_product.product_id = product.id WHERE customer_email=?", (email,))
# products = cursor.fetchall()
# if (product):
# cursor.execute("SELECT * FROM orders JOIN order_product ON order_product.date_of_order = orders.order_date JOIN product ON product.id = order_product.product_id where customer_email=? and product.name=? ORDER BY order_date DESC", (email, product))
# else:
# cursor.execute("SELECT * FROM orders where customer_email=? ORDER BY order_date DESC", (email,))
# orders = cursor.fetchall()
if session.get("email"):
if "sort" in args.keys():
if args["sort"] == "new":
email = session["email"]
cursor.execute("SELECT * FROM orders JOIN order_product ON orders.order_date = order_product.date_of_order JOIN product ON order_product.product_id = product.id WHERE customer_email=?", (email,))
products = cursor.fetchall()
cursor.execute("SELECT * FROM orders where customer_email=? ORDER BY order_date DESC", (email,))
orders = cursor.fetchall()
return render_template("orders.html", products=products, orders=orders, product=product)
elif args["sort"] == "old":
email = session["email"]
cursor.execute("SELECT * FROM orders JOIN order_product ON orders.order_date = order_product.date_of_order JOIN product ON order_product.product_id = product.id WHERE customer_email=?", (email,))
products = cursor.fetchall()
cursor.execute("SELECT * FROM orders where customer_email=? ORDER BY order_date ASC", (email,))
orders = cursor.fetchall()
return render_template("orders.html", products=products, orders=orders, product=product)
else:
email = session["email"]
cursor.execute("SELECT * FROM orders JOIN order_product ON orders.order_date = order_product.date_of_order JOIN product ON order_product.product_id = product.id WHERE customer_email=?", (email,))
products = cursor.fetchall()
if (product):
cursor.execute("SELECT * FROM orders JOIN order_product ON order_product.date_of_order = orders.order_date JOIN product ON product.id = order_product.product_id where customer_email=? and product.name=? ORDER BY order_date DESC", (email, product))
else:
cursor.execute("SELECT * FROM orders where customer_email=? ORDER BY order_date DESC", (email,))
orders = cursor.fetchall()
return render_template("orders.html", products=products, orders=orders, product=product)
else:
return redirect("/login")
@app.route('/sort-orders')
def sort_orders():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
args = request.args
args.to_dict()
if args["sort"] == "newest":
return redirect('/orders?sort=new')
elif args["sort"] == "oldest":
return redirect('/orders?sort=old')
@app.route('/logout', methods=["POST"])
def logout():
connection = sqlite3.connect("shop.db")
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
page = request.form["page"]
session.clear()
if (page == "login"):
return render_template("login.html")
elif (page == "signup"):
return render_template("signup.html")
# if __name__ == '__main__':
# app.run(debug=True)
# # app.run(host='0.0.0.0', port=8080)
app.run(host='0.0.0.0', port=8080)