-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
464 lines (416 loc) · 16.4 KB
/
main.py
File metadata and controls
464 lines (416 loc) · 16.4 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#required libraries/files/extensions imported
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, flash
import hashlib
from flask_mysqldb import MySQL, MySQLdb
#Some Global Data used by several functions
products_dict = {1: 'Book Store Bag', 2:'Medical Store Bag', 3:'Pamphlets'}
purchase_dict = {0: 'ADVERTISE AND BUY', 1:'ADVERTISE ' , 2:'BUY'}
#Flask object
app = Flask(__name__)
app.secret_key = 'nfhgtErY#$09&*!nnYYZZ8955'
##Database connection
app.config['MYSQL_HOST'] = '127.0.0.1'
app.config['MYSQL_USER'] = 'root'
#app.config['MYSQL_PASSWORD'] = 'admin'
app.config['MYSQL_DB'] = 'green_strips'
mysql = MySQL(app)
#Home Page / Index Page of the website
@app.route('/')
def index():
loggedIn, name = getLoginDetails()
return render_template('index.html', user_data=name)
#Fetch user details if logged in
@app.route('/loggedin', methods=['GET', 'POST'])
def check_login():
loggedIn, name = getLoginDetails()
return jsonify({'status': loggedIn, 'name': name})
#Returns the name of the user if logged in
def getLoginDetails():
if 'email' not in session:
loggedIn = False
name = 'GUEST'
else:
loggedIn = True
name = session['name']
return loggedIn, name
#Returns the User Registration/ Sign-Up
@app.route('/signup', methods=['GET', 'POST'])
def user_signup():
return render_template('advertiseform.html')
#Returns the page containing all information about the logged in user
@app.route('/userDetails', methods=['GET', 'POST'])
def user_details():
loggedIn, name = getLoginDetails()
if not loggedIn:
return redirect(url_for('login_user'))
user_data = get_user_details()
return render_template('user.html', user_data=user_data)
#Returns All detials of the logged in user
def get_user_details():
loggedIn, name = getLoginDetails()
if not loggedIn:
return redirect(url_for('login_user'))
else:
user_id = session['user_id']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(" SELECT * FROM users WHERE id = %s", (user_id,))
user_data = cur.fetchone()
cur.close()
return user_data
#Registers a new user; adds the user in the 'users' table
@app.route('/register', methods=['POST'])
def register_advertiser():
if request.method == 'POST':
# Parse form data
password = request.form['password']
password = hashlib.md5(password.encode()).hexdigest()
email = request.form['email']
firstName = request.form['f_name']
lastName = request.form['l_name']
address = request.form['address']
organization = request.form['org_name']
phone = request.form['phone']
print('###################')
print(password, phone, address)
try:
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(
"INSERT INTO users (f_name, l_name, organization, address, email, phone, password) VALUES (%s, %s, %s, %s, %s, %s, %s)",
(firstName, lastName, organization, address, email, phone, password,))
mysql.connection.commit()
# On sucessful registration, log the user in
cur.execute(" SELECT * FROM users WHERE email = %s", (email,))
user = cur.fetchone()
session['email'] = user['email']
session['name'] = user['f_name']
session['user_id'] = user['id']
flash('You were successfully registered')
except Exception as exc:
mysql.connection.rollback()
msg = "Error occured"
flash(str(exc))
flash("One or Many Details Already Exists in the System. Contact Admin to retrive the account or Create a new one if required")
return redirect(url_for('user_signup'))
cur.close()
return redirect(url_for('advertise'))
#Checks the entered details and allows legitimate users to log in
@app.route('/login', methods=['POST', 'GET'])
def login_user():
if request.method == 'POST':
password = request.form['password']
password = hashlib.md5(password.encode()).hexdigest()
email = request.form['email']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(" SELECT * FROM users WHERE email = %s", (email,))
user = cur.fetchone()
cur.close()
if user is None:
flash("Email not registered")
return redirect(url_for('login_user'))
if password == user['password']:
session['email'] = user['email']
session['name'] = user['f_name']
session['user_id'] = user['id']
flash('You were successfully logged in')
return redirect(url_for('index'))
else:
flash("Incorrect Password")
return redirect(url_for('login_user'))
else:
return render_template('login.html')
#Allows the signed in User to Sign Out
@app.route('/logout', methods=['POST'])
def logout_user():
session.clear()
msg = "User Logged Out!"
return jsonify({'status': True, 'message': msg})
@app.route('/editUser', methods=['POST'])
def update_user_details():
if request.method == 'POST':
# Parse form data
#password = request.form['password']
#password = hashlib.md5(password.encode()).hexdigest()
email = request.form['email']
firstName = request.form['f_name']
lastName = request.form['l_name']
address = request.form['address']
organization = request.form['org_name']
phone = request.form['phone']
try:
user_id = session['user_id']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(
"UPDATE users SET f_name=%s, l_name=%s, organization=%s, address=%s, email=%s, phone=%s where id=%s",
(firstName, lastName, organization, address, email, phone, user_id))
mysql.connection.commit()
#flash('Your details were updated')
except Exception as exc:
mysql.connection.rollback()
msg = "Error occured"
flash(str(exc))
return redirect(url_for('user_details'))
'''
#Lists all available products
@app.route('/products')
def products():
loggedIn, name = getLoginDetails()
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(" SELECT * FROM products ")
prods = cur.fetchall()
cur.close()
return render_template('buy.html', prods=prods, user_data=name)
'''
#Directs to the products page; all purchase to be done from this page; user must to logged in to see this page
@app.route('/advertise')
def advertise():
loggedIn, name = getLoginDetails()
if not loggedIn:
return redirect(url_for('login_user'))
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(" SELECT * FROM products ")
prods = cur.fetchall()
cur.close()
return render_template('advertise.html', prods=prods, user_data=name)
#Adds user selected items in the cart table of the database
def addToCart(prod_id):
loggedIn = getLoginDetails()[0]
if not loggedIn:
return redirect(url_for('login_user'))
user_id = session['user_id']
qty = request.form['qty']
purchase_type = request.form['side-book']
if purchase_type == 'ssa':
purchase_type = 0
elif purchase_type == 'bsa':
purchase_type = 1
elif purchase_type == 'b':
purchase_type = 2
try:
cur = mysql.connection.cursor()
cur.execute("INSERT INTO cart (user_id, product_id, qty, purchase_type) VALUES (%s, %s, %s, %s)",
(user_id, prod_id, qty, purchase_type,))
mysql.connection.commit()
msg = "Added to Cart Successfully"
flash(msg)
except:
mysql.connection.rollback()
msg = "Error occured"
flash(msg)
return redirect(url_for('advertise'))
# return {'status': False, 'message': msg}
cur.close()
return redirect(url_for('advertise'))
#Allows users to add items to their carts
@app.route('/addToCartAdvertise/<int:prod_id>', methods=['POST'])
def addToCartAdvertise(prod_id):
addToCart(prod_id)
return redirect(url_for('advertise'))
'''
@app.route('/checkCart', methods=['POST'])
def check_cart():
user_id = session['user_id']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(" SELECT 1 FROM cart WHERE user_id = %s", (user_id,))
cart_prods = cur.fetchall()
cur.close()
if len(cart_prods) > 0:
msg = "Products exist in cart"
return jsonify({'status': True, 'message': msg})
else:
msg = "No products in your cart"
return jsonify({'status': False, 'message': msg})
'''
#Displays the cart of the logged in user
@app.route('/cart')
def cart():
loggedIn, name = getLoginDetails()
if not loggedIn:
return redirect(url_for('login_user'))
user_id = session['user_id']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(
" SELECT * FROM cart JOIN products ON cart.product_id=products.p_id WHERE user_id = %s;",
(user_id,))
cart_prods = cur.fetchall()
cur.close()
for product in cart_prods:
if product['purchase_type'] == 0:
product['purchase_type'] = "Advertise and Buy"
price = product['price_ad'] + product['price_buy']
elif product['purchase_type'] == 1:
product['purchase_type'] = "Advertise"
price = product['price_ad']
else:
product['purchase_type'] = "Buy"
price = product['price_buy']
product['total_price'] = price * product['qty']
return render_template('cart.html', cart_prods=cart_prods, user_data=name)
#Removes selected items from the cart of the user
@app.route('/cart_item/remove/<int:cart_item_id>', methods=['POST'])
def removerCartItem(cart_item_id):
try:
cur = mysql.connection.cursor()
cur.execute("DELETE FROM cart WHERE cart_item_id = %s", (cart_item_id,))
mysql.connection.commit()
msg = "Removed from the Cart Successfully"
print(msg)
except:
mysql.connection.rollback()
msg = "Error occured"
print(msg)
return msg
cur.close()
return redirect(url_for('cart'))
#Places items in the cart to the order page
@app.route('/checkout', methods=['POST'])
def checkout():
cur = mysql.connection.cursor()
cur.execute(" SELECT * FROM order_details ")
orders = cur.fetchall()
if len(orders) == 0:
order_id = 1
else:
cur.execute(" SELECT o_id FROM order_details ORDER BY o_id DESC LIMIT 1")
order_id = cur.fetchone()[0] + 1
user_id = session['user_id']
cur.execute(
" SELECT user_id, product_id, SUM(qty), purchase_type FROM cart WHERE user_id = %s GROUP BY product_id, purchase_type ",
(user_id,))
cart_details = cur.fetchall()
for items in cart_details:
print('##########')
print(items[1])
print('##########')
cur.execute(" SELECT * FROM products WHERE p_id = %s;", (items[1],))
product = cur.fetchone()
print(product)
if items[3] == 0:
price = product[-1] + product[-2]
elif items[3] == 1:
price = product[-2]
else:
price = product[-1]
total_price = price * items[2]
try:
cur.execute(" INSERT INTO order_details (o_id, p_id, u_id, qty, purchase_type, total_amount) VALUES (%s, %s, %s, %s, %s, %s)",
(order_id, items[1], user_id, items[2], items[3], total_price))
mysql.connection.commit()
cur.execute(" DELETE FROM cart where user_id = %s", (user_id,))
mysql.connection.commit()
except:
mysql.connection.rollback()
msg = "Error occured"
print(msg)
return msg
cur.close()
return redirect(url_for('order'))
#Shows the order history of the user
'''
@app.route('/order')
def order():
loggedIn, name = getLoginDetails()
if not loggedIn:
return redirect(url_for('login_user'))
user_id = session['user_id']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(" SELECT * FROM order_details WHERE u_id = %s", (user_id,))
order_prods = cur.fetchall()
cur.close()
return render_template('order.html', order_prods=order_prods, user_data=name,
products = products_dict, purchase = purchase_dict)
'''
@app.route('/order')
def order():
loggedIn, name = getLoginDetails()
if not loggedIn:
return redirect(url_for('login_user'))
user_id = session['user_id']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(
" SELECT * FROM order_details JOIN products ON order_details.p_id=products.p_id WHERE u_id = %s;",
(user_id,))
order_prods = cur.fetchall()
cur.close()
for product in order_prods:
if product['purchase_type'] == 0:
product['purchase_type'] = "Advertise and Buy"
price = product['price_ad'] + product['price_buy']
elif product['purchase_type'] == 1:
product['purchase_type'] = "Advertise"
price = product['price_ad']
else:
product['purchase_type'] = "Buy"
price = product['price_buy']
product['total_price'] = price * product['qty']
return render_template('order.html', order_prods=order_prods, user_data=name)
#Admin-Login Page
@app.route('/admin-login', methods=['POST', 'GET'])
def admin():
if request.method == 'POST':
password = request.form['password']
name = request.form['username']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(" SELECT * FROM admin WHERE admin_name = %s", (name,))
admin_details = cur.fetchone()
cur.close()
if admin_details == None:
return " Wrong Admin!!! Intruder!!! Back Off Now!!!!!!!!!!!!!! "
elif password == admin_details['password']:
session['admin-login'] = True
return redirect(url_for('admin_panel'))
else:
return "Passwords not match"
else:
return render_template('admin-login.html')
#Opens up the admin panel
@app.route('/site/maintenance/admin-panel')
def admin_panel():
try:
if session['admin-login'] == True:
cur = mysql.connection.cursor()
cur.execute(" SELECT * FROM order_details ORDER BY o_id DESC ")
order_details = cur.fetchall()
cur.close()
order_details_dict = {}
for items in order_details:
order_id = items[0]
cur = mysql.connection.cursor()
cur.execute(" SELECT f_name, l_name, address, email, phone FROM users WHERE id = %s", (items[2],))
user_data = cur.fetchone()
cur.close()
if order_id in order_details_dict.keys():
order_details_dict[order_id]['details'] += [[products_dict[items[1]], items[3], purchase_dict[items[5]], items[4] ]]
else:
order_details_dict[order_id] = {'user_details': user_data}
order_details_dict[order_id]['details'] = [[products_dict[items[1]], items[3], purchase_dict[items[5]], items[4] ]]
order_details_dict[order_id]['status'] = items[6]
print(order_details_dict)
return render_template('admin-panel.html', order_details=order_details_dict, keys=order_details_dict.keys())
else:
return redirect(url_for('admin_panel'))
except:
return render_template('admin-login.html')
#Changes the status of a particular order to delivered
@app.route('/site/maintenance/delivery/status/update/<int:order_id>', methods=['POST'])
def delivery_status_update(order_id):
try:
cur = mysql.connection.cursor()
cur.execute(" UPDATE order_details SET deliveryandpayment = 'DONE' WHERE o_id = %s", (order_id,))
mysql.connection.commit()
msg = "Successfully Completed"
print(msg)
except:
mysql.connection.rollback()
msg = "Error occured"
print(msg)
return msg
cur.close()
return redirect(url_for('admin_panel'))
#log outs the admin
@app.route('/admin_logout', methods=['POST'])
def admin_logout():
session.clear()
msg = "User Logged Out!"
return redirect(url_for('admin'))
if __name__ == '__main__':
app.run(debug=True)