-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
349 lines (321 loc) · 8.22 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
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
from flask import Flask, render_template, request, redirect, url_for, abort, Response, jsonify
app = Flask(__name__)
app.config['SECRET_KEY'] = 'qpowiecmnIhpjasdIaY'
def MergeDict(dict1, dict2):
return dict(list(dict1.items()) + list(dict2.items()))
@app.route('/')
def home():
title = "Home"
return redirect(url_for('product'))
def notpresent(var):
for i in cart:
if i["id"] == var["id"]:
return False
else:
return True
def total():
total = 0
for i in cart:
total += i["subtotal"]
return total
@app.route('/product', methods=['POST', 'GET'])
def product():
title = "Product"
if request.method == 'POST':
d = request.form.get('pid')
q = int(request.form.get('quantity'))
# print(q,'this is q')
id = int(d)
id -= 1
p = products[id]
price = int(p["price"])
# print(p, "this is product selected")
subtotal = 0
for i in range (q):
subtotal += price
# print(subtotal, 'this is price of selected product')
if notpresent(p):
p["quantity"] = q
p["subtotal"] = subtotal
cart.append(p)
# print(cart, 'This is product funciton')
length = len(cart)
return render_template('product.html', title=title, products=products, l=length)
@app.route('/productdetails/<int:id>', methods=['GET', 'POST'])
def productdetails(id):
title = "Product Details"
length = len(cart)
return render_template('productdetails.html', title=title, l=length, products=products, id=id)
@app.route('/cart')
def cart():
title = "Cart"
if len(cart)<=0:
return redirect(url_for('home'))
length = len(cart)
return render_template('cart.html', title=title, cart=cart, l=length, total=total())
@app.route('/delete/<int:id>')
def delete(id):
for i in cart:
if i["id"] == id:
cart.remove(i)
return redirect(url_for('cart'))
@app.route('/clearcart')
def clearcart():
cart.clear()
return redirect(url_for('home'))
@app.route('/checkout')
def checkout():
title = "Checkout"
return render_template('checkout.html', title=title, t=total())
@app.route('/success')
def success():
title = "Success"
return render_template('success.html', title=title, purchase=total())
@app.route('/shop_api/<int:id>')
def shopapi(id):
id = id - 1
p = products[id]
return p
@app.route('/shop_api/')
def shopallapi():
p = products
return jsonify(p)
@app.route('/check-cart', methods=['POST'])
def checkcart():
if request.get_json:
dataGet = request.get_json(force=True)
# print(dataGet, "this is datag et")
if dataGet == {"product":"product"}:
dataReply = 200
else:
dataReply = 404
else:
dataReply = 404
return jsonify(dataReply)
@app.route('/check-checkout', methods=['POST'])
def checkcheckout():
if request.get_json:
dataGet = request.get_json(force=True)
if dataGet == {"checkout":"checkout"}:
dataReply = 200
else:
dataReply = 404
else:
dataReply = 404
return jsonify(dataReply)
@app.route('/check-payment', methods=['POST'])
def checkpayment():
if request.get_json:
dataGet = request.get_json(force=True)
if dataGet == {"card":"card"}:
dataReply = 200
else:
dataReply = 404
else:
dataReply = 404
return jsonify(dataReply)
@app.route('/error')
def error():
abort (404)
@app.errorhandler(417)
def page_not_found(e):
return render_template('code_417.html'), 417
@app.errorhandler(400)
def page_not_found(e):
return render_template('code_400.html'), 400
@app.errorhandler(403)
def page_not_found(e):
return render_template('code_403.html'), 403
@app.errorhandler(404)
def page_not_found(e):
return render_template('code_404.html'), 404
@app.errorhandler(405)
def page_not_found(e):
return render_template('code_405.html'), 405
@app.errorhandler(500)
def page_not_found(e):
return render_template('code_500.html'), 500
cart = []
products = [
{
"id":1,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/oneplus/oneplus-8-1.jpg",
"name": "OnePlus 8",
"price": 500,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":2,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/alcatel/tcl-10-pro-1.jpg",
"name": "TCL 10 Pro",
"price": 399,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":3,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/samsung/samsung-galaxy-s10-1.jpg",
"name": "Samsung Galaxy S10",
"price": 254,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":4,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/google/google-pixel-6-pro-r1.jpg",
"name": "Google Pixel 6 Pr",
"price": 299 ,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":5,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/apple/apple-iphone-12-pro-max-1.jpg",
"name": "Apple iPhone 12 Pro Max ",
"price": 999,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":6,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/nokia/nokia-x20-1.jpg",
"name": "Nokia X20",
"price": 199,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":7,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/xiaomi/xiaomi-redmi-note10-pro-1.jpg",
"name": "Xiaomi Redmi Note 10 Pro",
"price": 354,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":8,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/sony/sony-xperia-1-iii-02.jpg",
"name": "Sony Xperia 1 III",
"price": 365,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":9,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/lg/lg-velvet-1.jpg",
"name": "LG Velvet 5G",
"price": 329.99,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":10,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/asus/asus-rog-phone-5-0.jpg",
"name": "Asus ROG Phone 5",
"price":829 ,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":11,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/htc/htc-desire-21-pro-5g-1.jpg",
"name": "HTC Desire 21 Pro 5G",
"price": 226,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":12,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/huawei/huawei-mate40-pro-1.jpg",
"name": "Huawei Mate 40 Pro 4G",
"price":271 ,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":13,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/oppo/oppo-reno6-pro-plus-1.jpg",
"name": "Oppo Reno6 Pro+ 5G",
"price": 499,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":14,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/vivo/vivo-iqoo-7-india-2.jpg",
"name": "vivo iQOO 7 (India)",
"price": 99.09,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":15,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/vivo/vivo-x60-pro-plus-1.jpg",
"name": "vivo X60t Pro+",
"price": 200,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":16,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/motorola/motorola-moto-g60-1.jpg",
"name": "Motorola Moto G60",
"price": 279,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":17,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/realme/realme-gt-5g-1.jpg",
"name": "Realme GT 5G",
"price": 549,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":18,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/tecno/tecno-phantom-x-1.jpg",
"name": "Tecno Phantom X",
"price":299 ,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":19,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/microsoft/microsoft-surface-duo-01.jpg",
"name": "Microsoft Surface Duo",
"price": 238,
"quantity": 1,
"stock": 5,
"subtotal": 0
},
{
"id":20,
"imgurl": "https://fdn2.gsmarena.com/vv/pics/lava/lava-z2-max-1.jpg",
"name": "Lava Z2 Max",
"price": 300,
"quantity": 1,
"stock": 5,
"subtotal": 0
}
]
app.run(debug=True)