-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
246 lines (225 loc) · 9.5 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
import datetime
from os import name
import json
from flask import *
import pyrebase
import ast
config = {
"apiKey": "AIzaSyCg4U026dC23tr0anVQIB3lyIg_-grc6Ak",
"authDomain": "hackathon-3dc95.firebaseapp.com",
"projectId": "hackathon-3dc95",
"storageBucket": "hackathon-3dc95.appspot.com",
"messagingSenderId": "242647933043",
"appId": "1:242647933043:web:87f2639e8f4e71f39e4a06",
"measurementId": "G-H2QPZR2PBG",
"databaseURL" : "https://hackathon-3dc95-default-rtdb.asia-southeast1.firebasedatabase.app/"
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
db = firebase.database()
app = Flask(__name__)
#Home route
@app.route('/')
def home():
return render_template("onpg.html")
@app.route('/loginpage')
def loginpage():
return render_template("login.html")
@app.route('/aboutpage')
def aboutpage():
return render_template("about.html")
@app.route('/registerpage')
def registerpage():
return render_template("registration.html")
@app.route('/searchpage')
def searchpage():
return render_template("search.html")
@app.route('/respage',methods = ['POST'])
def respage():
if request.method == 'POST':
uid = request.form['uid']
data = request.form['data']
datatime_book = request.form['datetime_book']
seats = request.form['seats']
data = ast.literal_eval(data)
seats_available = data[datatime_book]
return render_template("res.html",data = {"uid":uid,"value":data,"seats":seats_available,"seats_tobook":seats,"datatime_book":datatime_book})
#Login route
@app.route('/login',methods = ['POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
try:
user = auth.sign_in_with_email_and_password(email,password)
customer = db.child("users").child(user['localId']).get()
name = customer.val()["name"]
return render_template("search.html", data = {"id" : user['localId'], "name": name})
except:
return {"auth" : False, "msg":"Invalid Credentials"}
else:
return {"auth":"Something is wrong"}
@app.route('/loginscreen',methods = ['POST'])
def loginscreen():
if request.method == 'POST':
uid = request.form['uid']
try:
customer = db.child("users").child(uid).get()
name = customer.val()["name"]
return render_template("search.html", data = {"id" : uid, "name": name})
except:
return {"auth" : False, "msg":"Invalid Credentials"}
else:
return {"auth":"Something is wrong"}
#register route
@app.route('/register',methods = ['POST'])
def register():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
name = request.form['name']
user = 'arman'
try:
user = auth.create_user_with_email_and_password(email,password)
data = {
"name":name,
"booked_seats" : [0,]
}
results = db.child("users").child(user['localId']).set(data)
return render_template("search.html", data = {"id" : user['localId'], "name": name})
except:
return {"auth" : False, "msg":"Email ID already registered"}
else:
return {"auth":"Something is wrong"}
#search resto
@app.route('/search',methods = ['POST'])
def search():
if request.method == 'POST':
users = db.child("resto_list").get()
date = request.form['date']
time = request.form['time']
place = request.form['place']
seats = request.form['seats']
uid = request.form['uid']
available = []
for i in users.val():
if i!=None and i[date+time] >= int(seats) and place.lower() in i['address'].lower():
available.append(i)
if(len(available)!=0):
return render_template("list.html", data = {"value":available,"uid" : uid,"datetime_book":date+time,"seats":seats})
else:
return {'msg':'No restaurents found'}
#booking seats
@app.route('/book',methods = ['POST'])
def book():
if request.method == 'POST':
uid = request.form['uid']
resto_name = request.form['resto_name']
resto_add = request.form['resto_add']
seats = request.form['seats']
datetime = request.form['datetime']
users = db.child("resto_list").get()
customer = db.child("users").child(uid).get()
old_tickets = customer.val()['booked_seats']
for i in users.val():
if i!=None and i["name"]==resto_name and i["address"] == resto_add and i[datetime] >= int(seats):
data = {
"name" : i["name"],
"address" : i["address"],
"reviews": i['reviews'],
"total_seats":i["total_seats"],
datetime : i[datetime]-int(seats)
}
index = users.val().index(i)
db.child("resto_list").child(index).update(data)
instance_found = 0
for j in old_tickets:
if j!=0 and j!=None and j["name"]==resto_name and j["address"] == resto_add and j['datetime'] == datetime:
order_data = {
"name" : j["name"],
"address" : j["address"],
"seats" : int(j['seats'])+int(seats),
"datetime" : j['datetime']
}
old_index = old_tickets.index(j)
db.child("users").child(uid).child("booked_seats").child(old_index).update(order_data)
instance_found = 1
if instance_found == 0:
order_data = {
"name" : i["name"],
"address" : i["address"],
"seats" : seats,
"datetime" : datetime
}
old_tickets.append(order_data)
customer_data = {
"name" : customer.val()['name'],
"booked_seats" : old_tickets
}
customer = db.child("users").child(uid).update(customer_data)
return render_template("thank_you_book.html",data = {"msg":"Succesfully booked your seats","uid":uid,"secmsg":"Click the button below to go to the search page"})
#cancel order
@app.route('/cancel',methods = ['POST'])
def cancel():
if request.method == 'POST':
uid = request.form['uid']
resto_name = request.form['resto_name']
resto_add = request.form['resto_add']
seats = request.form['seats']
datetime = request.form['datetime']
users = db.child("resto_list").get()
customer = db.child("users").child(uid).get()
old_tickets = customer.val()['booked_seats']
name = customer.val()['name']
print(old_tickets)
for i in users.val():
if i!=None and i["name"]==resto_name and i["address"] == resto_add:
data = {
"name" : i["name"],
"address" : i["address"],
"reviews": i['reviews'],
"total_seats":i["total_seats"],
datetime : i[datetime]+int(seats)
}
index = users.val().index(i)
for j in old_tickets:
if j!=0 and j!=None and j["name"]==resto_name and j["address"] == resto_add and j['datetime'] == datetime and int(j["seats"]) >= int(seats):
order_data = {
"name" : j["name"],
"address" : j["address"],
"seats" : int(j["seats"])-int(seats),
"datetime" : datetime
}
old_index = old_tickets.index(j)
db.child("users").child(uid).child("booked_seats").child(old_index).update(order_data)
db.child("resto_list").child(index).update(data)
return render_template("thank_you_cancel.html",data = {"msg":"Succesfully cancelled your tickets","uid":uid,"name":name,"secmsg":"Click the button below to go to the my bookings page"})
#mybookings
@app.route('/mybookings',methods = ['POST'])
def mybookings():
if request.method == 'POST':
uid = request.form['uid']
customer = db.child("users").child(uid).get()
name = customer.val()['name']
old_tickets = customer.val()['booked_seats']
bookings = []
for k in old_tickets:
if k!=0 and k!= None:
bookings.append(k)
return render_template("bookings.html", data = {"value":bookings,"name":name,"uid":uid})
#give reviews
@app.route('/reviews',methods = ['POST'])
def reviews():
if request.method == 'POST':
resto_name = request.form['resto_name']
resto_add = request.form['resto_add']
reviews = request.form['reviews']
users = db.child("resto_list").get()
for i in users.val():
if i!=None and i["name"]==resto_name and i["address"] == resto_add:
i['reviews'] = (int(i['reviews']) + int(reviews))/2
index = users.val().index(i)
db.child("resto_list").child(index).update(i)
return {"msg":"success"}
if __name__ == '__main__':
app.run(debug = True)