-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeal_requests.py
353 lines (267 loc) · 11.4 KB
/
meal_requests.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
from ast import Is
from requests import get
from database import new_connection, close_connection, update_request_usage_metric
from matcher import match_requests
from big_lists import dhall_list
from datetime import datetime, date
from sys import stdout
import random
import string
def add_request(netid, meal_type, start_time, end_time, dhall_arr, atdhall):
cur, conn = new_connection()
# confirm the user does not have an existing request for the current meal period
if not validate_request(netid, meal_type):
print("Cannot add request: there is already a request in the current meal period")
return False
sql = "INSERT INTO requests (REQUESTID, NETID,BEGINTIME,ENDTIME, LUNCH,"
for dhall in dhall_list:
sql += "{},".format(dhall)
dhall_strargs = "%s, " * len(dhall_list)
sql += "ATDHALL, ACTIVE) VALUES (%s, %s, %s, %s, %s, {}%s, %s)".format(
dhall_strargs)
requestId = ''.join(random.choice(
string.ascii_letters + string.digits) for _ in range(16))
val = [requestId, netid, start_time, end_time, meal_type]
val += dhall_arr
val += [atdhall, True]
cur.execute(sql, val)
close_connection(cur, conn)
clean_requests()
match_requests()
# increment request counter
update_request_usage_metric()
return True
# Remove request from request table
def modify_request(request_id, match_id):
sql = "UPDATE requests SET MATCHID = %s WHERE REQUESTID = %s"
val = (match_id, request_id)
cur, conn = new_connection()
cur.execute(sql, val)
close_connection(cur, conn)
remove_requests([request_id])
print("Modified request", file=stdout)
def get_all_requests(netid):
# remove expired requests
clean_requests()
dhall_str = ', '.join(dhall_list)
query = """SELECT begintime, endtime, lunch, {} ,atdhall, requestid FROM requests as r
WHERE r.netid = %s
AND r.active = TRUE""".format(dhall_str)
cur, conn = new_connection()
cur.execute(query, [netid])
all_requests = cur.fetchall()
close_connection(cur, conn)
return all_requests
def validate_request(netid, meal_type):
# search for active requests made by user with netid
sql = """SELECT *
FROM requests
WHERE netid = %s AND active = TRUE AND LUNCH = %s"""
cur, conn = new_connection()
cur.execute(sql, (netid, meal_type))
rows = cur.fetchall()
close_connection(cur, conn)
return len(rows) == 0
def clean_requests():
sql = """SELECT * FROM requests
WHERE active = TRUE"""
cur, conn = new_connection()
cur.execute(sql)
rows = cur.fetchall()
close_connection(cur, conn)
now = datetime.now()
# list of requestids for requests that have expired
old_requests = [row[0] for row in rows if row[3] < now]
remove_requests(old_requests)
def remove_requests(requestids: list):
sql = """UPDATE requests
SET active = FALSE
WHERE requestid = %s"""
cur, conn = new_connection()
for id in requestids:
# print('request id to remove: ', id)
cur.execute(sql, [id])
close_connection(cur, conn)
def configure_recurring_request(netid, start_time_datetime, end_time_datetime, days, location):
# update user's configured recurring request
update_user_sql = ''' UPDATE users
SET recur = TRUE, recur_begintime = %s, recur_endtime = %s, days = %s, location = %s
WHERE netid = '{}' '''.format(netid)
cur, conn = new_connection()
cur.execute(update_user_sql, (
start_time_datetime, end_time_datetime, days, location))
close_connection(cur, conn)
# given info as stored in DB for recurring requests, convert information to be used in /submitrequest route
def recur_request_to_normal_request(recur_request_dict):
normal_request_dict = {}
normal_request_dict['netid'] = recur_request_dict['netid']
normal_request_dict['days'] = recur_request_dict['days']
# all recurring requests are scheduled
normal_request_dict['at_dhall'] = False
# convert location to dhall array
normal_request_dict['dhall_arr'] = [
hall_name in recur_request_dict['location'] for hall_name in dhall_list]
print(normal_request_dict['dhall_arr'])
begin = recur_request_dict['recur_begintime']
end = recur_request_dict['recur_endtime']
def _zero_pad_minute(min_num):
if min_num < 10:
return ('0' + str(min_num))
else:
return str(min_num)
# strip day and make it current, keep hour
normal_starttime = datetime.fromisoformat(
date.today().isoformat() + " " + str(begin.hour) + ":" + _zero_pad_minute(begin.minute) + ":00")
normal_endtime = datetime.fromisoformat(
date.today().isoformat() + " " + str(end.hour) + ":" + _zero_pad_minute(end.minute) + ":00")
print('Begin: ' + normal_starttime.isoformat())
normal_request_dict['starttime'] = normal_starttime
normal_request_dict['endtime'] = normal_endtime
# assume request is already validated for lunch/brunch by getting here,
# so include brunch hours in valid request
if (normal_starttime.hour >= 10 and normal_starttime.hour <= 14
and normal_endtime.hour >= 10 and normal_endtime.hour <= 14):
# Lunch is determined by boolean
normal_request_dict['meal_type'] = True
elif (normal_starttime.hour >= 17 and normal_starttime.hour <= 20
and normal_endtime.hour >= 17 and normal_endtime.hour <= 20):
normal_request_dict['meal_type'] = False
else:
print('UH-OH DETERMING MEAL TYPE WENT WRONG')
return
return normal_request_dict
# return array of dictionaries, each dictionary contains information for a request
# filters for current day
def get_all_recurring_requests():
now = datetime.now().replace(second=0, microsecond=0)
ISOWEEKDAY_NUM_TO_DAY_CHAR = {1: 'M', 2: 'T',
3: 'W', 4: 'R', 5: 'F', 6: 'S', 7: 'U'}
# get enabled recurring requests scheduled on today's weekday
sql = """ SELECT u.netid, u.recur_begintime, u.recur_endtime, u.days, u.location
FROM users as u WHERE (u.recur = TRUE AND u.days LIKE '%{}%')
""".format(str(ISOWEEKDAY_NUM_TO_DAY_CHAR[now.isoweekday()]))
cur, conn = new_connection()
cur.execute(sql)
rows = cur.fetchall()
close_connection(cur, conn)
keys = ["netid", 'recur_begintime', 'recur_endtime', 'days', 'location']
reqs = []
for row in rows:
reqs.append(dict(zip(keys, row)))
return reqs
def execute_recurring_requests_lunch():
print('LUNCH JOB')
recur_reqs_dicts = get_all_recurring_requests()
normalized_req_dicts = []
print('made past get')
for recur_dict in recur_reqs_dicts:
print('going into iter')
normal_req_dict = recur_request_to_normal_request(recur_dict)
# if boolean lunch field is true
if normal_req_dict['meal_type'] == True:
normalized_req_dicts.append(normal_req_dict)
print('made it out of iter')
for req in normalized_req_dicts:
success = add_request(req['netid'], req['meal_type'], req['starttime'],
req['endtime'], req['dhall_arr'], req['at_dhall'])
if success:
print('recur request for ' + req['netid'] + ' added to pool')
else:
print('error aah')
print(req)
def execute_recurring_requests_dinner():
print('DINNER JOB RUNNING')
recur_reqs_dicts = get_all_recurring_requests()
normalized_req_dicts = []
print('made past get')
for recur_dict in recur_reqs_dicts:
print('going into iter')
normal_req_dict = recur_request_to_normal_request(recur_dict)
# if boolean lunch field is true
if normal_req_dict['meal_type'] == False:
normalized_req_dicts.append(normal_req_dict)
print('made it out of iter')
print(normalized_req_dicts)
for req in normalized_req_dicts:
success = add_request(req['netid'], req['meal_type'], req['starttime'],
req['endtime'], req['dhall_arr'], req['at_dhall'])
if success:
print('recur request for ' + req['netid'] + ' added to pool')
else:
print('error aah')
print(req)
def get_users_recurring_request(netid):
update_user_sql = ''' SELECT netid, recur, recur_begintime, recur_endtime, days, location
FROM users
WHERE netid = '{}' '''.format(netid)
cur, conn = new_connection()
cur.execute(update_user_sql)
row = cur.fetchone()
close_connection(cur, conn)
keys = ["netid", 'recur', 'recur_begintime',
'recur_endtime', 'days', 'location']
req_dict = dict(zip(keys, row))
print('testyyyy ' + str(req_dict))
if req_dict != None and req_dict['recur'] == True:
print('req dict')
print('Non-null reqdict ' + str(req_dict))
return req_dict
else:
print('Not valid RR ' + str(req_dict))
return None
def recurring_meal_string_to_days(day_string):
day_char_arr = list(day_string)
day_char_to_full_name = {'M': "Monday", 'T': 'Tuesday', 'W': "Wednesday",
'R': 'Thursday', 'F': "Friday", "S": "Saturday", "U": 'Sunday'}
days = ""
for day_char in day_char_arr:
days += (day_char_to_full_name[day_char]+",")
# for one day, there will be 2 elements in the split
if len(days.split(',')) == 2:
return days.split(',')[0]
# 2 days are presenr
elif len(days.split(',')) == 3:
sep_days = days.split(',')
print(sep_days[0] + ' and ' + sep_days[1])
return sep_days[0] + ' and ' + sep_days[1]
elif len(days.split(',')) > 3:
day_str = ""
sep_days = days.split(',')
for i in range(len(sep_days) - 2):
day_str += (sep_days[i] + ", ")
print(day_str)
day_str += " and " + sep_days[len(sep_days) - 2]
print(day_str)
return day_str
def cancel_recurring_request(netid):
update_user_sql = ''' UPDATE users
SET recur = FALSE, recur_begintime = NULL, recur_endtime = NULL, days = NULL, location = NULL
WHERE netid = '{}' '''.format(netid)
cur, conn = new_connection()
cur.execute(update_user_sql)
print('the update cancel rr has executed')
close_connection(cur, conn)
def get_current_weekday_char():
now = datetime.now().replace(second=0, microsecond=0)
ISOWEEKDAY_NUM_TO_DAY_CHAR = {1: 'M', 2: 'T',
3: 'W', 4: 'R', 5: 'F', 6: 'S', 7: 'U'}
return ISOWEEKDAY_NUM_TO_DAY_CHAR[now.isoweekday()]
def recur_request_to_string(recur_req_dict):
if recur_req_dict == None:
return None
else:
# change to normal req for easier field access
req = recur_request_to_normal_request(recur_req_dict)
if req['meal_type']:
meal = 'Lunch'
else:
meal = 'Dinner'
dhalls_in_req = [dhall_list[i]
for i in range(len(dhall_list)) if req['dhall_arr'][i]]
# append dining halls into a string split by /
loc = '/'.join(dhalls_in_req)
day_string = recurring_meal_string_to_days(req['days'])
ret_string = meal + ' @ ' + loc + " from " + \
req['starttime'].strftime('%-I:%M%p') + "-" + req['endtime'].strftime('%-I:%M%p') \
+ ', on ' + day_string
return ret_string