This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
363 lines (267 loc) · 9.02 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
from flask import Flask
from flask_socketio import SocketIO
from markupsafe import escape
import os
import json
# import other .py files
import handler.ManagerHandler as MH
import handler.UserHandler as UH
import handler.RestaurantHandler as RH
import handler.FoodHandler as FH
import handler.CommentHandler as CH
import db as db
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'app.sqlite'),
)
app.config.from_pyfile('config.py', silent=True)
db.init_app(app)
socketio = SocketIO(app, cors_allowed_origins="*")
@app.route("/")
def sayHello():
return 'Hello!'
@app.route("/create")
def createUser():
res = handle_create_user()
return res
@app.route("/get/<phone_number>")
def getUser(phone_number):
res = UH.get_user_by_phone(escape(phone_number))
return "None" if res is None else res
# @app.route("/<name>")
# def hello(name):
# return f"Hello, {escape(name)}!"
# --------------------------------------------------------------------
# TEST
# --------------------------------------------------------------------
@socketio.on('give me user name')
def handle_testtt():
print('message received')
socketio.emit('get user name', 'alireza mohammadi')
print('message sent')
# --------------------------------------------------------------------
# USER EVENTS
# --------------------------------------------------------------------
@socketio.on('create user')
def handle_create_user(json_req):
response = UH.create(json_req)
# socketio.emit('my response', response)
# print(response)
print(response)
if response == 'confirm password error':
socketio.emit('u confirm password error', 1)
elif response == 'bad password':
socketio.emit('u bad password', 1)
elif response == 'rep':
socketio.emit('u rep', 1)
elif response == 'User registered successfully':
socketio.emit('user registered', 1)
else:
socketio.emit('u bad req', 1)
return response
@socketio.on('test_server')
def handle_test():
print('test function')
socketio.send('message', 'amir')
socketio.emit('test_client', 'amir')
@socketio.on('login user')
def some_func(json_req):
json_dict = json.loads(json_req)
phone_number = json_dict["phone_number"]
print('phone_number:', phone_number)
print('type(phone_number):', type(phone_number))
answer = UH.get_user_by_phone(phone_number)
print(answer)
wrong_pass = False
if answer is not None:
answer_dict = json.loads(answer)
wrong_pass = answer_dict['password'] != json_dict['password']
socketio.emit('user found', ('0' if answer is None else ('wrong pass' if wrong_pass else '1')))
@socketio.on('message')
def handle_test_msg(msg):
print('test msg function')
socketio.send(msg, broadcast=True)
return None
@socketio.on('update user')
def handle_update_user(json_req):
response = UH.update(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('get user')
def handle_get_user():
# dictt = {
# 'phone_number': '333',
# 'password': '123@'
# }
json_req = ''
# json_req = json.dumps(dictt)
response = UH.get_user_by_phone(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
# socketio.emit('my response', response)
# print(response)
return response
@socketio.on('make order')
def handle_make_order(json_req):
"""
phone_number
restaurant_name
food
count
"""
response = UH.submit_order(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('submit comment')
def handle_submit_comment(json_req):
response = UH.submit_comment(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('get favorite foods list')
def handle_get_favorite_foods_list(json_req):
response = UH.get_favorite_foods_list(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('get orders history')
def handle_get_orders_history(json_req):
response = UH.get_orders_history(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
# --------------------------------------------------------------------
# MANAGER EVENTS
# --------------------------------------------------------------------
@socketio.on('create manager')
def handle_create_manager(json_req):
response = MH.create(json_req)
# socketio.emit('my response', response)
# print(response)
print(response)
if response == 'confirm password error':
socketio.emit('m confirm password error', 1)
elif response == 'bad password':
socketio.emit('m bad password', 1)
elif response == 'rep':
socketio.emit('m rep', 1)
elif response == 'Manager registered successfully':
socketio.emit('manager registered', 1)
else:
socketio.emit('m bad req', 1)
return response
@socketio.on('login manager')
def some_func2(json_req):
json_dict = json.loads(json_req)
email = json_dict["email"]
answer = MH.get_by_email(email)
print(answer)
wrong_pass = False
if answer is not None:
answer_dict = json.loads(answer)
wrong_pass = answer_dict['password'] != json_dict['password']
socketio.emit('manager found', ('0' if answer is None else ('wrong pass' if wrong_pass else '1')))
@socketio.on('update manager')
def handle_update_manager(json_req):
response = MH.update(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('get manager')
def handle_get_manager(json_req):
response = MH.get_by_email(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('add food')
def handle_add_food(json_req):
response = FH.create_food(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('get food from manager', json_req)
print(response)
@socketio.on('remove food')
def handle_remove_food(json_req):
response = MH.removeFood(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('disable food')
def handle_disable_food(json_req):
response = MH.disableFood(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('accept order')
def handle_accept_order(json_req):
response = MH.acceptOrder(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
@socketio.on('start status ctr')
def handle_masmali():
print("hereeeeeeeeeee")
import time
socketio.emit('status', 'در انتظار تایید رستوران')
time.sleep(10)
socketio.emit('status', 'در حال آماده سازی')
time.sleep(10)
socketio.emit('status', 'در حال ارسال توسط پیک')
time.sleep(10)
socketio.emit('status', 'تحویل داده شده')
@socketio.on('reply to comment')
def handle_reply_to_comment(json_req):
response = MH.replyToComment(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
# --------------------------------------------------------------------
# RESTAURANT EVENTS
# --------------------------------------------------------------------
@socketio.on('create restaurant')
def handle_create_restaurant(json_req):
response = RH.create(json_req)
# print('received my event: ' + str(json_req))
# response = {
# "status_code" : "200"
# }
socketio.emit('my response', response)
# --------------------------------------------------------------------
# FOOD EVENTS
# --------------------------------------------------------------------
@socketio.on('add food ')
def handle_create_restaurant(json_req):
response = FH.get_by_food_name(json_req)
socketio.emit('search result page - get foods', response)
if __name__ == '__main__':
socketio.run(app, port=4001)