-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
313 lines (209 loc) · 9.6 KB
/
app.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
#from asyncio.windows_events import NULL
from ast import excepthandler
import sqlite3
from turtle import clear
from urllib import response
from flask import Flask, render_template, redirect, request, session
from functools import wraps
from flask_session import Session
from werkzeug.security import check_password_hash, generate_password_hash
from sqlite3 import connect
import json
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# to disable caching
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
def index():
# checking if user session exists
try:
session["user"]
except KeyError:
session["user"] = 0
return render_template('index.html', sesh=session["user"])
@app.route("/menu", methods=['GET','POST'])
def menu():
if request.method == 'GET':
with connect('restaurant.db') as conn:
db = conn.cursor()
# retrieve menu items
res = db.execute('SELECT * FROM menu').fetchall()
# print(res)
# to compose menu json from db
menu = {}
# cycling through categories
for i in range(1, 14):
catg = db.execute('SELECT type FROM category WHERE id=?', [i]).fetchall()[0][0]
items = {}
for j in res:
if i == j[1]:
# print(j)
# item id at j[0]
# category at j[1]
item = {}
result = db.execute('SELECT * FROM items WHERE id = ?', [j[0]]).fetchall()[0]
name = result[1]
item["price"] = result[2]
item["desc"] = result[3]
item["id"] = result[0]
# appending each item to items dict
items.update({name: item})
#print(item["id"])
# adding category and its items to the menu
# with each cycle of nested loop
menu[catg] = items
#print(menu)
print('\n----------------------')
""" with open('menu_cleaned.json', 'r') as items:
menu = json.load(items)
print(menu) """
# adding selected items by the user to a list
# to show in the cart
if not (request.args.get('id') is None):
id = request.args.get('id')
# checking if cart has items
try:
session["cart"]
if id in session["cart"]:
session["cart"][id] += 1
else:
session["cart"].update({id:1})
print(session["cart"])
# if not, starting count
except KeyError:
session["cart"].update({id:1})
return render_template('menu.html', sesh=session["user"], menu=menu)
@app.route("/gift")
def gift():
return render_template('giftcards.html', sesh=session["user"])
@app.route("/signin", methods=["GET", "POST"])
def signin():
if request.method == "GET":
return render_template('signin.html', sesh=session["user"])
if request.method == "POST":
with connect('restaurant.db') as conn:
db = conn.cursor()
username = request.form.get("username")
password = request.form.get("password")
rows = db.execute("SELECT * FROM users WHERE email=?", [username]).fetchall()
# matching entered credentials with database records
# print(rows)
check = rows[0][2]
if check_password_hash(check, password):
# setting session id
session["user"] = rows[0][0]
# setting cart empty
session["cart"] = {}
print('login successful for ', session["user"])
return redirect('/user')
else:
msg = "Login failed. Incorrect username or password provided."
return render_template('error.html', msg=msg)
@app.route("/signup", methods=["POST"])
def signup():
if request.method == "POST":
# retrieve info from form
fname = request.form.get('fname')
lname = request.form.get('lname')
email = request.form.get('email')
# opening connection to database and committing the newly created account
with connect('restaurant.db') as conn:
db = conn.cursor()
result = db.execute('SELECT email FROM users WHERE email = ?', [email]).fetchall()
print(result, len(result))
# phone = request.form.get('phone')
pwd = request.form.get('pwd')
c_pwd = request.form.get('c_pwd')
if pwd == c_pwd:
try:
db.execute('INSERT INTO users (email, hash, name) VALUES (?, ?, ?)', (email, generate_password_hash(pwd), fname + ' ' + lname))
except:
return render_template('error.html', msg="Username already in use.", sesh=session["user"])
else:
return render_template('error.html', msg="Passwords didn't match.", sesh=session["user"])
return redirect('/signin')
@app.route("/user")
def useracc():
with connect('restaurant.db') as conn:
db = conn.cursor()
rows = db.execute("SELECT name, email FROM users WHERE id=?", [session["user"]]).fetchall()
# first name
name = rows[0][0].split(' ')[0]
# username
email = rows[0][1]
results = db.execute("SELECT selectedItems, subtotal, tax, total, receivedAt FROM orders WHERE userID=?", [session["user"]]).fetchall()
# print(results)
if not results is None:
orders = {}
orderInfo = {}
names = {}
order_count = 1
for i in results:
details = json.loads(i[0])
subtotal = i[1]
tax = i[2]
total = i[3]
datetime = i[4]
print(details)
orders.update({order_count : { "subtotal" : subtotal, "tax": tax, "total": total, "datetime": datetime}})
# retrieving item names and storing into details dict
for id in details:
names[id] = db.execute("SELECT item FROM items WHERE id=?", [id]).fetchall()[0][0]
# updating orderInfo dict
orderInfo.update({order_count: details})
order_count += 1
print(orders)
print(orderInfo)
return render_template('useracc.html', name=name, username=email, sesh=session["user"], orders=orders, details=orderInfo, items=names)
@app.route("/cart", methods=['GET','POST'])
def cart():
if len(session["cart"]) != 0:
with connect('restaurant.db') as conn:
db = conn.cursor()
# storing prices for every selected item
prices = {}
subtotal = 0
for id in session["cart"]:
prices.update({id:db.execute('SELECT price, item FROM items WHERE id=?', [id]).fetchall()[0]})
subtotal += prices[id][0] * session["cart"][id]
tax = round(0.07 * subtotal, 2)
subtotal = round(subtotal, 2)
# print(prices)
# confirming the order
if request.method == "POST":
place_order(tax, subtotal)
return render_template('confirm.html')
else:
prices = {}
subtotal = 0
tax = 0
return render_template('cart.html', cart=session["cart"], prices=prices, subtotal=subtotal, tax=tax)
@app.route("/inquire", methods=['POST', 'GET'])
def contactus():
if request.method == "POST":
email = request.form.get('email')
inquiry = request.form.get('question')
return render_template('inquire.html')
@app.route("/signout")
def signout():
session.clear()
return redirect('/')
@app.route("/error")
def error(msg="What did you do!?"):
return render_template('error.html', msg=msg)
def place_order(tax, subtotal):
with connect('restaurant.db') as conn:
db = conn.cursor()
db.execute('INSERT INTO orders (userID, selectedItems, subtotal, tax, total) VALUES (?, ?, ?, ?, ?)', (session["user"], json.dumps(session["cart"]), subtotal, tax, round(subtotal+tax, 2)))
# clear cart since order is placed
session["cart"].clear()
return
app.run()