-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
366 lines (309 loc) · 12.2 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from flask_bcrypt import Bcrypt
from flask import request,session
from flask import flash
# from flask_mysqldb import MySQL
import mysql.connector
from flask import Flask
from flask import redirect, url_for, render_template
import random,datetime
from functools import wraps
from controller.utilities import category_items, cart_value, upass, buyid, connect
from controller.order import orhistory
from controller.cart import add_item, cart_items, delete_item, update_item
from controller.medicines import product_detail
from controller.checkout import normal_checkout, checkout_details
from controller.product import single_product
from controller.search import query_search
from controller.favourites import fav, favourite
from controller.supplier import ssearch, supproduct_detail
from controller.supplier_main import supadd, supupdate
app = Flask(__name__)
app.secret_key = "super secret key"
# remotemysql.com - email- [email protected]
app.config['MYSQL_HOST'] = 'remotemysql.com'
app.config['MYSQL_PORT'] = '3306'
app.config['MYSQL_USER'] = 'v00dpY1wp4'
app.config['MYSQL_PASSWORD'] = 'plWh42dJzQ'
app.config['MYSQL_DB'] = 'v00dpY1wp4'
# mysql = MySQL(app)
bcrypt = Bcrypt(app)
@app.route("/")
@app.route("/home")
def home():
print(dir(mysql))
return render_template("index.html")
# def index():
# return "Hello"
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'email' in session:
return f(*args, **kwargs)
else:
# if(request.endpoint in ['auth', 'registration']):
# return f(*args, **kwargs)
flash("You need to login first!!", category="danger")
return redirect(url_for('login'))
return wrap
def customer_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'email' in session:
if session['type']=='customer' or 'powner':
return f(*args, **kwargs)
else :
flash("You need to login as customer first!!", category="danger")
return redirect(url_for('login'))
else:
# if(request.endpoint in ['auth', 'registration']):
# return f(*args, **kwargs)
flash("You need to login first!!", category="danger")
return redirect(url_for('login'))
return wrap
def supplier_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'email' in session:
if session['type']=='supplier':
return f(*args, **kwargs)
else :
flash("You need to login as supplier first!!", category="danger")
return redirect(url_for('login'))
else:
# if(request.endpoint in ['auth', 'registration']):
# return f(*args, **kwargs)
flash("You need to login first!!", category="danger")
return redirect(url_for('login'))
return wrap
@app.route("/logout")
@login_required
def logout():
session.clear()
flash("You have been successfully logged out!!", category="success")
return redirect(url_for('home'))
@app.route("/login", methods=['POST', 'GET'])
def login():
# m=message
if request.method =='POST':
email = request.form['email'] # access the data inside
password = request.form['pass']
query = "SELECT * FROM login WHERE user_email = "+"'"+email+"'"
# query1 = "SELECT user_category FROM login WHERE user_email=%s"
connection = connect()
cur=connection.cursor()
try:
cur.execute(query)
user = cur.fetchone()
connection.commit()
if user:
if bcrypt.check_password_hash(user[2], password):
session['email'] = email
session['user'] = user[0]
session['type'] = user[6]
session['person'] = user[3]
session['cdis'] = 0.00
session['pdis'] = 0.00
# if session['user'] == 'customew'
if user[6]=="supplier":
session['role'] = user[3]
flash("Login Successfull!!", 'success')
return redirect(url_for('supplier'))
else:
flash("Login Successfull!!", 'success')
return redirect(url_for('mhome'))
else:
flash("Password incorrect!!", 'danger')
return redirect(url_for('login'))
else:
flash("Email id does not exist!!", 'danger')
return redirect(url_for('login'))
except mysql.connector.Error as e:
print(e)
return None
finally:
cur.close()
connection.close()
# user = Login.query.filter_by(user_email=email).first()
# if user and bcrypt.check_password(password=password):
# connection.commit()
# cur.close()
# # message = 'Logged In Successfully!!'
# return redirect(url_for('customer'))
# else:
# return redirect(url_for('login'))
# # message = 'Wrong email or password'
return render_template("abc.html", title='Login')
@app.route("/signup", methods=['POST', 'GET'])
def signup():
# message = ' '
if request.method == 'POST':
hashed_pass = bcrypt.generate_password_hash(request.form['pass']).decode('utf-8')
fname = request.form['fname']
lname = request.form['lname']
email = request.form['email']
address = request.form['address']
category = request.form['category']
connection = connect()
cur = connection.cursor()
cur.execute("SELECT * FROM login WHERE user_email = %s", (email, ))
existing_user = cur.fetchone()
if existing_user is None:
cur.execute("INSERT INTO login(user_email, user_pass, user_first_name, user_last_name, user_address, user_category) VALUES(%s, %s, %s, %s, %s, %s)",(email, hashed_pass, fname, lname, address, category))
connection.commit()
cur.close()
connection.close()
# message = "SignUp successfull!!"
flash("SignUp Successfull!!", 'success')
return redirect(url_for('login'))
# existing_user = Login.query.filter_by(user_email=email).first() # Check if user exists
# if existing_user is None:
# user = Login(user_id=i, user_email=email, user_pass=hashed_pass, user_first_name=fname, user_last_name=lname, user_address=address, user_category=category)
# db.session.add(user)
# db.session.commit()
else:
flash("Email id already exists!!", 'danger')
cur.close()
connection.close()
return redirect(url_for('signup'))
# message = "User already exists with that email id"
return render_template("signup.html", title='SignUp')
@app.route("/cart")
@customer_required
def cart():
items, subtotal, items_len = cart_items()
buid = buyid()
coupon = 0.00
if session['type']=='powner':
dis = subtotal*0.25
session['pdis'] = dis
val = 200
else:
dis = 0.00
val = 1
return render_template("cart.html", items=items, val=val, subtotal=subtotal, items_len=items_len, coupon=coupon, dis=dis, buid=buid)
@app.route("/ccoupon", methods=['POST'])
@customer_required
def coupon():
items, subtotal, items_len = cart_items()
buid = buyid()
couponcode = request.form.get('coupo', None)
if session['type']=='customer':
if couponcode=='SOMAIYA15':
flash("Coupon applied successfully!!", category="success")
coupon = subtotal*0.15
session['cdis'] = coupon
dis = 0.00
val = 1
return render_template("cart.html", items=items, val=val, subtotal=subtotal, items_len=items_len, coupon=coupon, dis=dis, buid=buid)
else:
flash("Coupon Invalid!!", category="danger")
coupon = 0.00
dis = 0.00
val = 1
return render_template("cart.html", items=items, val=val, subtotal=subtotal, items_len=items_len, coupon=coupon, dis=dis, buid=buid)
else:
flash("Coupon Discount is only for Normal Customers and not for Pharmacy Store Owners!!", category="danger")
coupon = 0.00
dis = subtotal*0.25
session['pdis'] = dis
val = 200
return render_template("cart.html", items=items, val=val, subtotal=subtotal, items_len=items_len, coupon=coupon, dis=dis, buid=buid)
@app.route("/product/<pur>", methods=['POST','GET'])
@customer_required
def product(pur):
return product_detail(pur)
@app.route("/supproduct/<pur>", methods=['POST','GET'])
@supplier_required
def supproduct(pur):
return supproduct_detail(pur)
@app.route("/checkout", methods=['POST','GET'])
@customer_required
def checkout():
if(request.method == 'POST'):
return checkout_details()
if(request.method == 'GET'):
return normal_checkout()
@app.route("/search", methods=['POST','GET'])
@customer_required
def qsearch():
return query_search()
@app.route("/favourites", methods=['POST','GET'])
@customer_required
def favourites():
if (request.method == 'GET'):
return favourite()
if (request.method == 'POST'):
return fav()
@app.route("/hdemo", methods=['POST', 'GET'])
def demo():
if request.method=='POST':
flash("Thank you for your Feedback!!", category="success")
return redirect(request.referrer)
@app.route("/supplier", methods=['POST','GET'])
@supplier_required
def supplier():
if request.method == 'GET':
return render_template("supplier.html")
if request.method == 'POST':
if(request.form.get('type', None) == 'add'):
if(supadd()):
flash("Medicine added to the database!!", category="success")
else:
flash("Failed to add medicine to the database!!", category="danger")
if(request.form.get('type', None) == 'update'):
msg, category=supupdate()
flash(msg, category=category)
return redirect(request.referrer)
@app.route("/supsearch", methods=['POST','GET'])
@supplier_required
def qsearchsup():
return ssearch()
@app.route("/ohistory/<bid>", methods=['POST','GET'])
@customer_required
def ohistory(bid):
return orhistory(bid)
@app.route("/updatepassword", methods=['POST','GET'])
@login_required
def updatepassword():
if request.method=='GET':
return render_template("password.html")
if request.method=='POST':
return upass()
@app.route("/singleproduct/<pid>/<rol>", methods=['POST','GET'])
@customer_required
def singleproduct(pid, rol):
return single_product(pid, rol)
@app.route("/cart_item", methods=['POST'])
@customer_required
def item():
if request.method == "POST":
if(request.form.get('type', None) == 'delete'):
delete_item(request.form.get('item_id'))
flash("Item removed from the cart!!", category="success")
elif(request.form.get('type', None) == 'update'):
msg, cat = update_item(request.form.get('item_id', None),
request.form.get('quantity', None),
request.form.get('med_id', None),
request.form.get('med_role', None))
flash(msg, category=cat)
else:
if session['type']=="powner":
add_item(request.form.get('med_id', None),
200,
request.form.get('med_role', None))
else:
add_item(request.form.get('med_id', None),
request.form.get('quantity', None),
request.form.get('med_role', None))
return redirect(request.referrer)
@app.route("/mhome")
@customer_required
def mhome():
categories=category_items()
categories1=category_items()
categories2=category_items()
subtotal, len_items = cart_value()
buid = buyid()
return render_template("home.html", categories=categories, categories1=categories1, categories2=categories2 ,subtotal=subtotal, len_items=len_items, buid=buid)
if __name__ == "__main__":
app.run(debug=True)