Skip to content

Commit

Permalink
Changes to arhitecture were made. API folder deleted and user token a…
Browse files Browse the repository at this point in the history
…dded to domain. Added get contact by id in user route.
  • Loading branch information
danilojezernik committed Jun 17, 2024
1 parent 36388b8 commit bf565db
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from src.routes import index, blog, email, login, user, experiences, links, register, contact, projects, newsletter, \
subscriber, comments, github, book
from src.services import db
from src.services.domain_to_txt import write_fields_to_txt
from src.utils.domain_to_txt import write_fields_to_txt
from src.tags_metadata import tags_metadata

app = FastAPI(openapi_tags=tags_metadata)
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 23 additions & 2 deletions src/routes/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from src.domain.contact import Contact
from src.services import db, emails
from src.template import email_template

from src.services.security import get_current_user
from src.template import email_template

router = APIRouter()

Expand Down Expand Up @@ -77,6 +76,28 @@ async def get_all_emails_private(current_user: str = Depends(get_current_user)):
return contact_list


@router.get('/{:id}', operation_id='get_email_by_id_admin')
async def get_email_by_id_admin(_id: str,current_user: str = Depends(get_current_user)):
"""
This route handles the retrieval of one email by its ID from the database
:param current_user: Current user that is registered
:param _id: The ID of the contact to be retrieved
:return: If the project is found, returns the contact data; otherwise, returns a 404 error
"""

# Attempt to find a project in the database based on the provided ID
cursor = db.process.contact.find_one({'_id': id})

# If no contact is found, return a 404 error with a relevant detail message
if cursor is None:
raise HTTPException(status_code=404, detail=f'Contact by ID: ({id}) not found!')
else:

# If the contact is found, convert the cursor data into a Contact object and return it
return Contact(**cursor)


# Delete email by ID
@router.delete('/{_id}', operation_id='delete_email_by_id_private')
async def delete_email_by_id_private(_id: str, current_user: str = Depends(get_current_user)):
Expand Down
2 changes: 1 addition & 1 deletion src/routes/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from fastapi import Depends, HTTPException, status, APIRouter
from fastapi.security import OAuth2PasswordRequestForm

from src.api.token import Token
from src.domain.token import Token
from src.services.security import authenticate_user, create_access_token

# Create a new APIRouter instance for this module
Expand Down
23 changes: 11 additions & 12 deletions src/routes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def get_user_by_id(_id: str) -> User:


# Get all users from database
@router.get('/private/', operation_id='get_user_private')
@router.get('/admin/', operation_id='get_user_private')
async def get_user_private(current_user: str = Depends(get_current_user)) -> list[User]:
"""
This route handles the retrieval of all the users from the database
Expand Down Expand Up @@ -109,21 +109,20 @@ async def edit_user_by_id(_id: str, user: User, current_user: str = Depends(get_
:return: If the user is successfully edited, returns the updated User object; otherwise, returns None.
"""

# Check if the user wants to update the password
if 'hashed_password' in user.dict(by_alias=True):
# Hash the provided password using pwd_context.hash
hashed_password = pwd_context.hash(user.dict(by_alias=True)['hashed_password'])
user.dict()['hashed_password'] = hashed_password

# Convert the user object to a dictionary with alias
user_dict = user.dict(by_alias=True)

# Remove '_id' from the dictionary as it shouldn't be updated
del user_dict['_id']
# Check if the user wants to update the password
if 'hashed_password' in user_dict and user_dict['hashed_password']:
# Hash the provided password using pwd_context.hash
hashed_password = pwd_context.hash(user_dict['hashed_password'])
user_dict['hashed_password'] = hashed_password
else:
# Remove the 'hashed_password' key if it exists but is empty
user_dict.pop('hashed_password', None)

# Hash the password before updating the document in the database
if 'hashed_password' in user_dict:
user_dict['hashed_password'] = pwd_context.hash(user_dict['hashed_password'])
# Remove '_id' from the dictionary as it shouldn't be updated
user_dict.pop('_id', None)

# Update the user document in the database
cursor = db.process.user.update_one({'_id': _id}, {'$set': user_dict})
Expand Down
2 changes: 1 addition & 1 deletion src/services/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from src.domain.user import User
from src import env
from src.domain.user_in_db import UserInDB
from src.api.token_data import TokenData
from src.domain.token_data import TokenData
from src.services import db

# Initialize a password context with bcrypt hashing
Expand Down
File renamed without changes.

0 comments on commit bf565db

Please sign in to comment.