11import uuid
22from typing import Any
33
4- from fastapi import APIRouter , Depends , HTTPException
4+ from fastapi import APIRouter , Depends , HTTPException , status
55from sqlmodel import col , delete , func , select
66
77from app .auth .dependencies import (
@@ -60,7 +60,7 @@ def create_user(*, session: SessionDep, user_in: UserCreate) -> Any:
6060 user = user_service .get_user_by_email (session = session , email = user_in .email )
6161 if user :
6262 raise HTTPException (
63- status_code = 400 ,
63+ status_code = status . HTTP_400_BAD_REQUEST ,
6464 detail = "The user with this email already exists in the system." ,
6565 )
6666
@@ -91,7 +91,8 @@ def update_user_me(
9191 )
9292 if existing_user and existing_user .id != current_user .id :
9393 raise HTTPException (
94- status_code = 409 , detail = "User with this email already exists"
94+ status_code = status .HTTP_409_CONFLICT ,
95+ detail = "User with this email already exists" ,
9596 )
9697 user_data = user_in .model_dump (exclude_unset = True )
9798 current_user .sqlmodel_update (user_data )
@@ -110,10 +111,13 @@ def update_password_me(
110111 """
111112 verified , _ = verify_password (body .current_password , current_user .hashed_password )
112113 if not verified :
113- raise HTTPException (status_code = 400 , detail = "Incorrect password" )
114+ raise HTTPException (
115+ status_code = status .HTTP_400_BAD_REQUEST , detail = "Incorrect password"
116+ )
114117 if body .current_password == body .new_password :
115118 raise HTTPException (
116- status_code = 400 , detail = "New password cannot be the same as the current one"
119+ status_code = status .HTTP_400_BAD_REQUEST ,
120+ detail = "New password cannot be the same as the current one" ,
117121 )
118122 hashed_password = get_password_hash (body .new_password )
119123 current_user .hashed_password = hashed_password
@@ -137,7 +141,8 @@ def delete_user_me(session: SessionDep, current_user: CurrentUser) -> Any:
137141 """
138142 if current_user .is_superuser :
139143 raise HTTPException (
140- status_code = 403 , detail = "Super users are not allowed to delete themselves"
144+ status_code = status .HTTP_403_FORBIDDEN ,
145+ detail = "Super users are not allowed to delete themselves" ,
141146 )
142147 session .delete (current_user )
143148 session .commit ()
@@ -152,7 +157,7 @@ def register_user(session: SessionDep, user_in: UserRegister) -> Any:
152157 user = user_service .get_user_by_email (session = session , email = user_in .email )
153158 if user :
154159 raise HTTPException (
155- status_code = 400 ,
160+ status_code = status . HTTP_400_BAD_REQUEST ,
156161 detail = "The user with this email already exists in the system" ,
157162 )
158163 user_create = UserCreate .model_validate (user_in )
@@ -172,11 +177,13 @@ def read_user_by_id(
172177 return user
173178 if not current_user .is_superuser :
174179 raise HTTPException (
175- status_code = 403 ,
180+ status_code = status . HTTP_403_FORBIDDEN ,
176181 detail = "The user doesn't have enough privileges" ,
177182 )
178183 if user is None :
179- raise HTTPException (status_code = 404 , detail = "User not found" )
184+ raise HTTPException (
185+ status_code = status .HTTP_404_NOT_FOUND , detail = "User not found"
186+ )
180187 return user
181188
182189
@@ -198,7 +205,7 @@ def update_user(
198205 db_user = session .get (User , user_id )
199206 if not db_user :
200207 raise HTTPException (
201- status_code = 404 ,
208+ status_code = status . HTTP_404_NOT_FOUND ,
202209 detail = "The user with this id does not exist in the system" ,
203210 )
204211 if user_in .email :
@@ -207,7 +214,8 @@ def update_user(
207214 )
208215 if existing_user and existing_user .id != user_id :
209216 raise HTTPException (
210- status_code = 409 , detail = "User with this email already exists"
217+ status_code = status .HTTP_409_CONFLICT ,
218+ detail = "User with this email already exists" ,
211219 )
212220
213221 db_user = user_service .update_user (
@@ -225,10 +233,13 @@ def delete_user(
225233 """
226234 user = session .get (User , user_id )
227235 if not user :
228- raise HTTPException (status_code = 404 , detail = "User not found" )
236+ raise HTTPException (
237+ status_code = status .HTTP_404_NOT_FOUND , detail = "User not found"
238+ )
229239 if user == current_user :
230240 raise HTTPException (
231- status_code = 403 , detail = "Super users are not allowed to delete themselves"
241+ status_code = status .HTTP_403_FORBIDDEN ,
242+ detail = "Super users are not allowed to delete themselves" ,
232243 )
233244 statement = delete (Item ).where (col (Item .owner_id ) == user_id )
234245 session .exec (statement )
0 commit comments