Skip to content

Commit

Permalink
adding user dashboard profile change for authenticated user
Browse files Browse the repository at this point in the history
  • Loading branch information
danilojezernik committed Sep 4, 2024
1 parent eadde9f commit bb94be7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/domain/user_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import BaseModel
from typing import Optional


class UserProfile(BaseModel):
username: Optional[str]
profession: Optional[str]
full_name: Optional[str]
description: Optional[str]
email: Optional[str]
facebook: Optional[str]
instagram: Optional[str]
twitter: Optional[str]
github: Optional[str]
www: Optional[str]
blog_notification: Optional[bool]
confirmed: Optional[bool]
62 changes: 62 additions & 0 deletions src/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from fastapi import APIRouter, Depends, HTTPException

from src.domain.user import User
from src.domain.user_profile import UserProfile
from src.services import db
from src.services.security import get_current_user, pwd_context, make_hash

Expand Down Expand Up @@ -224,3 +225,64 @@ async def delete_user_by_id(_id: str, current_user: str = Depends(get_current_us
else:
# If the blog was not found, raise a 404 error
raise HTTPException(status_code=404, detail=f'User by ID: ({_id}) not found!')


# USER DASHBOARD ROUTES

# Get ot view profile
@router.get('/me/profile', operation_id='get_user_profile')
async def get_user_profile(current_user: User = Depends(get_current_user)) -> UserProfile:
"""
Get the profile information for the authenticated user.
Only the logged-in user can access this route.
"""

# Fetch the user profile from the database
user_profile = db.process.user.find_one({'_id': current_user.id}, {
'username': 1, 'profession': 1, 'email': 1, 'facebook': 1, 'instagram': 1,
'twitter': 1, 'github': 1, 'www': 1, 'blog_notification': 1, 'full_name' : 1,
'description' :1, 'confirmed': 1
})

# If user profile is not found, raise a 404 error
if not user_profile:
raise HTTPException(status_code=404, detail="User profile not found")

# Return the user profile
return UserProfile(**user_profile)


# Edit or update profile
@router.put('/me/profile', operation_id='update_user_profile')
async def update_user_profile(
profile_data: UserProfile, current_user: User = Depends(get_current_user)
) -> UserProfile:
"""
Update the profile information for the authenticated user.
Users can only update certain fields like profession, social links, email, etc.
"""

# Convert profile_data to a dictionary and remove any empty fields
# Create an empty dictionary to hold the updated data
update_data = {}

# Convert the profile_data object to a dictionary
profile_dict = profile_data.dict()

# Loop through each key-value pair in the profile dictionary
for key, value in profile_dict.items():
# Check if the value is not None (i.e., only include fields that have values)
if value is not None:
# Add the key-value pair to the update_data dictionary
update_data[key] = value

# Update the user document in the database
update_result = db.process.user.update_one({'_id': current_user.id}, {'$set': update_data})

# Check if the update was successful
if update_result.modified_count == 0:
raise HTTPException(status_code=400, detail="Update failed or no changes detected")

# Return the updated profile data
updated_profile = db.process.user.find_one({'_id': current_user.id})
return UserProfile(**updated_profile)
2 changes: 1 addition & 1 deletion src/services/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def register_user(user: User):
email=user.email,
full_name=user.full_name,
hashed_password=hashed_password,
role=user.role # Ensure roles are also stored
role=user.role
)

# Save user to database
Expand Down

0 comments on commit bb94be7

Please sign in to comment.