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 post(self): body_parser = reqparse.RequestParser(bundle_errors=True) body_parser.add_argument('username', type=str, required=True, help="Missing the login of the user") body_parser.add_argument('password', type=str, required=True, help="Missing the password associated to the user login") args = body_parser.parse_args(strict=True) # Accepted only if these two parameters are strictly declared in body else raise exception try: hash = User.generate_hash(args['password']) user = User(username=args['username'],password=hash).save() return sendSuccess({'user' : user.asJson()}) except Exception as err: return sendJson(400,str(err),args)
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 post(self): body_parser = reqparse.RequestParser(bundle_errors=True) body_parser.add_argument('username', type=str, required=True, help="Missing the login of the user") body_parser.add_argument( 'password', type=str, required=True, help="Missing the password associated to the user login") args = body_parser.parse_args( strict=True ) # Accepted only if these two parameters are strictly declared in body else raise exception try: user = User.objects(username=args['username']).first() if user is None: return sendJson( 404, "Le mot de passe ou le nom d'utilisateur n'est pas correct (a)", {'username': args['username']}) if not User.verify_hash(args['password'], user.password): return sendJson( 404, "Le mot de passe ou le nom d'utilisateur n'est pas correct", {'username': args['username']}) else: token = user.encode_auth_token(user.id) return sendSuccess({'user': user.asJson(), 'token': token}) except Exception as err: return sendJson(400, str(err), args)
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 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 put(self, username): 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 try: if 'todo_list' in args: todo_list = TodoList(name=args['name'], todo_list=args['todo_list'], username=username, created_on=datetime.today()).save() else: todo_list = TodoList(name=args['name'], todo_list=[], username=username, created_on=datetime.today()).save() return sendSuccess({'todo_list': todo_list.asJson()}) except Exception as err: return sendJson(400, str(err), args)