Skip to content

Commit 5078d2d

Browse files
committed
Type checking error resolved
1 parent 1ff6949 commit 5078d2d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/app/api/v1/users.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ async def read_user(request: Request, username: str, db: Annotated[AsyncSession,
7272
return cast(UserRead, db_user)
7373

7474

75+
# In src/app/api/v1/users.py, replace the patch_user function with this:
76+
7577
@router.patch("/user/{username}")
7678
async def patch_user(
7779
request: Request,
@@ -80,25 +82,32 @@ async def patch_user(
8082
current_user: Annotated[dict, Depends(get_current_user)],
8183
db: Annotated[AsyncSession, Depends(async_get_db)],
8284
) -> dict[str, str]:
83-
# Remove schema_to_select to get a dictionary
8485
db_user = await crud_users.get(db=db, username=username)
8586
if db_user is None:
8687
raise NotFoundException("User not found")
8788

88-
# Now both are dictionaries, use dictionary access
89-
if db_user["username"] != current_user["username"]:
90-
raise ForbiddenException()
89+
# Handle both dict and UserRead object types
90+
if isinstance(db_user, dict):
91+
db_username = db_user["username"]
92+
db_email = db_user["email"]
93+
else:
94+
db_username = db_user.username
95+
db_email = db_user.email
9196

92-
if values.username != db_user["username"]:
93-
existing_username = await crud_users.exists(db=db, username=values.username)
94-
if existing_username:
95-
raise DuplicateValueException("Username not available")
97+
if db_username != current_user["username"]:
98+
raise ForbiddenException()
9699

97-
if values.email != db_user["email"]:
98-
existing_email = await crud_users.exists(db=db, email=values.email)
99-
if existing_email:
100+
# Check for email conflicts if email is being updated
101+
if values.email is not None and values.email != db_email:
102+
if await crud_users.exists(db=db, email=values.email):
100103
raise DuplicateValueException("Email is already registered")
101104

105+
# Check for username conflicts if username is being updated
106+
if values.username is not None and values.username != db_username:
107+
if await crud_users.exists(db=db, username=values.username):
108+
raise DuplicateValueException("Username not available")
109+
110+
# Update the user
102111
await crud_users.update(db=db, object=values, username=username)
103112
return {"message": "User updated"}
104113

0 commit comments

Comments
 (0)