From 4e5399d402efe4f7fa7eab6b7c52c0716cb6a3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sat, 12 Jan 2019 08:11:17 +0400 Subject: [PATCH] :bug: Fix order of path operations, it matters --- .../app/app/api/api_v1/endpoints/user.py | 90 +++++++++---------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/{{cookiecutter.project_slug}}/backend/app/app/api/api_v1/endpoints/user.py b/{{cookiecutter.project_slug}}/backend/app/app/api/api_v1/endpoints/user.py index e3857cd..2af90ba 100644 --- a/{{cookiecutter.project_slug}}/backend/app/app/api/api_v1/endpoints/user.py +++ b/{{cookiecutter.project_slug}}/backend/app/app/api/api_v1/endpoints/user.py @@ -92,34 +92,6 @@ def route_users_post( return user -@router.put("/users/{username}", tags=["users"], response_model=User) -def route_users_put( - *, - username: str, - user_in: UserInUpdate, - current_user: UserInDB = Depends(get_current_user), -): - """ - Update a user - """ - if not check_if_user_is_active(current_user): - raise HTTPException(status_code=400, detail="Inactive user") - elif not check_if_user_is_superuser(current_user): - raise HTTPException( - status_code=400, detail="The user doesn't have enough privileges" - ) - bucket = get_default_bucket() - user = get_user(bucket, username) - - if not user: - raise HTTPException( - status_code=404, - detail="The user with this username does not exist in the system", - ) - user = update_user(bucket, user_in) - return user - - @router.put("/users/me", tags=["users"], response_model=User) def route_users_me_put( *, @@ -155,6 +127,36 @@ def route_users_me_get(current_user: UserInDB = Depends(get_current_user)): return current_user +@router.post("/users/open", tags=["users"], response_model=User) +def route_users_post_open( + *, + username: str = Body(...), + password: str = Body(...), + email: EmailStr = Body(None), + full_name: str = Body(None), +): + """ + Create new user without the need to be logged in + """ + if not config.USERS_OPEN_REGISTRATION: + raise HTTPException( + status_code=403, + detail="Open user resgistration is forbidden on this server", + ) + bucket = get_default_bucket() + user = get_user(bucket, username) + if user: + raise HTTPException( + status_code=400, + detail="The user with this username already exists in the system", + ) + user_in = UserInCreate( + username=username, password=password, email=email, full_name=full_name + ) + user = upsert_user(bucket, user_in, persist_to=1) + return user + + @router.get("/users/{username}", tags=["users"], response_model=User) def route_users_id_get( username: str, current_user: UserInDB = Depends(get_current_user) @@ -175,31 +177,29 @@ def route_users_id_get( return user -@router.post("/users/open", tags=["users"], response_model=User) -def route_users_post_open( +@router.put("/users/{username}", tags=["users"], response_model=User) +def route_users_put( *, - username: str = Body(...), - password: str = Body(...), - email: EmailStr = Body(None), - full_name: str = Body(None), + username: str, + user_in: UserInUpdate, + current_user: UserInDB = Depends(get_current_user), ): """ - Create new user without the need to be logged in + Update a user """ - if not config.USERS_OPEN_REGISTRATION: + if not check_if_user_is_active(current_user): + raise HTTPException(status_code=400, detail="Inactive user") + elif not check_if_user_is_superuser(current_user): raise HTTPException( - status_code=403, - detail="Open user resgistration is forbidden on this server", + status_code=400, detail="The user doesn't have enough privileges" ) bucket = get_default_bucket() user = get_user(bucket, username) - if user: + + if not user: raise HTTPException( - status_code=400, - detail="The user with this username already exists in the system", + status_code=404, + detail="The user with this username does not exist in the system", ) - user_in = UserInCreate( - username=username, password=password, email=email, full_name=full_name - ) - user = upsert_user(bucket, user_in, persist_to=1) + user = update_user(bucket, user_in) return user