def patch(self, username, list_id: str): body_parser = reqparse.RequestParser(bundle_errors=True) body_parser.add_argument('name', type=str, required=True, help="Missing the name of the list") args = body_parser.parse_args( strict=False ) # Accepted only if these two parameters are strictly declared in body else raise exception if len(list_id) != 24: return sendErrorNotFound({"message": "todo_list id not found"}) try: todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) if 'todo_list' in args: todo_list.update( name=args['name'], todo_list=args['todo_list'], ) else: todo_list.update(name=args['name'], ) todo_list = TodoList.objects(id=list_id).first() return sendSuccess({'todo_list': todo_list.asJson()}) except Exception as err: return sendJson(400, str(err), args)
def get(self, username): try: liste = list( map(lambda todoList: todoList.asJson(), TodoList.objects(username=username))) return sendSuccess(liste) except Exception as e: raise (e)
def get_user_todo_list_by_id(todo_list_id): """ Create an user in the database :param todo_list_id: String, To do list id :return: List, user to do list. """ try: return TodoList.objects(id=todo_list_id).first() except Exception: return None
def get(self, username, list_id: str): if len(list_id) != 24: return sendErrorNotFound({"message": "todo_list id not found"}) try: todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) return sendSuccess({'todo_list': todo_list.asJson()}) except Exception as err: return sendJson(400, str(err), {"list_id": list_id})
def put(self, username, list_id: str): body_parser = reqparse.RequestParser(bundle_errors=True) body_parser.add_argument('name', type=str, required=True, help="Missing the name of the list") body_parser.add_argument('description', type=str, required=True, help="Missing the description of the list") args = body_parser.parse_args( strict=False ) # Accepted only if these two parameters are strictly declared in body else raise exception if len(list_id) != 24: return sendErrorNotFound( {"message": "todo_list id not found (can't be alive)"}) try: todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) todo = Todo(name=args['name'], description=args['description'], username=username, created_on=datetime.today()).save() todo_list.update(push__todo_list=todo, username=username) updated_todo_list = TodoList.objects(id=list_id, username=username).first() return sendSuccess({ "todo": todo.asJson(), "updated_todo_list": updated_todo_list.asJson() }) except Exception as err: return sendJson(400, str(err), {"list_id": list_id})
def get_user_todo_list(user_id): """ Create an user in the database :param user_id: String, user id :return: List, user to do list. """ try: list = [] todo_list = TodoList.objects(user_id=user_id) for i in todo_list: list.append({ 'id': str(i.id), 'description': i.description, 'status': i.status, }) return list except Exception: return []
def delete(self, username, list_id: str, todo_id: str): if len(list_id) != 24: return sendErrorNotFound({"message": "todo_list id not found"}) if len(todo_id) != 24: return sendErrorNotFound({"message": "todo_id id not found"}) try: todo = Todo.objects(id=todo_id, username=username).first() if todo is None: return sendErrorNotFound({"message": "todo id not found"}) todo_list = TodoList.objects(id=list_id, username=username).first() if todo_list is None: return sendErrorNotFound({"message": "todo_list id not found"}) todo.delete() todo_list.update(pull__todo_list=todo.id, username=username) return sendSuccess({'todo_id': todo_id, "list_id": list_id}) except Exception as err: return sendJson(400, str(err), {"list_id": list_id})
def update_user_todo_list(todo_list_id, status): todo_list = TodoList.objects(id=todo_list_id).first() todo_list.status = status todo_list.save()