From bf565db385591d966350719180a3e4caa16510a8 Mon Sep 17 00:00:00 2001 From: danilojezernik Date: Mon, 17 Jun 2024 12:52:22 +0200 Subject: [PATCH] Changes to arhitecture were made. API folder deleted and user token added to domain. Added get contact by id in user route. --- src/__main__.py | 2 +- src/{api => domain}/token.py | 0 src/{api => domain}/token_data.py | 0 src/routes/contact.py | 25 ++++++++++++++++++++++-- src/routes/login.py | 2 +- src/routes/user.py | 23 +++++++++++----------- src/services/security.py | 2 +- src/{services => utils}/domain_to_txt.py | 0 8 files changed, 37 insertions(+), 17 deletions(-) rename src/{api => domain}/token.py (100%) rename src/{api => domain}/token_data.py (100%) rename src/{services => utils}/domain_to_txt.py (100%) diff --git a/src/__main__.py b/src/__main__.py index ae8d635..f44ea60 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -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) diff --git a/src/api/token.py b/src/domain/token.py similarity index 100% rename from src/api/token.py rename to src/domain/token.py diff --git a/src/api/token_data.py b/src/domain/token_data.py similarity index 100% rename from src/api/token_data.py rename to src/domain/token_data.py diff --git a/src/routes/contact.py b/src/routes/contact.py index 44ffc4e..ab7a147 100644 --- a/src/routes/contact.py +++ b/src/routes/contact.py @@ -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() @@ -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)): diff --git a/src/routes/login.py b/src/routes/login.py index 6708952..d581aae 100644 --- a/src/routes/login.py +++ b/src/routes/login.py @@ -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 diff --git a/src/routes/user.py b/src/routes/user.py index 9a40acc..7552884 100644 --- a/src/routes/user.py +++ b/src/routes/user.py @@ -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 @@ -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}) diff --git a/src/services/security.py b/src/services/security.py index fcb28d8..2087563 100644 --- a/src/services/security.py +++ b/src/services/security.py @@ -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 diff --git a/src/services/domain_to_txt.py b/src/utils/domain_to_txt.py similarity index 100% rename from src/services/domain_to_txt.py rename to src/utils/domain_to_txt.py