-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
382 lines (265 loc) · 7.01 KB
/
Copy pathapp.py
File metadata and controls
382 lines (265 loc) · 7.01 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from flask import Flask, render_template, request, redirect, session
import sqlite3
app = Flask(__name__)
app.secret_key = "shopease_secret_key_2026"
DATABASE = "users.db"
# =============================
# PRODUCT PRICES
# =============================
PRICES = {
"Smartphone": 29999,
"Laptop": 59999,
"Headphones": 2999
}
# =============================
# DATABASE SETUP
# =============================
def init_db():
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS orders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL,
product TEXT NOT NULL,
total REAL NOT NULL,
status TEXT NOT NULL,
payment TEXT NOT NULL
)
""")
conn.commit()
conn.close()
init_db()
# =============================
# HOME
# =============================
@app.route("/")
def home():
return render_template("index.html")
# =============================
# PRODUCTS
# =============================
@app.route("/products")
def products():
return render_template("products.html")
# =============================
# LOGIN
# =============================
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
email = request.form.get("email")
password = request.form.get("password")
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute(
"""
SELECT *
FROM users
WHERE email = ?
AND password = ?
""",
(email, password)
)
user = cursor.fetchone()
conn.close()
if user:
session["user"] = user[1]
session["email"] = user[2]
return redirect("/")
return "Invalid email or password"
return render_template("login.html")
# =============================
# REGISTER
# =============================
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
name = request.form.get("name")
email = request.form.get("email")
password = request.form.get("password")
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
try:
cursor.execute(
"""
INSERT INTO users (name, email, password)
VALUES (?, ?, ?)
""",
(name, email, password)
)
conn.commit()
except sqlite3.IntegrityError:
conn.close()
return "Email already registered."
conn.close()
return redirect("/login")
return render_template("register.html")
# =============================
# LOGOUT
# =============================
@app.route("/logout")
def logout():
session.clear()
return redirect("/")
# =============================
# ADD TO CART
# =============================
@app.route("/add-to-cart/<product_name>")
def add_to_cart(product_name):
cart_items = session.get("cart", [])
cart_items.append(product_name)
session["cart"] = cart_items
return redirect("/cart")
# =============================
# CART
# =============================
@app.route("/cart")
def cart():
cart_items = session.get("cart", [])
return render_template(
"cart.html",
cart_items=cart_items,
prices=PRICES
)
# =============================
# REMOVE FROM CART
# =============================
@app.route("/remove-from-cart/<product_name>")
def remove_from_cart(product_name):
cart_items = session.get("cart", [])
if product_name in cart_items:
cart_items.remove(product_name)
session["cart"] = cart_items
return redirect("/cart")
# =============================
# CHECKOUT
# =============================
@app.route("/checkout")
def checkout():
cart_items = session.get("cart", [])
if not cart_items:
return redirect("/cart")
total = 0
for item in cart_items:
item_name = str(item).strip()
total += PRICES.get(item_name, 0)
return render_template(
"checkout.html",
cart_items=cart_items,
prices=PRICES,
total=total
)
# =============================
# PLACE ORDER
# =============================
@app.route("/place-order", methods=["POST"])
def place_order():
cart_items = session.get("cart", [])
if not cart_items:
return redirect("/cart")
email = session.get(
"email",
"guest@example.com"
)
payment = request.form.get(
"payment",
"Cash on Delivery"
)
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
# Create one order for every product
for product in cart_items:
product_name = str(product).strip()
product_price = PRICES.get(
product_name,
0
)
cursor.execute(
"""
INSERT INTO orders
(email, product, total, status, payment)
VALUES (?, ?, ?, ?, ?)
""",
(
email,
product_name,
product_price,
"Order Placed",
payment
)
)
conn.commit()
conn.close()
# Clear cart after successful order
session["cart"] = []
return render_template(
"order_success.html"
)
# =============================
# MY ORDERS
# =============================
@app.route("/orders")
def orders():
email = session.get("email")
if not email:
return redirect("/login")
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute(
"""
SELECT *
FROM orders
WHERE email = ?
ORDER BY id DESC
""",
(email,)
)
orders_list = cursor.fetchall()
conn.close()
return render_template(
"orders.html",
orders=orders_list
)
# =============================
# CANCEL ORDER
# =============================
@app.route("/cancel-order/<int:order_id>")
def cancel_order(order_id):
email = session.get("email")
if not email:
return redirect("/login")
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute(
"""
UPDATE orders
SET status = ?
WHERE id = ?
AND email = ?
AND status = ?
""",
(
"Cancelled",
order_id,
email,
"Order Placed"
)
)
conn.commit()
conn.close()
return redirect("/orders")
# =============================
# RUN APPLICATION
# =============================
if __name__ == "__main__":
app.run(debug=True)