Skip to content
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

Final PR of 2024 #42

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions app/api/v1/endpoints/nurse.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from passlib.context import CryptContext
from passlib.exc import UnknownHashError
from typing import List, Union, Dict

from ....db.session import get_db
from ....schemas.nurse import *
from ....crud import crud_nurse
from ....utils.passwd_helpers import get_password_hash, verify_password

router = APIRouter()

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

@router.get("/", response_model=Union[List[Nurse], Dict[str, str]])
async def read_nurses(skip: int = 0, limit: int = 100, db: AsyncSession = Depends(get_db)) -> Union[List[Nurse], Dict[str, str]]:
nurses = await crud_nurse.get_nurses(db, skip=skip, limit=limit)
Expand Down Expand Up @@ -40,29 +37,52 @@ async def delete_nurse(nurse_id: int, db: AsyncSession = Depends(get_db)) -> Uni
return success
return {"detail": "Nurse not found or delete operation failed"} # Return detail message if the delete operation fails

@router.post("/register", response_model=Union[Nurse, Dict[str, str]])
async def register_nurse(nurse: NurseRegister, db: AsyncSession = Depends(get_db)) -> Union[Nurse, Dict[str, str]]:
@router.post("/register", response_model=Nurse)
async def register_nurse(nurse: NurseRegister, db: AsyncSession = Depends(get_db)) -> Nurse:
if await crud_nurse.is_email_taken(db, nurse.email):
return {"detail": "Email already registered"} # Return detail message if the email is already taken

# Hash the password before storing
nurse.password = pwd_context.hash(nurse.password)

created_nurse = await crud_nurse.create_nurse(db, nurse)
if created_nurse:
raise HTTPException(
status_code=400,
detail="Email already registered"
)

try:
# Hash the password using the security module
nurse.password = get_password_hash(nurse.password)
created_nurse = await crud_nurse.create_nurse(db, nurse)

if not created_nurse:
raise HTTPException(
status_code=500,
detail="Failed to create nurse"
)

return created_nurse
return {"detail": "Nurse registration failed"} # Return detail message if nurse creation fails

except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Registration failed: {str(e)}"
)

@router.post("/login", response_model=Dict[str, Union[str, int]])
async def login_nurse(nurse: NurseLogin, db: AsyncSession = Depends(get_db)) -> Dict[str, Union[str, int]]:
db_nurse = await crud_nurse.get_nurse_by_email(db, nurse.email)
if not db_nurse:
return {"detail": "Invalid email or password"} # Return detail message if email is incorrect
raise HTTPException(
status_code=401,
detail="Invalid email or password"
)

try:
if not pwd_context.verify(nurse.password, db_nurse.password):
return {"detail": "Invalid email or password"} # Return detail message if password is incorrect
except UnknownHashError:
return {"detail": "Invalid email or password"} # Catch and return message for unknown hash errors
if not verify_password(nurse.password, db_nurse.password):
raise HTTPException(
status_code=401,
detail="Invalid email or password"
)
except Exception as e:
raise HTTPException(
status_code=500,
detail="Authentication error"
)

return {"message": "Login successful", "nurse_id": db_nurse.id}
15 changes: 15 additions & 0 deletions app/utils/passwd_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import bcrypt

def get_password_hash(password: str) -> str:
"""Hash a password using bcrypt"""
return bcrypt.hashpw(
password.encode('utf-8'),
bcrypt.gensalt()
).decode('utf-8')

def verify_password(plain_password: str, hashed_password: str) -> bool:
"""Verify a password against a hash"""
return bcrypt.checkpw(
plain_password.encode('utf-8'),
hashed_password.encode('utf-8')
)
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ asyncpg = "^0.30.0"
pydantic-settings = "^2.4.0"
pgvector = "^0.3.2"
passlib = "^1.7.4"
bcrypt = "^4.0.1"
bcrypt = "^4.2.1"

