def post_task(event, user): try: list_id = event.get("listId", None) date_time_str = event.get("dateTime", None) date_time_obj = None if date_time_str: date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S') title = event.get("title", None) if list_id is None or list_id == '': raise Exception('Missing field listId') _list = session.query(List).filter_by(id=list_id).filter_by( user=user['email']).first() if _list is None: raise Exception('List does not exist') if title is None or title == '': raise Exception('Task title cannot be empty') new_task = Task(title=title, listId=list_id, datetime=date_time_obj) session.add(new_task) session.commit() check_and_update_list_status(list_id) response = copy.deepcopy(constants.SUCCESS_RESPONSE) response['data'] = [] return response except Exception as e: response = copy.deepcopy(constants.FAIL_RESPONSE) response['message'] = str(e) return response
def check_and_update_list_status(list_id): list = session.query(List).filter_by(id=list_id).first() tasks = session.query(Task).filter_by(listId=list_id).all() for task in tasks: if not task.completed: list.completed = False session.commit() return list.completed = True session.commit()
def delete_list(list_id, user): try: _list = session.query(List).filter_by(id = list_id).filter_by(user = user['email']).first() if _list is None: raise Exception('List does not exist') res = session.query(List).filter_by(id = list_id).delete() session.commit() response = copy.deepcopy(constants.SUCCESS_RESPONSE) response['data'] = _list.as_dict() return response except Exception as e: response = copy.deepcopy(constants.FAIL_RESPONSE) response['message'] = str(e) return response
def get_all_lists(user): try: session.commit() # lists = session.query(List).all() lists = session.query(List).filter_by(user=user['email']).all() response = copy.deepcopy(constants.SUCCESS_RESPONSE) response['data'] = [] for list in lists: response['data'].append(list.as_dict()) return response except Exception as e: response = constants.FAIL_RESPONSE response['message'] = str(e) return response
def post_list(event, user): try: title = event.get('title', None) user = user['email'] if title is None or title == '': raise Exception("Title cannot be Null") new_list = List(title=title, user=user) session.add(new_list) session.commit() response = copy.deepcopy(constants.SUCCESS_RESPONSE) response['data'] = None return response except Exception as e: response = copy.deepcopy(constants.FAIL_RESPONSE) response['message'] = str(e) return response
def get_list(list_id, user): try: _list = session.query(List).filter_by(id=list_id).filter_by( user=user['email']).first() if _list is None: raise Exception('List does not exist') session.commit() tasks = session.query(Task).filter_by(listId=list_id).all() response = copy.deepcopy(constants.SUCCESS_RESPONSE) response['data'] = [] for task in tasks: response['data'].append(task.as_dict()) return response except Exception as e: response = copy.deepcopy(constants.FAIL_RESPONSE) response['message'] = str(e) return response
def delete_task(task_id, user): try: task = session.query(Task).filter_by(id = task_id).first() if task is None: raise Exception('Task does not exist') if task: _list = session.query(List).filter_by(id = task.listId).first() if not _list.user == user['email']: raise Exception('Task does not exist') res = session.query(Task).filter_by(id = task_id).delete() session.commit() check_and_update_list_status(task.listId) response = copy.deepcopy(constants.SUCCESS_RESPONSE) response['data'] = task.as_dict() return response except Exception as e: response = copy.deepcopy(constants.FAIL_RESPONSE) response['message'] = str(e) return response
def put_list(list_id, user): try: list = session.query(List).filter_by(id=list_id).filter_by( user=user['email']).first() if list is None: raise Exception('List does not exist') if list.completed: list.completed = False else: list.completed = True session.commit() update_tasks_status(list_id, list.completed) response = copy.deepcopy(constants.SUCCESS_RESPONSE) response['data'] = get_all_lists(user)['data'] return response except Exception as e: response = copy.deepcopy(constants.FAIL_RESPONSE) response['message'] = str(e) return response
def update_tasks_status(list_id, status): tasks = session.query(Task).filter_by(listId=list_id).all() for task in tasks: task.completed = status session.commit()