-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
399 lines (331 loc) · 15.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
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
# Imports:
from flask import Flask, render_template, request, redirect, url_for, session,flash,make_response,session
import datetime
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re
import os
import twilio
from twilio.rest import Client
# Configs:
app = Flask(__name__)
app.secret_key = 'secret'
app.config['MYSQL_HOST'] = 'b2pj7xtitxa67llbua43-mysql.services.clever-cloud.com'
app.config['MYSQL_USER'] = 'umo0wrj8n1rqnx90'
app.config['MYSQL_PASSWORD'] = 'ETintB86zfDXSCCMrspY'
app.config['MYSQL_DB'] = 'b2pj7xtitxa67llbua43'
mysql = MySQL(app)
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
# @app.before_request
# def before_request():
# if not request.is_secure and app.env != "development":
# url = request.url.replace("http://","https://",1)
# code = 301
# return redirect(url,code=code)
# Routes:
@app.route('/customer/login', methods=['GET', 'POST'])
def login():
msg=''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
username = request.form['username']
password = request.form['password']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM customer WHERE username = %s AND password = %s', (username, password,))
customer = cursor.fetchone()
if customer:
session['loggedin'] = True
session['id'] = customer['cus_id']
session['username'] = customer['username']
session['mobile'] = customer['mobileno']
session['region'] = customer['region']
return redirect(url_for('customer_display'))
else:
msg = 'Incorrect username/password!'
return render_template('cuslogin.html',msg=msg)
@app.route('/contact', methods=['GET', 'POST'])
def contact():
return render_template('contact.html')
@app.route('/shop/login', methods=['GET', 'POST'])
def login1():
msg=''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
username = request.form['username']
password = request.form['password']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM shop WHERE username = %s AND password = %s', (username, password,))
shop = cursor.fetchone()
if shop:
session['loggedin'] = True
session['id'] = shop['shopid']
session['username'] = shop['username']
session['mobile'] = shop['mobile']
session['region'] = shop['region']
session['address'] = shop['address']
session['shop_name'] = shop['shop_name']
session['owner_name'] = shop['owner_name']
return redirect(url_for('display'))
else:
msg = 'Incorrect username/password!'
return render_template('shoplogin.html',msg=msg)
@app.route("/",methods=['GET','POST'])
@app.route("/home",methods=['GET'])
def home():
try:
if request.method=='GET':
return render_template('index.html')
except Exception as e:
print(e)
@app.route('/logout')
def logout():
session.pop('loggedin', None)
session.pop('id', None)
session.pop('username', None)
return redirect(url_for('home'))
@app.route('/customer/register', methods=['GET', 'POST'])
def register():
msg =''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form:
username = request.form['username']
password = request.form['password']
email = request.form['email']
mobile = request.form['mobile']
region = request.form['region']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM customer WHERE username = %s', (username,))
account = cursor.fetchone()
if account:
msg = 'Account already exists!'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
msg = 'Invalid email address!'
elif not re.match(r'[A-Za-z0-9]+', username):
msg = 'Username must contain only characters and numbers!'
elif not username or not password or not email or not mobile:
msg = 'Please fill out the form!'
else:
cursor.execute('INSERT INTO customer VALUES (NULL, %s, %s, %s,%s,%s)', (username, password, email,mobile,region))
mysql.connection.commit()
msg = 'You have successfully registered!'
return render_template('cuslogin.html')
elif request.method == 'POST':
msg = 'Please fill out the form!'
return render_template('cusregister.html',msg=msg)
@app.route('/shop/register', methods=['GET', 'POST'])
def register1():
msg =''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
username = request.form['username']
password = request.form['password']
ownername = request.form['ownername']
shopname = request.form['shopname']
mobile = request.form['mobile']
address = request.form['address']
region = request.form['region']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM shop WHERE username = %s', (username,))
shop = cursor.fetchone()
if shop:
msg = 'Account already exists!'
elif not re.match(r'[A-Za-z0-9]+', username):
msg = 'Username must contain only characters and numbers!'
elif not username or not password or not mobile or not address or not ownername or not shopname:
# print(1)
msg = 'Please fill out the form!'
else:
# print(3)
cursor.execute('INSERT INTO shop VALUES (NULL, %s, %s, %s,%s,%s,%s,%s)', (ownername,shopname,region,address,mobile,username, password))
mysql.connection.commit()
msg = 'You have successfully registered!'
return render_template('shoplogin.html')
elif request.method == 'POST':
# print(2)
msg = 'Please fill out the form!'
return render_template('shopregister.html',msg=msg)
@app.route('/dashboard', methods=['GET', 'POST'])
def display():
msg=''
now = datetime.datetime.now()
shopid = session['id']
eve = now.replace(hour=19, minute=0, second=0, microsecond=0)
if now <= eve:
date = datetime.date.today() + datetime.timedelta(days=1)
else:
date = datetime.date.today() + datetime.timedelta(days=2)
if request.method == 'POST':
start = request.form['start'][:2]
end = request.form['end'][:2]
shopid = session['id']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM slotbook WHERE date = %s and id_shop=%s', [date,shopid])
boolean = cursor.fetchone()
if boolean:
msg = 'Slot has been already placed for'
return render_template('shopdash.html',date=date,session=session,msg=msg,shopid=shopid)
else:
cursor.execute('INSERT INTO slotbook VALUES (%s, %s, %s,%s)', (shopid,date, start, end,))
mysql.connection.commit()
msg = 'Slot has been Successfully placed for'
return render_template('shopdash.html',date=date,session=session,msg=msg,shopid=shopid)
@app.route('/customer/today',methods=['GET','POST'])
def today():
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
today_date = datetime.date.today()
cursor.execute('select distinct shop.shop_name,shop.owner_name,shop.address,shop.mobile,slotbook.id_shop from shop inner join slotbook on shop.shopid=slotbook.id_shop where slotbook.date=%s',[today_date,])
shops_today = cursor.fetchall()
# print(shops_today,len(shops_today))
length =len(shops_today)
if shops_today:
return render_template('today.html',shops=shops_today,l=length,date=today_date)
else:
return render_template('noshoptoday.html',shops=shops_today,l=length,date=today_date)
@app.route('/customer/tomorrow')
def tomorrow():
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
tomorrow_date = date = datetime.date.today() + datetime.timedelta(days=1)
cursor.execute('select distinct shop.shop_name,shop.owner_name,shop.address,shop.mobile,slotbook.id_shop from shop inner join slotbook on shop.shopid=slotbook.id_shop where slotbook.date=%s',[tomorrow_date,])
shops_tomorrow = cursor.fetchall()
# print(shops_tomorrow,len(shops_tomorrow))
length = len(shops_tomorrow)
if shops_tomorrow:
response = make_response(render_template('tomorrow.html',shops=shops_tomorrow,l=length,date=tomorrow_date))
return response
else:
return render_template('noshoptomorrow.html',shops=shops_tomorrow,l=length,date=tomorrow_date)
@app.route('/today/slots',methods=['GET','POST'])
def today_slot():
today_date = datetime.date.today()
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
shopid = request.form['shopid']
cursor.execute('select start,end from slotbook where date=%s and id_shop=%s',[today_date,shopid])
duration = cursor.fetchone()
start = int(duration['start'])
end= int(duration['end'])
cursor.execute('select slot_time from bookedslots where shop_id=%s and date=%s',[shopid,today_date])
slots = cursor.fetchall()
# print(slots)
sl = len(slots)
return render_template('slot.html',s=start,e=end,shopid=shopid,date=today_date,slots=slots)
@app.route('/tomorrow/slots',methods=['GET','POST'])
def tomorrow_slot():
tomorrow_date = date = datetime.date.today() + datetime.timedelta(days=1)
shopid = request.form['shopid']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('select start,end from slotbook where date=%s and id_shop=%s',[tomorrow_date,shopid])
duration = cursor.fetchone()
# print(duration)
start = int(duration['start'])
end= int(duration['end'])
cursor.execute('select slot_time from bookedslots where shop_id=%s and date=%s',[shopid,tomorrow_date])
slots = cursor.fetchall()
# print(slots)
sl = len(slots)
return render_template('slot.html',s=start,e=end,shopid=shopid,date=tomorrow_date,slots=slots)
@app.route('/booked/customer',methods=['GET','POST'])
def booked_slots():
today_date = datetime.date.today()
shopid = session['id']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM bookedslots where date=%s and shop_id=%s order by cast(slot_time as unsigned);',[today_date,shopid])
booked_slots = cursor.fetchall()
length = len(booked_slots)
# print(length)
return render_template('shopview.html',booked=booked_slots,l=length,shop=shopid)
@app.route('/customer/dashboard',methods=['GET','POST'])
def customer_display():
if request.method=='GET':
now = datetime.datetime.now()
date = datetime.date.today()
# print(date)
mobile = session['mobile']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM bookedslots where cus_mobile=%s order by date;',[mobile])
booked_slots = cursor.fetchall()
shopname=[]
for k in range(len(booked_slots)):
cursor.execute('select shop_name from shop where shopid=%s',[booked_slots[k]['shop_id']])
shop = cursor.fetchone()
shopname.append(shop)
length = len(booked_slots)
return render_template('cusdash.html',date=date,session=session,booked=booked_slots,l=length,name=shopname)
elif request.method=='POST':
time = request.form['slotid']
name = session['username']
mobile = session['mobile']
shopid = request.form['shopid']
date = request.form['date']
# print(date)
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('select * from bookedslots where cus_mobile=%s and date=%s',[mobile,date])
exist = cursor.fetchone()
if exist:
msg='You have already booked a slot for ' + str(date)
# print(msg)
else:
cursor.execute('insert into bookedslots VALUES (%s, %s, %s,%s,%s)',[name,mobile,time,date,shopid])
mysql.connection.commit()
account_sid = 'your_account_sid'
auth_token = 'your_account_token'
client = Client(account_sid, auth_token)
body = 'Your slot has been successfully placed for '+date+' at '+time[:-3]+'.'
message = client.messages.create(
body=body,
from_='your_twilio_number',
to='customer_mobile_number'
)
# print(message.sid)
@app.route('/shopkeeper/<int:shop_id>', methods=['POST', 'GET'])
def shopkeeper_inventory(shop_id):
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
if request.method == 'POST':
name = request.form['name']
unit = request.form['unit']
quantity = request.form['quantity']
shop = shop_id
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM inventory WHERE name = %s AND shop_id = %s',(name,shop,))
data = cursor.fetchone()
if data:
cursor.execute('UPDATE inventory SET QUANTITY = %s WHERE name = %s AND SHOP_ID = %s', (int(quantity) + data['quantity'] ,name,shop,))
else:
cursor.execute('INSERT INTO inventory VALUES (NULL, %s, %s, %s, %s)', (name,unit,quantity,shop,))
mysql.connection.commit()
cursor.close()
return redirect('/shopkeeper/' + str(shop_id))
else:
cursor.execute('SELECT * FROM inventory WHERE shop_id = %s',(shop_id,))
items = cursor.fetchall()
cursor.close()
return render_template('shopinv.html', data = items, shop=shop_id)
@app.route('/shopkeeper/<int:shop_id>/delete/<int:item_id>')
def remove_item(shop_id,item_id):
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('DELETE FROM inventory WHERE id = %s',(item_id,))
mysql.connection.commit()
cursor.close()
return redirect('/shopkeeper/' + str(shop_id))
@app.route('/shopkeeper/<int:shop_id>/edit/<int:item_id>', methods=['GET', 'POST'])
def edit_qty(shop_id,item_id):
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM inventory WHERE id = %s',(item_id,))
item = cursor.fetchone()
val = int(request.form['quantity'])
if not(val < 0):
cursor.execute('UPDATE inventory SET quantity = %s WHERE id = %s',(val,item_id,))
mysql.connection.commit()
cursor.close()
return redirect('/shopkeeper/' + str(shop_id))
@app.route('/customer/<int:shop_id>')
def customer_inventory(shop_id):
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM inventory WHERE shop_id = %s',(shop_id,))
items = cursor.fetchall()
cursor.close()
return render_template('cusinv.html', data = items, shop = shop_id)
@app.route('/about',methods=['GET','POST'])
def about():
return render_template('about.html')
@app.route('/service-worker.js')
def sw():
return app.send_static_file('service-worker.js')
port = int(os.getenv('PORT', 5001))
if __name__ == '__main__':
app.run(host ='0.0.0.0', port = port, debug = True)