[tool.poe.tasks]
run = "uvicorn 'app.main:app' --host=0.0.0.0 --port=8000 --loop asyncio"
Expand Down
82 changes: 41 additions & 41 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ aiohappyeyeballs==2.4.4 ; python_version >= "3.10" and python_version < "4.0"
aiohttp==3.11.10 ; python_version >= "3.10" and python_version < "4.0"
aiosignal==1.3.2 ; python_version >= "3.10" and python_version < "4.0"
amqp==5.3.1 ; python_version >= "3.10" and python_version < "4.0"
annotated-types==0.6.0 ; python_version >= "3.10" and python_version < "4.0"
anyio==4.3.0 ; python_version >= "3.10" and python_version < "4.0"
annotated-types==0.7.0 ; python_version >= "3.10" and python_version < "4.0"
anyio==4.7.0 ; python_version >= "3.10" and python_version < "4.0"
asgiref==3.8.1 ; python_version >= "3.10" and python_version < "4.0"
async-timeout==4.0.3 ; python_version >= "3.10" and python_version < "3.12.0"
asyncpg==0.29.0 ; python_version >= "3.10" and python_version < "4.0"
async-timeout==4.0.3 ; python_version >= "3.10" and python_full_version < "3.11.3"
asyncpg==0.30.0 ; python_version >= "3.10" and python_version < "4.0"
attrs==24.2.0 ; python_version >= "3.10" and python_version < "4.0"
backoff==2.2.1 ; python_version >= "3.10" and python_version < "4.0"
bcrypt==4.2.1 ; python_version >= "3.10" and python_version < "4.0"
billiard==4.2.1 ; python_version >= "3.10" and python_version < "4.0"
botocore==1.34.91 ; python_version >= "3.10" and python_version < "4.0"
botocore==1.35.90 ; python_version >= "3.10" and python_version < "4.0"
build==1.2.2.post1 ; python_version >= "3.10" and python_version < "4.0"
cachecontrol[filecache]==0.14.1 ; python_version >= "3.10" and python_version < "4.0"
cachetools==5.3.3 ; python_version >= "3.10" and python_version < "4.0"
cachetools==5.5.0 ; python_version >= "3.10" and python_version < "4.0"
celery==5.4.0 ; python_version >= "3.10" and python_version < "4.0"
celery[redis]==5.4.0 ; python_version >= "3.10" and python_version < "4.0"
certifi==2024.2.2 ; python_version >= "3.10" and python_version < "4.0"
certifi==2024.12.14 ; python_version >= "3.10" and python_version < "4.0"
cffi==1.17.1 ; python_version >= "3.10" and python_version < "4.0" and (sys_platform == "darwin" or sys_platform == "linux") and (sys_platform == "darwin" or platform_python_implementation != "PyPy")
charset-normalizer==3.3.2 ; python_version >= "3.10" and python_version < "4.0"
charset-normalizer==3.4.1 ; python_version >= "3.10" and python_version < "4.0"
chroma-hnswlib==0.7.6 ; python_version >= "3.10" and python_version < "4.0"
chromadb==0.5.23 ; python_version >= "3.10" and python_version < "4.0"
cleo==2.1.0 ; python_version >= "3.10" and python_version < "4.0"
click-didyoumean==0.3.1 ; python_version >= "3.10" and python_version < "4.0"
click-plugins==1.1.1 ; python_version >= "3.10" and python_version < "4.0"
click-repl==0.3.0 ; python_version >= "3.10" and python_version < "4.0"
click==8.1.7 ; python_version >= "3.10" and python_version < "4.0"
click==8.1.8 ; python_version >= "3.10" and python_version < "4.0"
colorama==0.4.6 ; python_version >= "3.10" and python_version < "4.0" and (platform_system == "Windows" or os_name == "nt" or sys_platform == "win32")
coloredlogs==15.0.1 ; python_version >= "3.10" and python_version < "4.0"
crashtest==0.4.1 ; python_version >= "3.10" and python_version < "4.0"
Expand All @@ -38,47 +38,47 @@ distlib==0.3.9 ; python_version >= "3.10" and python_version < "4.0"
dulwich==0.21.7 ; python_version >= "3.10" and python_version < "4.0"
durationpy==0.9 ; python_version >= "3.10" and python_version < "4.0"
exceptiongroup==1.2.2 ; python_version >= "3.10" and python_version < "3.11"
fastapi==0.110.2 ; python_version >= "3.10" and python_version < "4.0"
fastapi==0.115.6 ; python_version >= "3.10" and python_version < "4.0"
fastjsonschema==2.21.1 ; python_version >= "3.10" and python_version < "4.0"
filelock==3.16.1 ; python_version >= "3.10" and python_version < "4.0"
flatbuffers==24.3.25 ; python_version >= "3.10" and python_version < "4.0"
flower==2.0.1 ; python_version >= "3.10" and python_version < "4.0"
frozenlist==1.5.0 ; python_version >= "3.10" and python_version < "4.0"
fsspec==2024.10.0 ; python_version >= "3.10" and python_version < "4.0"
google-api-core==2.18.0 ; python_version >= "3.10" and python_version < "4.0"
google-auth==2.29.0 ; python_version >= "3.10" and python_version < "4.0"
google-api-core==2.24.0 ; python_version >= "3.10" and python_version < "4.0"
google-auth==2.37.0 ; python_version >= "3.10" and python_version < "4.0"
google-cloud-core==2.4.1 ; python_version >= "3.10" and python_version < "4.0"
google-cloud-storage==2.16.0 ; python_version >= "3.10" and python_version < "4.0"
google-crc32c==1.5.0 ; python_version >= "3.10" and python_version < "4.0"
google-resumable-media==2.7.0 ; python_version >= "3.10" and python_version < "4.0"
googleapis-common-protos==1.63.0 ; python_version >= "3.10" and python_version < "4.0"
google-cloud-storage==2.19.0 ; python_version >= "3.10" and python_version < "4.0"
google-crc32c==1.6.0 ; python_version >= "3.10" and python_version < "4.0"
google-resumable-media==2.7.2 ; python_version >= "3.10" and python_version < "4.0"
googleapis-common-protos==1.66.0 ; python_version >= "3.10" and python_version < "4.0"
greenlet==3.1.1 ; python_version < "3.13" and (platform_machine == "aarch64" or platform_machine == "ppc64le" or platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "AMD64" or platform_machine == "win32" or platform_machine == "WIN32") and python_version >= "3.10"
grpcio==1.68.1 ; python_version >= "3.10" and python_version < "4.0"
h11==0.14.0 ; python_version >= "3.10" and python_version < "4.0"
httpcore==1.0.5 ; python_version >= "3.10" and python_version < "4.0"
httpcore==1.0.7 ; python_version >= "3.10" and python_version < "4.0"
httptools==0.6.4 ; python_version >= "3.10" and python_version < "4.0"
httpx==0.27.0 ; python_version >= "3.10" and python_version < "4.0"
httpx==0.28.1 ; python_version >= "3.10" and python_version < "4.0"
huggingface-hub==0.26.5 ; python_version >= "3.10" and python_version < "4.0"
humanfriendly==10.0 ; python_version >= "3.10" and python_version < "4.0"
humanize==4.11.0 ; python_version >= "3.10" and python_version < "4.0"
idna==3.7 ; python_version >= "3.10" and python_version < "4.0"
idna==3.10 ; python_version >= "3.10" and python_version < "4.0"
importlib-metadata==8.5.0 ; python_version >= "3.10" and python_version < "4.0"
importlib-resources==6.4.5 ; python_version >= "3.10" and python_version < "4.0"
installer==0.7.0 ; python_version >= "3.10" and python_version < "4.0"
jaraco-classes==3.4.0 ; python_version >= "3.10" and python_version < "4.0"
jeepney==0.8.0 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "linux"
jinja2==3.1.4 ; python_version >= "3.10" and python_version < "4.0"
jinja2==3.1.5 ; python_version >= "3.10" and python_version < "4.0"
jmespath==1.0.1 ; python_version >= "3.10" and python_version < "4.0"
jq==1.8.0 ; python_version >= "3.10" and python_version < "4.0"
jsonpatch==1.33 ; python_version >= "3.10" and python_version < "4.0"
jsonpointer==3.0.0 ; python_version >= "3.10" and python_version < "4.0"
keyring==24.3.1 ; python_version >= "3.10" and python_version < "4.0"
kombu==5.4.2 ; python_version >= "3.10" and python_version < "4.0"
kubernetes==31.0.0 ; python_version >= "3.10" and python_version < "4.0"
langchain-community==0.2.19 ; python_version >= "3.10" and python_version < "4.0"
langchain-core==0.2.43 ; python_version >= "3.10" and python_version < "4.0"
langchain-text-splitters==0.2.4 ; python_version >= "3.10" and python_version < "4.0"
langchain==0.2.17 ; python_version >= "3.10" and python_version < "4.0"
langchain-community==0.2.5 ; python_version >= "3.10" and python_version < "4.0"
langchain-core==0.2.8 ; python_version >= "3.10" and python_version < "4.0"
langchain-text-splitters==0.2.1 ; python_version >= "3.10" and python_version < "4.0"
langchain==0.2.5 ; python_version >= "3.10" and python_version < "4.0"
langchainhub==0.1.21 ; python_version >= "3.10" and python_version < "4.0"
langsmith==0.1.147 ; python_version >= "3.10" and python_version < "4.0"
markdown-it-py==3.0.0 ; python_version >= "3.10" and python_version < "4.0"
Expand Down Expand Up @@ -114,66 +114,66 @@ pexpect==4.9.0 ; python_version >= "3.10" and python_version < "4.0"
pgvector==0.3.6 ; python_version >= "3.10" and python_version < "4.0"
pkginfo==1.12.0 ; python_version >= "3.10" and python_version < "4.0"
platformdirs==4.3.6 ; python_version >= "3.10" and python_version < "4.0"
poethepoet==0.25.1 ; python_version >= "3.10" and python_version < "4.0"
poethepoet==0.32.0 ; python_version >= "3.10" and python_version < "4.0"
poetry-core==1.9.1 ; python_version >= "3.10" and python_version < "4.0"
poetry-plugin-export==1.8.0 ; python_version >= "3.10" and python_version < "4.0"
poetry==1.8.5 ; python_version >= "3.10" and python_version < "4.0"
posthog==3.7.4 ; python_version >= "3.10" and python_version < "4.0"
prometheus-client==0.21.1 ; python_version >= "3.10" and python_version < "4.0"
prompt-toolkit==3.0.48 ; python_version >= "3.10" and python_version < "4.0"
propcache==0.2.1 ; python_version >= "3.10" and python_version < "4.0"
proto-plus==1.23.0 ; python_version >= "3.10" and python_version < "4.0"
protobuf==4.25.3 ; python_version >= "3.10" and python_version < "4.0"
proto-plus==1.25.0 ; python_version >= "3.10" and python_version < "4.0"
protobuf==4.25.5 ; python_version >= "3.10" and python_version < "4.0"
ptyprocess==0.7.0 ; python_version >= "3.10" and python_version < "4.0"
pyasn1-modules==0.4.0 ; python_version >= "3.10" and python_version < "4.0"
pyasn1==0.6.0 ; python_version >= "3.10" and python_version < "4.0"
pyasn1-modules==0.4.1 ; python_version >= "3.10" and python_version < "4.0"
pyasn1==0.6.1 ; python_version >= "3.10" and python_version < "4.0"
pycparser==2.22 ; python_version >= "3.10" and python_version < "4.0" and (sys_platform == "darwin" or sys_platform == "linux") and (sys_platform == "darwin" or platform_python_implementation != "PyPy")
pydantic-core==2.18.2 ; python_version >= "3.10" and python_version < "4.0"
pydantic-settings==2.7.0 ; python_version >= "3.10" and python_version < "4.0"
pydantic==2.7.1 ; python_version >= "3.10" and python_version < "4.0"
pygments==2.17.2 ; python_version >= "3.10" and python_version < "4.0"
pygments==2.18.0 ; python_version >= "3.10" and python_version < "4.0"
pyngrok==7.2.2 ; python_version >= "3.10" and python_version < "4.0"
pypdf==4.3.1 ; python_version >= "3.10" and python_version < "4.0"
pypika==0.48.9 ; python_version >= "3.10" and python_version < "4.0"
pyproject-hooks==1.2.0 ; python_version >= "3.10" and python_version < "4.0"
pyreadline3==3.5.4 ; sys_platform == "win32" and python_version >= "3.10" and python_version < "4.0"
python-dateutil==2.9.0.post0 ; python_version >= "3.10" and python_version < "4.0"
python-dotenv==1.0.1 ; python_version >= "3.10" and python_version < "4.0"
python-multipart==0.0.9 ; python_version >= "3.10" and python_version < "4.0"
python-multipart==0.0.20 ; python_version >= "3.10" and python_version < "4.0"
pytz==2024.2 ; python_version >= "3.10" and python_version < "4.0"
pywin32-ctypes==0.2.3 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "win32"
pyyaml==6.0.2 ; python_version >= "3.10" and python_version < "4.0"
rapidfuzz==3.10.1 ; python_version >= "3.10" and python_version < "4.0"
redis==5.2.1 ; python_version >= "3.10" and python_version < "4.0"
replicate==0.25.2 ; python_version >= "3.10" and python_version < "4.0"
replicate==0.34.2 ; python_version >= "3.10" and python_version < "4.0"
requests-oauthlib==2.0.0 ; python_version >= "3.10" and python_version < "4.0"
requests-toolbelt==1.0.0 ; python_version >= "3.10" and python_version < "4.0"
requests==2.31.0 ; python_version >= "3.10" and python_version < "4.0"
requests==2.32.3 ; python_version >= "3.10" and python_version < "4.0"
rich==13.9.4 ; python_version >= "3.10" and python_version < "4.0"
rsa==4.9 ; python_version >= "3.10" and python_version < "4"
s3transfer==0.10.1 ; python_version >= "3.10" and python_version < "4.0"
s3transfer==0.10.4 ; python_version >= "3.10" and python_version < "4.0"
secretstorage==3.3.3 ; python_version >= "3.10" and python_version < "4.0" and sys_platform == "linux"
shellingham==1.5.4 ; python_version >= "3.10" and python_version < "4.0"
six==1.16.0 ; python_version >= "3.10" and python_version < "4.0"
six==1.17.0 ; python_version >= "3.10" and python_version < "4.0"
sniffio==1.3.1 ; python_version >= "3.10" and python_version < "4.0"
sqlalchemy==2.0.36 ; python_version >= "3.10" and python_version < "4.0"
starlette==0.37.2 ; python_version >= "3.10" and python_version < "4.0"
starlette==0.41.3 ; python_version >= "3.10" and python_version < "4.0"
sympy==1.13.3 ; python_version >= "3.10" and python_version < "4.0"
tenacity==8.5.0 ; python_version >= "3.10" and python_version < "4.0"
tokenizers==0.20.3 ; python_version >= "3.10" and python_version < "4.0"
tomli==2.2.1 ; python_version >= "3.10" and python_version < "4.0"
tomli==2.2.1 ; python_version >= "3.10" and python_version < "3.11"
tomlkit==0.13.2 ; python_version >= "3.10" and python_version < "4.0"
tornado==6.4.2 ; python_version >= "3.10" and python_version < "4.0"
tqdm==4.67.1 ; python_version >= "3.10" and python_version < "4.0"
trove-classifiers==2024.10.21.16 ; python_version >= "3.10" and python_version < "4.0"
typer==0.15.1 ; python_version >= "3.10" and python_version < "4.0"
types-requests==2.32.0.20241016 ; python_version >= "3.10" and python_version < "4.0"
typing-extensions==4.11.0 ; python_version >= "3.10" and python_version < "4.0"
typing-extensions==4.12.2 ; python_version >= "3.10" and python_version < "4.0"
typing-inspect==0.9.0 ; python_version >= "3.10" and python_version < "4.0"
tzdata==2024.2 ; python_version >= "3.10" and python_version < "4.0"
urllib3==2.2.1 ; python_version >= "3.10" and python_version < "4.0"
uvicorn==0.29.0 ; python_version >= "3.10" and python_version < "4.0"
uvicorn[standard]==0.29.0 ; python_version >= "3.10" and python_version < "4.0"
urllib3==2.3.0 ; python_version >= "3.10" and python_version < "4.0"
uvicorn==0.34.0 ; python_version >= "3.10" and python_version < "4.0"
uvicorn[standard]==0.34.0 ; python_version >= "3.10" and python_version < "4.0"
uvloop==0.21.0 ; (sys_platform != "win32" and sys_platform != "cygwin") and platform_python_implementation != "PyPy" and python_version >= "3.10" and python_version < "4.0"
vine==5.1.0 ; python_version >= "3.10" and python_version < "4.0"
virtualenv==20.28.0 ; python_version >= "3.10" and python_version < "4.0"
Expand Down