-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.py
355 lines (331 loc) · 15.4 KB
/
routes.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
from flask import flash, redirect, render_template, request, session, url_for
from app import app
import addresses
import errors
import orders
import products
import users
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "GET":
if users.is_logged_in():
user_id = users.get_user_id_by_username()
return render_template(
"index.html", user_id=user_id, employee=users.is_employee(),
number_of_items=orders.get_total_number_of_items_in_cart(user_id))
return render_template("login.html")
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
if not users.login(username, password):
return render_template("login.html")
user_id = users.get_user_id_by_username()
return render_template(
"index.html", user_id=user_id, employee=users.is_employee(),
number_of_items=orders.get_total_number_of_items_in_cart(user_id))
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "GET":
if not users.is_logged_in():
return render_template("register.html")
return redirect("/")
if request.method == "POST":
username = request.form["username"]
password1 = request.form["password1"]
password2 = request.form["password2"]
if not users.register(username, password1, password2):
return render_template("register.html")
return redirect("/")
@app.route("/logout")
def logout():
users.logout()
return redirect("/")
@app.route("/customers")
def customers():
if not users.is_employee():
return errors.authentication_error()
customers = users.get_all_customers()
return render_template("customers.html", employee=True, customers=customers)
@app.route("/make-admin/<int:id>")
def make_admin(id):
if not users.is_employee():
return errors.authentication_error()
users.make_admin(id)
flash(f"Työntekijän oikeudet lisätty käyttäjälle {users.get_username_by_user_id(id)}!")
return redirect(url_for("customers"))
@app.route("/new-product", methods=["GET", "POST"])
def new_product():
if request.method == "GET":
if users.is_employee():
return render_template("new_product.html", employee=True)
return errors.authentication_error()
if request.method == "POST":
if session["csrf_token"] != request.form["csrf_token"]:
abort(403)
if not users.is_employee():
return errors.authentication_error()
name = request.form["name"]
description = request.form["description"]
quantity = request.form["quantity"]
price = request.form["price"]
product_id = products.add_product(name, quantity, price, description)
if not product_id:
return render_template(
"new_product.html", employee=True, name=name, description=description,
quantity=quantity, price=price)
return redirect(url_for("product", id=product_id))
@app.route("/all-products", methods=["GET", "POST"])
def all_products():
if not users.is_logged_in():
return errors.authentication_error()
order = request.form["order"] if request.method == "POST" else "alpha-asc"
if request.method == "POST" and session["csrf_token"] != request.form["csrf_token"]:
abort(403)
user_id = users.get_user_id_by_username()
number_of_items = orders.get_total_number_of_items_in_cart(user_id)
return render_template(
"all_products.html", products=products.all_products(order), user_id=user_id,
employee=users.is_employee(), order=order, number_of_items=number_of_items)
@app.route("/product/<int:id>")
def product(id):
if not users.is_logged_in():
return errors.authentication_error()
user_id = users.get_user_id_by_username()
return render_template(
"product.html", id=id, user_id=user_id, product=products.get_product_info(id),
price_history=products.get_price_history(id), employee=users.is_employee(),
number_of_items=orders.get_total_number_of_items_in_cart(user_id))
@app.route("/edit-product/<int:id>", methods=["GET", "POST"])
def edit_product(id):
if request.method == "GET":
if not users.is_employee():
return errors.authentication_error()
return render_template(
"edit_product.html", id=id, product=products.get_product_info(id), employee=True)
if request.method == "POST":
if session["csrf_token"] != request.form["csrf_token"]:
abort(403)
if not users.is_employee():
return errors.authentication_error()
name = request.form["name"]
quantity = request.form["quantity"]
price = request.form["price"]
description = request.form["description"]
if products.edit_product(id, name, quantity, price, description):
flash("Tuotteen tiedot päivitetty!", category="success")
return redirect(url_for("product", id=id))
return render_template(
"edit_product.html", id=id, product=products.get_product_info(id), employee=True)
@app.route("/account/<int:id>")
def account(id):
user_id = users.get_user_id_by_username()
if user_id != id and not users.is_employee():
return errors.authentication_error()
if users.is_employee(id) or not users.user_exists(id):
return errors.page_not_found()
username = users.get_username_by_user_id(id)
number_of_items = orders.get_total_number_of_items_in_cart(id)
user_addresses = addresses.get_all_addresses(id)
open_orders = orders.get_open_orders(id)
open_order_ids = orders.get_open_order_ids(id)
open_order_grand_totals, delivered_order_grand_totals = None, None
if open_order_ids:
open_order_grand_totals = []
for order_id in open_order_ids:
open_order_grand_totals.append(orders.get_total_sum(order_id[0]))
delivered_orders = orders.get_delivered_orders(id)
delivered_order_ids = orders.get_delivered_order_ids(id)
if delivered_order_ids:
delivered_order_grand_totals = []
for order_id in delivered_order_ids:
delivered_order_grand_totals.append(orders.get_total_sum(order_id[0]))
return render_template(
"account.html", employee=users.is_employee(), user_id=id, username=username,
addresses=user_addresses, number_of_items=number_of_items, open_orders=open_orders,
open_order_grand_totals=open_order_grand_totals, delivered_orders=delivered_orders,
delivered_order_grand_totals=delivered_order_grand_totals)
@app.route("/new-address/<int:id>", methods=["GET", "POST"])
def new_address(id):
if request.method == "GET":
username = users.get_username_by_user_id(id)
if not username:
return errors.page_not_found()
if id != users.get_user_id_by_username() and not users.is_employee():
return errors.authentication_error()
return render_template(
"new_address.html", user_id=id, employee=users.is_employee(),
number_of_items=orders.get_total_number_of_items_in_cart(id))
if request.method == "POST":
if session["csrf_token"] != request.form["csrf_token"]:
abort(403)
if id != users.get_user_id_by_username() and not users.is_employee():
return errors.authentication_error()
full_name = request.form["full_name"]
street_address = request.form["street_address"]
zip_code = request.form["zip_code"]
city = request.form["city"].upper()
phone = request.form["phone_number"]
email = request.form["email"]
if addresses.new_address(id, full_name, street_address, zip_code, city, phone, email):
flash("Uusi osoite lisätty!", category="success")
return redirect(url_for("account", id=id))
return render_template(
"new_address.html", user_id=id, employee=users.is_employee(),
full_name=full_name, street_address=street_address, zip_code=zip_code,
city=city, phone_number=phone, email=email)
@app.route("/edit-address/<int:id>", methods=["GET", "POST"])
def edit_address(id):
if request.method == "GET":
if not addresses.is_visible(id):
return errors.page_not_found()
if addresses.get_address_owner(id) != users.get_user_id_by_username():
if not users.is_employee():
return errors.authentication_error()
user_id = addresses.get_address_owner(id)
number_of_items = orders.get_total_number_of_items_in_cart(user_id)
return render_template(
"edit_address.html", id=id, user_id=user_id, address=addresses.get_address(id),
employee=users.is_employee(), number_of_items=number_of_items)
if request.method == "POST":
if session["csrf_token"] != request.form["csrf_token"]:
abort(403)
user_id = int(request.form["address_owner"])
if user_id != users.get_user_id_by_username() and not users.is_employee():
return errors.authentication_error()
full_name = request.form["full_name"]
street_address = request.form["street_address"]
zip = request.form["zip_code"]
city = request.form["city"].upper()
phone = request.form["phone_number"]
email = request.form["email"]
number_of_items = orders.get_total_number_of_items_in_cart(user_id)
if not addresses.new_address(user_id, full_name, street_address, zip, city, phone, email):
return render_template(
"edit_address.html", id=id, user_id=user_id, address=addresses.get_address(id),
employee=users.is_employee(), number_of_items=number_of_items)
addresses.delete_address(id)
flash("Osoitetiedot päivitetty!", category="success")
return redirect(url_for("account", id=user_id))
@app.route("/delete-address/<int:id>")
def delete_address(id):
if not addresses.is_visible(id):
return errors.page_not_found()
address_owner = addresses.get_address_owner(id)
if not address_owner or (address_owner != users.get_user_id_by_username()):
if not users.is_employee():
return errors.authentication_error()
addresses.delete_address(id)
return redirect(url_for("account", id=address_owner))
@app.route("/error")
def error():
return render_template("error.html")
@app.route("/cart")
def cart():
if users.is_employee():
return errors.page_not_found()
user_id = users.get_user_id_by_username()
order_id = orders.get_unfinished_order_id(user_id)
u_addresses = addresses.get_formatted_visible_addresses(addresses.get_all_addresses(user_id))
if not order_id:
return render_template(
"cart.html", user_id=user_id, employee=False, number_of_items=0,
user_addresses=u_addresses, default_address=orders.get_address_id(order_id))
products = orders.get_order_items(order_id)
total_sum = orders.get_total_sum(order_id)
number_of_items = orders.get_total_number_of_items_in_cart(user_id)
return render_template(
"cart.html", user_id=user_id, employee=False, products=products, total_sum=total_sum,
number_of_items=number_of_items, user_addresses=u_addresses,
default_address=orders.get_address_id(order_id))
@app.route("/update-quantity", methods=["POST"])
def update_quantity():
product_id = request.form["product_id"]
quantity = request.form["quantity"]
order_id = request.form["order_id"]
if session["csrf_token"] != request.form["csrf_token"]:
abort(403)
if users.is_employee():
return errors.page_not_found()
if orders.get_order_owner(order_id) != users.get_user_id_by_username():
return errors.authentication_error()
orders.update_item_quantity(product_id, quantity, order_id)
return redirect(url_for("cart"))
@app.route("/add-item-to-cart/<int:product_id>")
def add_item_to_cart(product_id):
user_id = users.get_user_id_by_username()
order_id = orders.get_unfinished_order_id(user_id)
if users.is_employee():
return errors.page_not_found()
if not order_id:
order_id = orders.create_new(user_id)
orders.add_item(order_id, product_id)
return redirect(url_for("product", id=product_id))
@app.route("/delete-item/<int:product_id>")
def delete_item(product_id):
user_id = users.get_user_id_by_username()
order_id = orders.get_unfinished_order_id(user_id)
if users.is_employee() or not order_id:
return errors.page_not_found()
orders.update_item_quantity(product_id, "0", order_id)
return redirect(url_for("cart"))
@app.route("/add-address-to-order", methods=["POST"])
def add_address_to_order():
if session["csrf_token"] != request.form["csrf_token"]:
abort(403)
address_id = request.form["address"]
order_id = request.form["order_id"]
address_owner = addresses.get_address_owner(address_id)
order_owner = orders.get_order_owner(order_id)
if address_owner != order_owner and not users.is_employee():
return errors.authentication_error()
orders.set_address_to_order(address_id, order_id)
return redirect(url_for("cart"))
@app.route("/send-order/<int:order_id>")
def send_order(order_id):
user_id = orders.get_order_owner(order_id)
if orders.get_order_status(order_id) != "unfinished":
flash("Tilaus on jo lähetetty!", category="error")
return redirect(url_for("account", id=user_id))
if user_id != users.get_user_id_by_username() and not users.is_employee():
return errors.authentication_error()
if not orders.get_address_id(order_id):
flash("Valitse toimitusosoite!", category="error")
return redirect(url_for("cart"))
orders.send_order(order_id)
flash("Tilaus lähetetty!", category="success")
return redirect(url_for("account", id=user_id))
@app.route("/order/<int:order_id>")
def order(order_id):
user_id = users.get_user_id_by_username()
status = orders.get_order_status(order_id)
if not status or status == "unfinished":
return errors.page_not_found()
if orders.get_order_owner(order_id) != user_id and not users.is_employee():
return errors.authentication_error()
order_info = orders.get_order_information(order_id)
grand_total = orders.get_total_sum(order_id)
address = addresses.get_address(orders.get_address_id(order_id))
sent_at = orders.get_order_time(order_id)
number_of_items = orders.get_total_number_of_items_in_cart(user_id)
return render_template(
"order.html", order_id=order_id, user_id=user_id, employee=users.is_employee(),
number_of_items=number_of_items, order_info=order_info, grand_total=grand_total,
address=address, sent_at=sent_at, status=status)
@app.route("/all-orders")
def all_orders():
if not users.is_employee():
return errors.authentication_error()
open_orders = orders.get_all_open_orders()
delivered_orders = orders.get_all_delivered_orders()
return render_template(
"all_orders.html", employee=True, open_orders=open_orders,
delivered_orders=delivered_orders)
@app.route("/handle-order/<int:order_id>")
def handle_order(order_id):
if not users.is_employee():
return errors.authentication_error()
if not orders.get_order_owner(order_id):
return errors.page_not_found()
orders.handle_order(order_id)
flash("Tilaus käsitelty!", category="success")
return redirect(url_for("order", order_id=order_id))