-
Notifications
You must be signed in to change notification settings - Fork 264
feat: add user profiles with JWT auth and S3 uploads #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmytro-malyk-dm
wants to merge
3
commits into
mate-academy:main
Choose a base branch
from
dmytro-malyk-dm:develop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,250 @@ | ||
| from fastapi import APIRouter | ||
| from datetime import date | ||
|
|
||
| from fastapi import ( | ||
| APIRouter, | ||
| Depends, | ||
| status, | ||
| HTTPException, | ||
| UploadFile, | ||
| Form, | ||
| File | ||
| ) | ||
| from sqlalchemy import select | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from config import get_s3_storage_client | ||
| from database import get_db, UserModel, UserProfileModel, UserGroupEnum | ||
| from exceptions import S3FileUploadError | ||
| from schemas.profiles import ProfileResponseSchema | ||
| from security.dependencies import get_user | ||
| from storages import S3StorageInterface | ||
| from validation import ( | ||
| validate_name, | ||
| validate_image, | ||
| validate_gender, | ||
| validate_birth_date | ||
| ) | ||
|
|
||
| router = APIRouter() | ||
|
|
||
| # Write your code here | ||
|
|
||
| def _validate_profile_input( | ||
| first_name: str, | ||
| last_name: str, | ||
| gender: str, | ||
| date_of_birth: date, | ||
| info: str, | ||
| avatar: UploadFile | ||
| ) -> None: | ||
| """Validate all profile input fields.""" | ||
| try: | ||
| validate_name(first_name) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e) | ||
| ) | ||
|
|
||
| try: | ||
| validate_name(last_name) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e) | ||
| ) | ||
|
|
||
| if not info or not info.strip(): | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail="Info cannot be empty or consist only of spaces." | ||
| ) | ||
|
|
||
| try: | ||
| validate_gender(gender) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e) | ||
| ) | ||
|
|
||
| try: | ||
| validate_birth_date(date_of_birth) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e) | ||
| ) | ||
|
|
||
| try: | ||
| validate_image(avatar) | ||
| except ValueError as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail=str(e) | ||
| ) | ||
|
|
||
|
|
||
| def _check_authorization( | ||
| current_user: UserModel, | ||
| user_id: int | ||
| ) -> None: | ||
| """Check if user has permission to create profile.""" | ||
| is_admin = current_user.has_group(UserGroupEnum.ADMIN) | ||
| if not is_admin and current_user.id != user_id: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_403_FORBIDDEN, | ||
| detail="You don't have permission to edit this profile." | ||
| ) | ||
|
|
||
|
|
||
| async def _verify_target_user( | ||
| user_id: int, | ||
| db: AsyncSession | ||
| ) -> None: | ||
| """Verify target user exists and is active.""" | ||
| stmt = select(UserModel).where(UserModel.id == user_id) | ||
| result = await db.execute(stmt) | ||
| target_user = result.scalar_one_or_none() | ||
|
|
||
| if not target_user or not target_user.is_active: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||
| detail="User not found or not active." | ||
| ) | ||
|
|
||
|
|
||
| async def _check_existing_profile( | ||
| user_id: int, | ||
| db: AsyncSession | ||
| ) -> None: | ||
| """Check if profile already exists.""" | ||
| stmt = select(UserProfileModel).where( | ||
| UserProfileModel.user_id == user_id | ||
| ) | ||
| result = await db.execute(stmt) | ||
| existing_profile = result.scalar_one_or_none() | ||
|
|
||
| if existing_profile: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail="User already has a profile." | ||
| ) | ||
|
|
||
|
|
||
| async def _upload_avatar( | ||
| user_id: int, | ||
| avatar: UploadFile, | ||
| s3_client: S3StorageInterface | ||
| ) -> str: | ||
| """Upload avatar to S3 and return key.""" | ||
| avatar_key = f"avatars/{user_id}_avatar.jpg" | ||
|
|
||
| try: | ||
| avatar.file.seek(0) | ||
| avatar_content = avatar.file.read() | ||
| await s3_client.upload_file(avatar_key, avatar_content) | ||
| except S3FileUploadError: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| detail="Failed to upload avatar. Please try again later." | ||
| ) | ||
| except Exception: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| detail="Failed to upload avatar. Please try again later." | ||
| ) | ||
|
|
||
| return avatar_key | ||
|
|
||
|
|
||
| async def _create_profile_in_db( | ||
| user_id: int, | ||
| first_name: str, | ||
| last_name: str, | ||
| gender: str, | ||
| date_of_birth: date, | ||
| info: str, | ||
| avatar_key: str, | ||
| db: AsyncSession | ||
| ) -> UserProfileModel: | ||
| """Create profile in database.""" | ||
| new_profile = UserProfileModel( | ||
| user_id=user_id, | ||
| first_name=first_name.lower(), | ||
| last_name=last_name.lower(), | ||
| gender=gender, | ||
| date_of_birth=date_of_birth, | ||
| info=info, | ||
| avatar=avatar_key | ||
| ) | ||
|
|
||
| db.add(new_profile) | ||
|
|
||
| try: | ||
| await db.commit() | ||
| await db.refresh(new_profile) | ||
| except Exception: | ||
| await db.rollback() | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| detail="Failed to create profile. Please try again later." | ||
| ) | ||
|
|
||
| return new_profile | ||
|
|
||
|
|
||
| @router.post( | ||
| "/users/{user_id}/profile/", | ||
| response_model=ProfileResponseSchema, | ||
| status_code=status.HTTP_201_CREATED, | ||
| summary="Create user profile", | ||
| description="Create a user profile with avatar upload to S3 storage", | ||
| ) | ||
| async def create_user_profile( | ||
| user_id: int, | ||
| first_name: str = Form(...), | ||
| last_name: str = Form(...), | ||
| gender: str = Form(...), | ||
| date_of_birth: date = Form(...), | ||
| info: str = Form(...), | ||
| avatar: UploadFile = File(...), | ||
| current_user: UserModel = Depends(get_user), | ||
| db: AsyncSession = Depends(get_db), | ||
| s3_client: S3StorageInterface = Depends(get_s3_storage_client), | ||
| ) -> ProfileResponseSchema: | ||
| """ | ||
| Create a user profile with validation and avatar upload. | ||
|
|
||
| Authorization Rules: | ||
| - A user can only create their own profile | ||
| - Admins can create profiles for any user | ||
| """ | ||
| _validate_profile_input( | ||
| first_name, last_name, gender, date_of_birth, info, avatar | ||
| ) | ||
|
|
||
| _check_authorization(current_user, user_id) | ||
|
|
||
| await _verify_target_user(user_id, db) | ||
|
|
||
| await _check_existing_profile(user_id, db) | ||
|
|
||
| avatar_key = await _upload_avatar(user_id, avatar, s3_client) | ||
|
|
||
| new_profile = await _create_profile_in_db( | ||
| user_id, first_name, last_name, gender, | ||
| date_of_birth, info, avatar_key, db | ||
| ) | ||
|
|
||
| avatar_url = await s3_client.get_file_url(avatar_key) | ||
|
|
||
| return ProfileResponseSchema( | ||
| id=new_profile.id, | ||
| user_id=new_profile.user_id, | ||
| first_name=new_profile.first_name, | ||
| last_name=new_profile.last_name, | ||
| gender=new_profile.gender, | ||
| date_of_birth=new_profile.date_of_birth, | ||
| info=new_profile.info, | ||
| avatar=avatar_url | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,6 @@ | |
| TokenRefreshRequestSchema, | ||
| TokenRefreshResponseSchema | ||
| ) | ||
| from schemas.profiles import ( | ||
| ProfileResponseSchema, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ProfileCreateSchemais defined inschemas/profiles.pybut is not exported here. It's good practice to export all public schemas from the package's__init__.pyfile for consistency.