-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathques.py
233 lines (218 loc) · 7.79 KB
/
ques.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
from db import get_db
import pandas as pd
from dbUser import MyUser
from waiter import Waiter
from notification import Notification
import Lineder_logging
my_logger = Lineder_logging.get_logger ('Ques')
DEBUG = False
class Ques:
def __init__(self, callee_id, waiter_id):
self.id = None
self.calle_id = callee_id
self.waiter_id = waiter_id
self.place_in_line = None
# @staticmethod
# def get_all_ranges():
# all_ranges: list = []
# db = get_db()
# result = db.execute(
# "SELECT * FROM freebusy"
# ).fetchall()
# all_ranges = [range_instance[0] for range_instance in result]
@staticmethod
def remove_from_que (callee_address, waiter_address) -> bool:
"""
Removes the waiter_address from the callee que
Used when the waiter does not want or should not wait anymore for the callee
:param callee_address:
:param waiter_address:
:return:
"""
callee_id = MyUser.get_id_by_email (callee_address)
waiter_id = MyUser.get_id_by_email (waiter_address)
waiter_place = Ques.get_place_in_line (waiter_id, callee_id)
db = get_db()
try:
temp = db.execute (
"DELETE from Ques WHERE callee_id = ? AND waiter_id = ?",
(callee_id, waiter_id)
)
print("DELETE from Ques WHERE callee_id = %s AND waiter_id = %s", callee_id, waiter_id)
print("temp:", temp)
except Exception as msg:
print("error:", msg)
pass
db.commit()
if Ques.get_place_in_line(waiter_id, callee_id) is None:
db.execute(
"UPDATE ques SET place_in_line = place_in_line - 1"
" WHERE place_in_line > ? AND callee_id =?",
(waiter_place, callee_id)
)
else:
db.rollback()
return False
db.commit()
db.close()
return Ques.get_place_in_line(waiter_id, callee_id) is None
@staticmethod
def get_my_que(user_address):
user_id = MyUser.get_id_by_email(user_address)
db = get_db()
que_ids = db.execute(
"SELECT * FROM ques WHERE callee_id = ?",
(user_id,)
).fetchall()
# print(que_ids)
que: list[Waiter] = []
for waiter in que_ids:
c_id = waiter[1]
c_user = MyUser.get(c_id)
c_name = c_user.name
c_address = c_user.email
c_phone = c_user.phone
c_waiter = Waiter(
email=c_address,
name=c_name,
phone=c_phone,
place=waiter[2]
)
que.append(c_waiter)
# print("que:", que)
# sort 'que' according to the 'place' attribute of waiter
que.sort(key=lambda x: x.place)
# print("que:", que)
return que
@staticmethod
def get_notifications(user_address):
user_id = MyUser.get_id_by_email(user_address)
db = get_db()
notifications_rows = db.execute(
"SELECT * FROM ques WHERE waiter_id = ? AND place_in_line = 1", (user_id,)
).fetchall()
notifications: list[Notification] = []
for notification in notifications_rows:
callee_id = notification[0]
callee = MyUser.get(callee_id)
notifications.append(Notification(callee.name, callee.phone, callee.email))
# print("notifications:", notifications)
return notifications
@staticmethod
def move_to_top(waiter_address, calle_address):
waiter_id = MyUser.get_id_by_email(waiter_address)
callee_id = MyUser.get_id_by_email(calle_address)
waiter_place = Ques.get_place_in_line(waiter_id, callee_id)
loop_start = waiter_place - 1
print("place:", waiter_place)
print("start:", loop_start)
db = get_db()
# loop to go over all the waiters in the que
# and move each one one place back
db.execute(
"UPDATE ques SET place_in_line = place_in_line + 1"
" WHERE place_in_line < ? AND callee_id =?",
(waiter_place, callee_id)
)
print("moved down people above")
Ques.print_table()
# move the waiter that needs to be moved to the top to the top
db.execute(
"UPDATE ques set place_in_line = 1 WHERE waiter_id = ? AND callee_id = ?",
(waiter_id, callee_id)
)
db.commit() # commit to finish transaction
db.close()
print("moved waiter up")
Ques.print_table()
updated_place = Ques.get_place_in_line(waiter_id, callee_id) # waiter place after change
if updated_place == 1: # check if the waiter is actually at the top of the que
return True
return False
@staticmethod
def get_place_in_line(waiter_id, callee_id):
db = get_db()
place = db.execute(
"SELECT place_in_line FROM ques WHERE callee_id = ? AND waiter_id = ?",
(callee_id, waiter_id)
).fetchone()
if place is None:
return None
return place[0]
@staticmethod
def get_que_size(owner_id):
db = get_db()
que = db.execute(
"SELECT place_in_line FROM ques WHERE callee_id = ?", (owner_id,)
).fetchall()
print("que:", que)
for q in que:
print(q)
print(len(que))
if not que:
return 0
return len(que)
@staticmethod
def get_user_que(owner_address):
owner_id = MyUser.get_id_by_email(owner_address)
db = get_db()
que = db.execute(
"SELECT * FROM ques WHERE callee_id = ?", (owner_id,)
).fetchall()
if not que:
return None
return que
@staticmethod
def create_que_item(callee_address, waiter_address):
"""
:param calle_address:
:param waiter_address:
:return: if successful - place in line,
if not - 0.
"""
waiter_id = MyUser.get_id_by_email(waiter_address)
callee_id = MyUser.get_id_by_email(callee_address)
callee_que = Ques.get_user_que(callee_address)
# create a list containing all of the id's
# of the users waiting in the callee's line
# print("callee_que:", callee_que[0][0], callee_que[0][1], callee_que[0][2])
if callee_que:
print("que alive")
callee_que_waiter_ids = [item[1] for item in callee_que]
if waiter_id in callee_que_waiter_ids:
print("already in que")
return Ques.get_place_in_line(waiter_id, callee_id) # return value for "already in the que"
db = get_db()
place_in_line = Ques.get_que_size(callee_id) + 1
db.execute(
"INSERT INTO ques (callee_id, waiter_id, place_in_line) VALUES (?, ?, ?)",
(callee_id, waiter_id, place_in_line)
)
db.commit()
db.close()
else:
print("que dead")
callee_que_waiter_ids = []
db = get_db()
place_in_line = 1
db.execute(
"INSERT INTO ques (callee_id, waiter_id, place_in_line) VALUES (?, ?, ?)",
(callee_id, waiter_id, place_in_line)
)
db.commit()
db.close()
print(callee_que_waiter_ids)
return place_in_line
@staticmethod
def get_que_item(item_id):
db = get_db()
item = db.execute(
"SELECT * FROM ques WHERE rowid = ?", (item_id,)
).fetchone()
if not item:
return None
return item
@staticmethod
def print_table():
db = get_db()
print(pd.read_sql_query("SELECT * FROM ques", db))