-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
201 lines (138 loc) · 4.8 KB
/
Copy pathapp.py
File metadata and controls
201 lines (138 loc) · 4.8 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
from flask import *
from flask_cors import CORS
import random
import sqlite3
app = Flask(__name__)
app.secret_key = "nobodyisguessingthisonelmao"
CORS(app)
db = sqlite3.connect("file:database.db?mode=ro", uri=True, check_same_thread=False)
db.row_factory = sqlite3.Row
auth = sqlite3.connect("file:auth.db?mode=ro", uri=True, check_same_thread=False)
auth.row_factory = sqlite3.Row
def template(name, **args):
return render_template(
name,
**args,
cart=session.get("cart", []),
weather=random.randint(50, 90),
fmt_money = lambda cents: f"{'-' if cents < 0 else ''}${abs(cents) / 100:.2f}",
)
@app.route("/")
def index():
return template("index.html")
@app.route("/search")
def search():
query = request.args.get("query")
cur = db.execute(f"SELECT * FROM books WHERE title LIKE '%{query}%' AND hidden = 0")
books = cur.fetchall()
books = list(enumerate(books))
return template("search.html", results=books, query=query)
@app.route("/book", methods=["POST"])
def book():
cart = session.get("cart", [])
req = int(request.form["book"])
if req in cart:
cart.remove(req)
else:
cart.append(req)
session["cart"] = cart
if request.form.get("from") == "cart":
return redirect("/cart")
return redirect("/search?query=" + request.form["query"])
def calculate_cart():
cart = session.get("cart", [])
cur = db.execute(
"SELECT * FROM books WHERE id IN ({})".format(", ".join("?" * len(cart))),
cart,
)
books = cur.fetchall()
prices = {}
prices["books"] = sum(book["price"] for book in books)
prices["handling"] = 1000
prices["shipping"] = 1500
prices["total"] = sum(prices.values())
coupon = session.get("coupon")
if coupon:
prices["discount"] = -int(prices["total"] * coupon[1] / 100)
prices["total"] += prices["discount"]
return books, prices
@app.route("/cart")
def cart():
books, prices = calculate_cart()
coupon = session.get("coupon")
return template("cart.html", results=books, prices=prices, coupon=coupon)
@app.route("/coupon", methods=["POST"])
def coupon():
coupon = request.form["coupon"]
cur = db.execute(f"SELECT * FROM coupons WHERE code='{coupon}'")
coupon = cur.fetchone()
if coupon:
session["coupon"] = (coupon["code"], coupon["discount"])
else:
flash("The coupon code entered is not valid.", "error")
return redirect("/cart")
@app.route("/order/justify")
def justify():
books, prices = calculate_cart()
if len(books) == 0:
return redirect("/cart")
if prices["total"] > 0:
return redirect("/cart")
return template("justify.html", results=books, prices=prices)
@app.route("/order/confirm", methods=["GET", "POST"])
def confirm():
cart = session.get("cart", [])
if set(session.get("confirmed", [])) == set(cart):
return template("confirm.html")
error = 0
for book_id in cart:
justification = request.form.get(str(book_id), "")
if justification == "":
error = "You must provide a justification for all entries in your order."
break
cur = db.execute("SELECT * FROM books WHERE id = (?)", str(book_id))
book = cur.fetchone()
if book["justification"] != justification:
error += 1
if error == 0:
session["confirmed"] = list(cart)
return redirect("/order/confirm")
if isinstance(error, int):
flash(f"You have provided incorrect justification for {error} entr{'y' if error == 1 else 'ies'} in your order.")
else:
flash(error)
return redirect("/order/justify")
@app.route("/login", methods=["GET", "POST"])
def admin():
cart = session.get("cart", [])
if set(session.get("confirmed", [])) != set(cart):
return redirect("/")
if 0 not in cart:
return redirect("/")
if request.method == "GET":
return template("login.html")
username = request.form["username"]
password = request.form["password"]
cur = auth.execute(f"SELECT * FROM users WHERE username = '{username}'")
res = cur.fetchone()
if res is None:
flash("User not found")
return redirect("/login")
cur2 = auth.execute("SELECT * FROM users WHERE username = 'admin'")
res2 = cur2.fetchone()
if username != res2["username"] or password != res2["password"]:
flash("Invalid username or password")
return redirect("/login")
session["youdidit"] = True
return redirect("/yay")
@app.route("/yay")
def yay():
if not session.get("youdidit", False):
return redirect("/")
return template("yay.html")
@app.route("/reset")
def reset():
session.clear()
return redirect("/")
if __name__ == "__main__":
app.run("0.0.0.0", threaded=False, debug=False)