def putTimers(timerId): """This function is for the server to update timers""" data = request.get_json() postAttrs = ['id', 'userId', 'title', 'startTime', 'duration', 'breakTime', 'round', 'description', 'zoomLink', 'isCreator', 'timerToUserId', 'added'] code, msg, result = 0, "", {"data": None} if not judgeKeysCorrect(data, postAttrs): code, msg = 400, apiStatus.getResponseMsg(400) else: if not judgeInputValid(data) : code, msg = 400, apiStatus.getResponseMsg(400) result["code"] = code result["message"] = msg return jsonify(result) targetTimer = Timer.query.get(timerId) if not targetTimer: code, msg = 404, apiStatus.getResponseMsg(404) else: try: targetTimer.update(data) db.session.commit() result["data"] = targetTimer.toDict({ "added": data["added"] if "added" in data else True, "isCreator": data["isCreator"] if "isCreator" in data else True, "timerToUserId": data["timerToUserId"] if "timerToUserId" in data else targetTimer.userId, }) code, msg = 201, apiStatus.getResponseMsg(201) except: code, msg = 500, apiStatus.getResponseMsg(500) result["code"] = code result["message"] = msg return jsonify(result)
def putTaskList(taskListId): """API for updating a task list with id as taskListId from request body""" data = request.get_json(force=True) postAttrs = ['userId', 'name'] code, msg, result = 0, '', {"data": None} if not judgeIntValid(taskListId): code, msg = 400, apiStatus.getResponseMsg(400) result["code"] = code result["message"] = msg return jsonify(result) if not judgeInputValid(data): code, msg = 400, apiStatus.getResponseMsg(400) result["code"] = code result["message"] = msg return jsonify(result) if not judgeKeysCorrect(data, postAttrs): code, msg = 400, apiStatus.getResponseMsg(400) else: targetTaskList = TaskList.query.get(taskListId) if not targetTaskList: code, msg = 404, apiStatus.getResponseMsg(404) else: try: targetTaskList.update(data) db.session.commit() result["data"] = targetTaskList.toDict() code, msg = 201, apiStatus.getResponseMsg(201) except: code, msg = 500, apiStatus.getResponseMsg(500) result["code"] = code result["message"] = msg return jsonify(result)
def createTaskList(): """API for creating a new task list from request body""" data = request.get_json(force=True) postAttrs = ['userId', 'name'] code, msg, result = 0, '', {"data": None} if not judgeInputValid(data): code, msg = 400, apiStatus.getResponseMsg(400) result["code"] = code result["message"] = msg return jsonify(result) elif not judgeKeysCorrect(data, postAttrs): code, msg = 400, apiStatus.getResponseMsg(400) else: try: userId = data['userId'] name = data['name'] newTaskList = TaskList(userId=userId, name=name) db.session.add(newTaskList) db.session.commit() result['data'] = newTaskList.toDict() code, msg = 201, apiStatus.getResponseMsg(201) except: code, msg = 500, apiStatus.getResponseMsg(500) result["code"] = code result["message"] = msg return jsonify(result)
def createTaskTimer(): """This function is used to create a new relation""" data = request.get_json() if not judgeInputValid(data): code, msg = 400, apiStatus.getResponseMsg(400) return jsonify({"code": code, "message": msg, "data": None}) postAttrs = ['taskId', 'timerId', 'userId'] code, msg, result = 0, "", {"data": None} if not judgeKeysExist(data, postAttrs): code, msg = 400, apiStatus.getResponseMsg(400) else: targetTask = Task.query.get(data['taskId']) # query by primary key targetTimer = Timer.query.get(data['timerId']) specifiedId = data['id'] if 'id' in data else None if not targetTask or not targetTimer: code, msg = 404, apiStatus.getResponseMsg(404) else: try: newTaskToTimer = TaskToTimer(taskId=str(targetTask.id), timerId=str(targetTimer.id), userId=str(data['userId'])) if specifiedId: newTaskToTimer.update({"id": specifiedId}) db.session.add(newTaskToTimer) db.session.commit() result["data"] = newTaskToTimer.toDict() code, msg = 201, apiStatus.getResponseMsg(201) except: # add repeat relations may cause internal error code, msg = 500, apiStatus.getResponseMsg(500) result["code"] = code result["message"] = msg return jsonify(result)
def testJudgeInputCompositeInvalidString(self): """test the utility function to judge whether int is too small""" data = { 'key': None, 'key1': 1, 'key2': 65535, 'key3': -1000, 'key4': 'a' * 141 } result = judgeInputValid(data) self.assertEqual(result, False)
def handleQueryTasksOrTimers(): """handle request to retrieve lists of tasks or timers""" taskId = request.args.get('taskId', None) timerId = request.args.get('timerId', None) userId = request.args.get('userId', None) # for verification # judge input if not judgeInputValid({"userId": userId}) or \ (taskId and not judgeIntValid(taskId)) or \ (timerId and not judgeIntValid(timerId)): code, msg = 400, apiStatus.getResponseMsg(400) return jsonify({"code": code, "message": msg, "data": None}) if (taskId and timerId) or not userId: code, msg = 500, apiStatus.getResponseMsg(500) return jsonify({"code": code, "message": msg, "data":[]}) code, msg, resultData = 0, "", [] result = {} if not timerId and not taskId and userId: # retrieve by user result['data'] = [] # should also return an empty list targetRels = TaskToTimer.query.filter_by(userId=userId).all() if not targetRels: code, msg = 404, apiStatus.getResponseMsg(404) else: for rel in targetRels: result['data'].append(rel.toDict()) code, msg = 200, apiStatus.getResponseMsg(200) elif timerId: targetTimer = Timer.query.get(timerId) if not targetTimer: code, msg = 404, apiStatus.getResponseMsg(404) # elif targetTimer.userId != userId: # code, msg = 401, apiStatus.getResponseMsg(401) else: code, msg, resultData = getTasksByTimerid(int(timerId)) elif taskId: targetTask = Task.query.get(taskId) if not targetTask: code, msg = 404, apiStatus.getResponseMsg(404) elif targetTask.userId !=userId: code, msg = 401, apiStatus.getResponseMsg(401) else: code, msg, resultData = getTimersByTaskid(int(taskId)) result["code"] = code result["message"] = msg result["data"] = resultData return jsonify(result)
def getTaskListsByUserId(): """API for getting all tasklists with user id as userId""" userId = request.args.get('userId', None) code, msg, result = 0, '', {"data": None} if not judgeInputValid({"userId":userId}): code, msg = 400, apiStatus.getResponseMsg(400) result["code"] = code result["message"] = msg return jsonify(result) result["data"] = [] taskLists = TaskList.query.filter_by(userId=userId).all() if not taskLists: code, msg = 404, apiStatus.getResponseMsg(404) else: for taskList in taskLists: result["data"].append(taskList.toDict()) code, msg = 200, apiStatus.getResponseMsg(200) result["code"] = code result["message"] = msg return jsonify(result)
def testJudgeInputValidIntegerBig(self): """test the utility function to judge whether int is too small""" data = {'key': 65537} result = judgeInputValid(data) self.assertEqual(result, False)
def testJudgeInputValidString(self): """test the utility function to judge whether input is valid""" data = {'key': 'testString'} result = judgeInputValid(data) self.assertEqual(result, True)
def testJudgeInputEmpty(self): """test the utility function to judge whether int is too small""" data = '' result = judgeInputValid(data) self.assertEqual(result, False)
def testJudgeInputNone(self): """test the utility function to judge whether int is too small""" data = {'key': None} result = judgeInputValid(data) self.assertEqual(result, True)
def testJudgeInputValidStringOver(self): """test the utility function to judge whether string is too long""" data = {'key': 'a' * 141} result = judgeInputValid(data) self.assertEqual(result, False)
def createTimers(): """This function is for the server to create new timers""" data = request.get_json() postAttrs = ['userId', 'title', 'startTime', 'duration', 'breakTime', 'round'] code, msg, result = 0, "", {"data": None} if not judgeKeysExist(data, postAttrs): code, msg = 400, apiStatus.getResponseMsg(400) else: if not judgeInputValid(data) : code, msg = 400, apiStatus.getResponseMsg(400) result["code"] = code result["message"] = msg return jsonify(result) userId = data['userId'] title = data['title'] description = data['description'] if 'description' in data else None zoomLink = data['zoomLink'] if 'zoomLink' in data else None startTime = data['startTime'] # formatStartTime = parser.parse(startTime) # testFormStartTime = datetime.datetime.fromisoformat(startTime) # print(formatStartTime, testFormStartTime) duration = int(data['duration']) breakTime = int(data['breakTime']) round = int(data['round']) oldTimers = Timer.query.filter_by(userId=userId).all() if oldTimers is not None: for oldTimer in oldTimers: totalDuration = (oldTimer.duration + oldTimer.breakTime) * oldTimer.round totalDuration = datetime.timedelta(minutes=totalDuration) endTime = parser.parse(oldTimer.startTime) + totalDuration # sTime = datetime.datetime.strptime(startTime, "%Y-%m-%d %H:%M:%S") sTime = parser.parse(startTime) newDuration = (duration + breakTime) * round newDuration = datetime.timedelta(minutes=newDuration) eTime = sTime + newDuration if parser.parse(oldTimer.startTime) <= sTime < endTime or parser.parse(oldTimer.startTime) < eTime <= endTime: code, msg = 403, apiStatus.getResponseMsg(403) result["code"] = code result["message"] = msg return jsonify(result) if sTime < parser.parse(oldTimer.startTime) and eTime > endTime: code, msg = 403, apiStatus.getResponseMsg(403) result["code"] = code result["message"] = msg return jsonify(result) try: newTimer = Timer(userId=str(userId), title=str(title), description=str(description), zoomLink=str(zoomLink), startTime=startTime, duration=str(duration), breakTime=str(breakTime), round=str(round)) db.session.add(newTimer) newTimerTwo = Timer.query.filter_by(userId=userId).all() # print(newTimerTwo) newTimerToUser = TimerToUser(timerId=newTimer.id, userId=userId, status=1) db.session.add(newTimerToUser) db.session.commit() result["data"] = newTimer.toDict({ "added": True, "isCreator": True, "timerToUserId": userId, }) # result["data"]["startTime"] = startTime # remain to be string for the frontend consistent, or change to utcstring code, msg = 201, apiStatus.getResponseMsg(201) except: code, msg = 500, apiStatus.getResponseMsg(500) result["code"] = code result["message"] = msg return jsonify(result)