-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.py
420 lines (341 loc) · 12.4 KB
/
scheduler.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import uuid
import os
import binascii
import string
import pprint
from flask import Flask
from flask import request
from flask_cors import CORS
import base64
import pymongo
import json
from bson.objectid import ObjectId
import urllib
import logging
fmt = '%(asctime)s %(name)s.%(funcName)s +%(lineno)s: ' + \
'%(levelname)-8s [%(process)d] %(message)s'
logging.basicConfig(
level=logging.DEBUG,
#format=fmt,
)
app = Flask(__name__)
CORS(app)
def randomString():
x = binascii.hexlify(os.urandom(16)).decode('utf8')
return x
class DB():
def __init__(self):
self.client = pymongo.MongoClient()
self.db = self.client.scheduler
self.users = self.db.users
self.schedules = self.db.schedules
self.avails = self.db.avails
logging.info("DB: db initialized")
def newUser(self):
user = {
'userid' : randomString(),
'realName' : 'DEFAULT_USER_NAME',
'schedules' : [],
}
userDbId = self.users.insert_one(user).inserted_id
newuser = self.users.find_one({'userid' : user['userid']})
logging.info("DB: newUser %s" % pprint.pprint(newuser))
return newuser['userid']
def getUser(self, userid):
tmp = {'userid' : userid}
faf = self.users.find_one(tmp)
return faf
def userExists(self, userid):
u = self.getUser(userid)
logging.info("DB: userExists() %s" % (u))
if u:
return { 'valid' : True, 'realName': u['realName'] };
else:
return { 'valid' : False, 'realName': 'dont_care' };
def updateRealName(self, userid, name):
user = {
'userid' : userid,
}
result = self.users.update_one(
{"userid" : userid},
{"$set" : { 'realName' : name } },
)
logging.info("DB: updateRealName() %s %s" % (
result.matched_count,
result.modified_count,
))
tmp = {'userid' : userid}
faf = self.users.find_one(tmp)
logging.info("DB: updateRealName() %s" % (faf))
return faf['realName']
def addNewSchedule(self, userid, sname):
schedule = {
'name' : sname,
'owner' : userid,
'schedid' : randomString(),
'avails' : [],
}
if not self.schedules.insert_one(schedule):
return False
result = self.users.update_one(
{"userid" : userid},
{"$push" : { 'schedules' : { 'name' : schedule['name'], 'schedid' : schedule['schedid'] } } },
)
logging.info("DB: addNewSchedule() %s %s" % (
result.matched_count,
result.modified_count,
))
if result.matched_count and result.modified_count:
return True
return False
def addExistingSchedule(self, userid, schedid):
schedule = {
'schedid' : schedid,
}
logging.info("DB: addExistingSchedule() schedule{} = %s" % (schedule,))
result = self.schedules.find_one(schedule)
logging.info("DB: addExistingSchedule() result = %s" % (result,))
if result:
result2 = self.users.update_one(
{"userid" : userid},
{"$push" : { 'schedules' : { 'name' : result['name'], 'schedid' : result['schedid'] } } },
)
logging.info("DB: addExistingSchedule() %s %s" % (
result2.matched_count,
result2.modified_count,
))
if result2.matched_count and result2.modified_count:
return True
return False
def rmSchedule(self, userid, schedid):
# try to rm assuming you're the owner
schedule = {
'schedid' : schedid,
'owner' : userid,
}
logging.info("DB: rmSchedule() %s" % (schedule))
result0 = self.schedules.delete_one(schedule)
logging.info("DB: rmSchedule() %s" % (
result0.deleted_count,
))
# then remove from your own schedule list
result1 = self.users.update_one(
{"userid" : userid},
{"$pull" : { 'schedules' : { 'schedid' : schedid } } },
)
logging.info("DB: rmSchedule() %s %s" % (
result1.matched_count,
result1.modified_count,
))
return 'ok'
def getSchedules(self, userid):
result = self.users.find_one({'userid' : userid})
logging.info("DB: getSchedules() %s" % (result))
return result['schedules']
def getScheduleInfo(self, userid, schedid):
scheduleInfo = self.schedules.find_one({'schedid' : schedid})
logging.info("DB: getScheduleInfo() %s" % (scheduleInfo,))
if not scheduleInfo:
return False
if scheduleInfo['owner'] == userid:
scheduleInfo['isOwner'] = True
logging.info("DB: getScheduleInfo() I am the owner")
else:
scheduleInfo['isOwner'] = False
logging.info("DB: getScheduleInfo() I am a peon")
del scheduleInfo['_id'] #delete sensitive info
del scheduleInfo['owner'] #delete sensitive info
avails = self.getAvailabilities(userid, schedid);
scheduleInfo['avails'] = avails
logging.info("DB: getScheduleInfo() return = %s" % (scheduleInfo,))
return scheduleInfo
def addNewAvailability(self, userid, schedid):
avail = {
'availid' : randomString(),
'owner' : userid,
'name' : 'John Adams',
'data' : [],
}
if not self.avails.insert_one(avail).inserted_id:
return False
newAvail = self.avails.find_one({'availid' : avail['availid']})
logging.info("DB: addNewAvailability() newAvail = %s" % (newAvail,))
if not newAvail:
return False
result = self.schedules.update_one(
{"schedid" : schedid},
{"$push" : { 'avails' : avail['availid'] } },
)
logging.info("DB: addNewAvailability() update results %s %s" % (
result.matched_count,
result.modified_count,
))
if result.matched_count == 0 or result.modified_count == 0:
return False
avails = self.getAvailabilities(userid, schedid);
return avails
def getAvailabilities(self, userid, schedid):
sched = self.schedules.find_one(
{"schedid" : schedid}
)
if not sched:
return False
availData = list(self.avails.find({'availid' : { '$in' : sched['avails']} }))
for a in availData:
del a['_id']
logging.info("getAvailabilities() %s" % (availData,))
return availData
def updateAvailName(self, userid, availid, name):
avail = self.avails.find_one({'availid' : availid})
logging.info("DB: updateAvailName() avail = %s" % (avail,))
if avail and avail['owner'] == userid:
result = self.avails.update_one({'availid' : availid}, { '$set': { 'name' : name} })
logging.info("DB: updateAvailName() %s %s" % (
result.matched_count,
result.modified_count,
))
if result.matched_count == 1 and result.modified_count == 1:
return True
return False
'''
remove availability
0) check db.avails and remove if you are avail owner
1) check db.schedules and remove avail if you are schedule owner
'''
def rmAvail(self, userid, schedid, availid):
result = self.avails.delete_one({
'owner' : userid,
'availid' : availid,
})
logging.info("DB: rmAvail() %s %s" % (result.deleted_count, result.acknowledged))
if result.deleted_count > 0:
result1 = self.schedules.update_one(
{"schedid" : schedid},
{"$pull" : { 'avails' : availid } },
)
logging.info("DB: rmAvail() %s %s" % (result1.matched_count, result1.modified_count,))
return True
return False
def saveAvail(self, userid, availid, data):
logging.info("DB: saveAvail() %s %s %s" % (userid, availid, data))
res = self.avails.update_one(
{
"availid" : availid,
"owner" : userid,
},
{
"$set" : { 'data' : data }
},
)
logging.info("DB: saveAvail() %s %s" % (res.matched_count, res.modified_count,))
if res.matched_count == 1 and res.modified_count == 1:
return True
return False
class User():
db = DB()
def __init__(self,userid=None):
if userid == None:
self.userid = User.db.newUser()
self.schedules = []
else:
self.userid = userid
def isValid(self):
return User.db.userExists(self.userid)
def addNewSchedule(self, sname):
return User.db.addNewSchedule(self.userid, sname)
def addNewAvailability(self, schedid):
return User.db.addNewAvailability(self.userid, schedid)
def addExistingSchedule(self, schedid):
return User.db.addExistingSchedule(self.userid, schedid)
def rmSchedule(self, schedid):
return User.db.rmSchedule(self.userid, schedid)
def getSchedules(self):
return User.db.getSchedules(self.userid)
def getScheduleInfo(self, schedid):
return User.db.getScheduleInfo(self.userid, schedid)
def updateRealName(self, name):
return User.db.updateRealName(self.userid, name)
def updateAvailName(self, availid, name):
return User.db.updateAvailName(self.userid, availid, name)
def rmAvail(self, schedid, availid):
return User.db.rmAvail(self.userid, schedid, availid)
def saveAvail(self, availid, data):
return User.db.saveAvail(self.userid, availid, data)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/generateNewUser')
def newAcct():
newUser = User()
return newUser.userid
@app.route('/validateUser')
def validateUser():
userid = request.args.get('userid')
u = User(userid)
resp = u.isValid()
logging.info("validateUser() %s" % (resp))
return json.dumps(resp)
@app.route('/updateRealName')
def updateRealName():
userid = request.args.get('userid')
realName = request.args.get('name')
u = User(userid)
resp = u.updateRealName(realName)
logging.info("updateRealName() %s" % (resp))
return resp
@app.route('/addNewSchedule')
def addNewSchedule():
name = request.args.get('name')
userid = request.args.get('userid')
u = User(userid)
return str(u.addNewSchedule(name))
@app.route('/getSchedules')
def getSchedules():
userid = request.args.get('userid')
u = User(userid)
return json.dumps(u.getSchedules())
@app.route('/rmSchedule')
def rmSchedule():
userid = request.args.get('userid')
schedid = request.args.get('schedid')
u = User(userid)
return u.rmSchedule(schedid)
@app.route('/rmAvail')
def rmAvail():
userid = request.args.get('userid')
schedid = request.args.get('schedid')
availid = request.args.get('availid')
u = User(userid)
return json.dumps(u.rmAvail(schedid, availid))
@app.route('/saveAvail')
def saveAvail():
userid = request.args.get('userid')
data = json.loads(urllib.parse.unquote(request.args.get('data')))
availid = request.args.get('availid')
u = User(userid)
return json.dumps(u.saveAvail(availid, data))
@app.route('/addExistingSchedule')
def addExistingSchedule():
userid = request.args.get('userid')
schedid = request.args.get('schedid')
u = User(userid)
return str(u.addExistingSchedule(schedid))
@app.route('/getScheduleInfo')
def getScheduleInfo():
userid = request.args.get('userid')
schedid = request.args.get('schedid')
u = User(userid)
return json.dumps(u.getScheduleInfo(schedid))
@app.route('/addNewAvailability')
def addNewAvailability():
userid = request.args.get('userid')
schedid = request.args.get('schedid')
u = User(userid)
return json.dumps(u.addNewAvailability(schedid))
@app.route('/updateAvailName')
def updateAvailName():
userid = request.args.get('userid')
availid = request.args.get('availid')
name = request.args.get('name')
u = User(userid)
return json.dumps(u.updateAvailName(availid, name))