示例#1
0
async def update_current_user(
    user_update: UserInUpdate = Body(..., embed=True, alias="user"),
    current_user: User = Depends(get_current_user_authorizer()),
    users_repo: UsersRepository = Depends(get_repository(UsersRepository)),
) -> UserInResponse:
    if user_update.name and user_update.name != current_user.name:
        if await check_username_is_taken(users_repo, user_update.name):
            raise HTTPException(
                status_code=HTTP_400_BAD_REQUEST,
                detail=strings.USERNAME_TAKEN,
            )

    if user_update.email and user_update.email != current_user.email:
        if await check_email_is_taken(users_repo, user_update.email):
            raise HTTPException(
                status_code=HTTP_400_BAD_REQUEST,
                detail=strings.EMAIL_TAKEN,
            )

    user = await users_repo.update_user(user=current_user,
                                        **user_update.dict())

    token = jwt.create_access_token_for_user(user, str(config.SECRET_KEY))
    return UserInResponse(user=UserWithToken(
        name=user.name,
        email=user.email,
        bio=user.bio,
        image=user.image,
        token=token,
    ), )
示例#2
0
async def update_user(current_user: User, user_update: UserInUpdate) -> User:
    await current_user.update_from_dict(user_update.dict())
    return current_user