Skip to content
This repository was archived by the owner on Nov 14, 2022. It is now read-only.

Commit 4e5399d

Browse files
committed
🐛 Fix order of path operations, it matters
1 parent aa74e3d commit 4e5399d

1 file changed

Lines changed: 45 additions & 45 deletions

File tree

  • {{cookiecutter.project_slug}}/backend/app/app/api/api_v1/endpoints

{{cookiecutter.project_slug}}/backend/app/app/api/api_v1/endpoints/user.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,6 @@ def route_users_post(
9292
return user
9393

9494

95-
@router.put("/users/{username}", tags=["users"], response_model=User)
96-
def route_users_put(
97-
*,
98-
username: str,
99-
user_in: UserInUpdate,
100-
current_user: UserInDB = Depends(get_current_user),
101-
):
102-
"""
103-
Update a user
104-
"""
105-
if not check_if_user_is_active(current_user):
106-
raise HTTPException(status_code=400, detail="Inactive user")
107-
elif not check_if_user_is_superuser(current_user):
108-
raise HTTPException(
109-
status_code=400, detail="The user doesn't have enough privileges"
110-
)
111-
bucket = get_default_bucket()
112-
user = get_user(bucket, username)
113-
114-
if not user:
115-
raise HTTPException(
116-
status_code=404,
117-
detail="The user with this username does not exist in the system",
118-
)
119-
user = update_user(bucket, user_in)
120-
return user
121-
122-
12395
@router.put("/users/me", tags=["users"], response_model=User)
12496
def route_users_me_put(
12597
*,
@@ -155,6 +127,36 @@ def route_users_me_get(current_user: UserInDB = Depends(get_current_user)):
155127
return current_user
156128

157129

130+
@router.post("/users/open", tags=["users"], response_model=User)
131+
def route_users_post_open(
132+
*,
133+
username: str = Body(...),
134+
password: str = Body(...),
135+
email: EmailStr = Body(None),
136+
full_name: str = Body(None),
137+
):
138+
"""
139+
Create new user without the need to be logged in
140+
"""
141+
if not config.USERS_OPEN_REGISTRATION:
142+
raise HTTPException(
143+
status_code=403,
144+
detail="Open user resgistration is forbidden on this server",
145+
)
146+
bucket = get_default_bucket()
147+
user = get_user(bucket, username)
148+
if user:
149+
raise HTTPException(
150+
status_code=400,
151+
detail="The user with this username already exists in the system",
152+
)
153+
user_in = UserInCreate(
154+
username=username, password=password, email=email, full_name=full_name
155+
)
156+
user = upsert_user(bucket, user_in, persist_to=1)
157+
return user
158+
159+
158160
@router.get("/users/{username}", tags=["users"], response_model=User)
159161
def route_users_id_get(
160162
username: str, current_user: UserInDB = Depends(get_current_user)
@@ -175,31 +177,29 @@ def route_users_id_get(
175177
return user
176178

177179

178-
@router.post("/users/open", tags=["users"], response_model=User)
179-
def route_users_post_open(
180+
@router.put("/users/{username}", tags=["users"], response_model=User)
181+
def route_users_put(
180182
*,
181-
username: str = Body(...),
182-
password: str = Body(...),
183-
email: EmailStr = Body(None),
184-
full_name: str = Body(None),
183+
username: str,
184+
user_in: UserInUpdate,
185+
current_user: UserInDB = Depends(get_current_user),
185186
):
186187
"""
187-
Create new user without the need to be logged in
188+
Update a user
188189
"""
189-
if not config.USERS_OPEN_REGISTRATION:
190+
if not check_if_user_is_active(current_user):
191+
raise HTTPException(status_code=400, detail="Inactive user")
192+
elif not check_if_user_is_superuser(current_user):
190193
raise HTTPException(
191-
status_code=403,
192-
detail="Open user resgistration is forbidden on this server",
194+
status_code=400, detail="The user doesn't have enough privileges"
193195
)
194196
bucket = get_default_bucket()
195197
user = get_user(bucket, username)
196-
if user:
198+
199+
if not user:
197200
raise HTTPException(
198-
status_code=400,
199-
detail="The user with this username already exists in the system",
201+
status_code=404,
202+
detail="The user with this username does not exist in the system",
200203
)
201-
user_in = UserInCreate(
202-
username=username, password=password, email=email, full_name=full_name
203-
)
204-
user = upsert_user(bucket, user_in, persist_to=1)
204+
user = update_user(bucket, user_in)
205205
return user

0 commit comments

Comments
 (0)