-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
367 lines (284 loc) · 12.2 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import os
import queries
from cart import Cart
from helpers import verify_signin, verify_signup
from create_fake_accounts import create_fake_db_accounts
from flask import Flask, redirect, render_template, request, session, url_for, flash, abort
app = Flask(__name__)
app.secret_key = os.urandom(16)
create_fake_db_accounts() # comment out this line after db initialised
@app.route('/')
def frontpage():
featured_products = queries.get_newest_products()
return render_template("home_page.html", products=featured_products)
@app.route('/home/')
def home():
return redirect(url_for("frontpage"))
@app.route('/sign_up/', methods=['GET', 'POST'])
def sign_up():
if request.method == "POST":
# grab user info from form
username = request.form["username"]
fullname = request.form["full_name"]
address = request.form["address"]
bio = request.form["bio"]
avatar_url = request.form["avatar_url"]
password = request.form["password"]
user_is_valid = verify_signup(
username, fullname, address, bio, avatar_url, password)
if user_is_valid:
# set session user
user = queries.get_user(username)
session["user"] = user
return redirect(url_for("frontpage"))
else:
return redirect(url_for('sign_up'))
else: # request.method == "GET"
if "user" in session:
return redirect(url_for("frontpage"))
return render_template("sign_up.html")
@app.route('/sign_in/', methods=['GET', 'POST'])
def login():
if request.method == "POST":
# retrieve user info
username = request.form["username"]
password = request.form["password"]
login_succesfull = verify_signin(username, password)
if login_succesfull:
# set session usser
user = queries.get_user(username)
session["user"] = user
return redirect(url_for("frontpage"))
else:
return redirect(url_for('login'))
else: # request.method == "GET"
if "user" in session:
return redirect(url_for("frontpage"))
return render_template('log_in.html')
@app.route('/sign_out/')
def logout():
if "user" in session:
session.pop("user", None)
flash("You have been signed out", 'info')
return redirect(url_for("frontpage"))
@app.route('/my_profile/')
def my_profile():
if "user" in session:
user = session["user"]
user_products = queries.list_user_products(user["user_id"])
user_sales = queries.list_user_sales(user["user_id"])
user_purchases = queries.list_user_purchases(user["user_id"])
return render_template('my_profile.html', user=user, products=user_products[:7], sales=user_sales[:7], purchases=user_purchases[:7])
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/my_profile/edit_profile/', methods=['GET', 'POST'])
def edit_profile():
if "user" in session:
user = session["user"]
if request.method == "POST":
# gather updated user info from form
name = request.form["full_name"]
address = request.form["address"]
bio = request.form["bio"]
avatar_url = request.form["avatar_url"]
edit_successful = queries.edit_user(
user["user_id"], name, address, bio, avatar_url) # returns a user dict
if edit_successful:
# reset user info in session
session["user"] = edit_successful
session.modified = True
return redirect(url_for('my_profile'))
else:
flash("Something went wrong. Couldn't update your profile", 'error')
return redirect(url_for('edit_profile'))
else: # request.method == "GET"
return render_template("edit_profile.html", user=user)
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/my_profile/add_product/', methods=['GET', 'POST'])
def add_product():
if "user" in session:
if request.method == "POST":
# get product info from form
title = request.form["title"]
description = request.form["description"]
price = request.form["price"]
qty = request.form["qty"]
user = session["user"]
product = {
"title": title,
"description": description,
"price_in_cents": float(price) * 100, # convert price to cents
"qty": int(qty),
"vendor": user
}
product_id = queries.add_product_to_catalog(
product) # returns a product id if succesfull
if product_id:
# add product images
image_1 = request.form["image_1"]
image_2 = request.form["image_2"]
image_3 = request.form["image_3"]
image_4 = request.form["image_4"]
image_5 = request.form["image_5"]
images = [image_1, image_2, image_3, image_4, image_5]
if images: # if image list isn't empty
queries.add_images_to_product(product_id, images)
# add product tags
tags = request.form["tags"]
tag_list = tags.split(", ")
if tag_list: # if list of tags isn't empty
queries.add_product_tags(product_id, tag_list)
return redirect(url_for("product_page", product_id=product_id, _method='GET'))
else:
flash("Could not add product", 'error')
return redirect(url_for('add_product'))
else: # request.method == "GET"
return render_template("add_product.html")
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/my_profile/edit_product/<product_id>', methods=['GET', 'POST'])
def edit_product(product_id):
if "user" in session:
if request.method == "POST":
# get updated product info from form
title = request.form["title"]
description = request.form["description"]
price = request.form["price"]
qty = request.form["qty"]
thumbnail = request.form["thumbnail"]
edit_successful = queries.edit_product(
product_id, title, description, price, qty, thumbnail)
if edit_successful:
return redirect(url_for("my_products"))
else:
flash("Something went wrong. Couldn't update product", 'error')
return redirect(url_for("edit_product", product_id=product_id, _method='GET'))
else: # request.method == "GET"
product = queries.get_product(product_id)
if product:
return render_template("edit_product.html", product=product)
else:
abort(404)
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/my_profile/remove_product/<product_id>')
def remove_product(product_id):
if "user" in session:
user = session["user"]
product_was_removed = queries.remove_product(
user["user_id"], product_id)
if product_was_removed:
flash("Product has been removed", 'info')
else:
flash("Could not remove product", 'error')
return redirect(url_for("my_products"))
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/my_profile/my_products/')
def my_products():
if "user" in session:
user = session["user"]
products = queries.list_user_products(user["user_id"])
return render_template("my_products.html", products=products)
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/my_profile/my_sales/')
def my_sales():
if "user" in session:
user = session["user"]
sales = queries.list_user_sales(user["user_id"])
return render_template("my_sales.html", sales=sales)
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/my_profile/my_purchases/')
def my_purchases():
if "user" in session:
user = session["user"]
purchases = queries.list_user_purchases(user["user_id"])
return render_template("my_purchases.html", purchases=purchases)
else: # user must be logged in
return redirect(url_for('login'))
@app.route('/user_page/<username>')
def user_page(username):
betsy_user = queries.get_user(username)
if betsy_user:
user_products = queries.list_user_products(betsy_user['user_id'])
return render_template("user_page.html", betsy_user=betsy_user, products=user_products)
else:
abort(404)
@app.route('/product_page/<product_id>', methods=['GET', 'POST'])
def product_page(product_id):
if request.method == "POST": # product added to cart
cart = Cart(session)
# get product and quantity
quantity = int(request.form["quantity"])
user_cart = cart.add_product(
product_id, quantity) # returns a cart dict
# update cart in session
session['cart'] = user_cart
session.modified = True
flash("Item added to cart", 'info')
return redirect(url_for("product_page", product_id=product_id, _method='GET'))
else: # request.method == "GET"
product = queries.get_product(product_id)
if product: # if product exists
product_tags = queries.get_product_tags(product_id)
product_images = queries.get_product_images(product_id)
return render_template("product_page.html", product=product, product_images=product_images, product_tags=product_tags)
else:
abort(404)
@app.route('/products/')
def all_products():
products = queries.get_all_products()
return render_template("products_page.html", query='All Products', products=products)
@app.route('/products/<tag>')
def search_products_by_tag(tag):
tagged_products = queries.list_products_per_tag(tag)
return render_template("products_page.html", query=tag, products=tagged_products)
@app.route('/products/search/')
def search_products():
query = request.args.get("query")
products = queries.search(query)
return render_template("products_page.html", query=query, products=products)
@app.route('/view_cart/', methods=['GET', 'POST'])
def view_cart():
if "user" in session:
cart = Cart(session)
if request.method == "POST": # if user has checked out cart items
buyer_id = session["user"]['user_id']
order_placed_succesfully = queries.checkout(buyer_id, cart)
if order_placed_succesfully:
session.pop("cart", None)
return redirect(url_for('success'))
else:
flash("Something went wrong, could not place your order", 'error')
return redirect(url_for('view_cart'))
else: # request.method == "GET"
remove_cart_item = request.args.get(
'remove_from_cart', '') # stores a product_id
update_item_qty = request.args.get(
'change_quantity', '') # stores a product_id
quantity = int(request.args.get('quantity', 0))
if remove_cart_item:
user_cart = cart.remove_product(remove_cart_item)
session['cart'] = user_cart
session.modified = True
return redirect(url_for('view_cart'))
if update_item_qty:
user_cart = cart.add_product(update_item_qty, quantity)
session['cart'] = user_cart
session.modified = True
return redirect(url_for('view_cart'))
return render_template('cart.html', cart=cart)
else: # user must be logged in
flash("you must be logged in to view your cart", 'error')
return redirect(url_for('login'))
@app.route('/checkout/success/')
def success():
return render_template('success.html')
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
if __name__ == "__main__":
app.run()