From 9e077430a662ea21db5733b50dffe2ed8e6eac0a Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sat, 21 Oct 2023 15:40:23 +0200 Subject: [PATCH 1/3] update make --- Makefile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 59e6b5a..cbbb8f3 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ py-upgrade: ## Upgrade project py files with pyupgrade library for python versio .PHONY: lint lint: ## Lint project code. - poetry run ruff . + poetry run ruff --fix . .PHONY: format format: ## Format project code. @@ -56,4 +56,8 @@ docker-feed-database: ## create database objects and insert data docker-compose exec db psql devdb user -f /home/gx/code/shakespeare_wordform.sql | true docker-compose exec db psql devdb user -f /home/gx/code/shakespeare_character.sql | true docker-compose exec db psql devdb user -f /home/gx/code/shakespeare_paragraph.sql | true - docker-compose exec db psql devdb user -f /home/gx/code/shakespeare_character_work.sql \ No newline at end of file + docker-compose exec db psql devdb user -f /home/gx/code/shakespeare_character_work.sql + +.PHONY: model-generate +model-generate: ## generate sqlalchemy models from database + poetry run sqlacodegen --generator declarative postgresql://user:secret@0.0.0.0/devdb --outfile models.py --schemas shakespeare From 587690cb3e59220458496e618f60f1c4163f6b9f Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sat, 21 Oct 2023 15:53:22 +0200 Subject: [PATCH 2/3] update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 01f1e93..2f668a6 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,6 @@ It took me a while to find nice data set. Hope works of Shakespeare as example w first part with read only declarative base configuration and all type of funny selects :) Data set is coming form https://github.com/catherinedevlin/opensourceshakespeare Next models were generated with https://github.com/agronholm/sqlacodegen -And after some tweaking I got desired result

(back to top)

@@ -133,7 +132,8 @@ poetry install Hope you enjoy it. ## Acknowledgments -Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off! +Use this space to list resources you find helpful and would like to give credit to. +I've included a few of my favorites to kick things off! * [Open Source Shakespeare Dataset](https://github.com/catherinedevlin/opensourceshakespeare) * [SQL Code Generator](https://github.com/agronholm/sqlacodegen) @@ -154,6 +154,7 @@ Use this space to list resources you find helpful and would like to give credit - **[JUL 7 2023]** migrate to pydantic 2.0 :fast_forward: - **[JUL 25 2023]** add user authentication with JWT and Redis as token storage :lock: :key: - **[SEP 2 2023]** add passlib and bcrypt for password hashing :lock: :key: +- **[OCT 21 2023]** refactor shakespeare models to use sqlalchemy 2.0 :fast_forward:

(back to top)

From 4e2d3026c86738ddae44595f64c43e2f8bda55f5 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Tue, 24 Oct 2023 20:28:30 +0200 Subject: [PATCH 3/3] refactor --- app/exceptions.py | 10 +++++----- app/models/shakespeare.py | 3 +-- app/services/auth.py | 18 +++++++----------- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/exceptions.py b/app/exceptions.py index 8f1806a..b6d7a2e 100644 --- a/app/exceptions.py +++ b/app/exceptions.py @@ -5,7 +5,7 @@ class BadRequestHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_400_BAD_REQUEST, - detail=msg if msg else "Bad request", + detail=msg or "Bad request", ) @@ -31,7 +31,7 @@ class ForbiddenHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_403_FORBIDDEN, - detail=msg if msg else "Requested resource is forbidden", + detail=msg or "Requested resource is forbidden", ) @@ -39,7 +39,7 @@ class NotFoundHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_404_NOT_FOUND, - detail=msg if msg else "Requested resource is not found", + detail=msg or "Requested resource is not found", ) @@ -47,7 +47,7 @@ class ConflictHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_409_CONFLICT, - detail=msg if msg else "Conflicting resource request", + detail=msg or "Conflicting resource request", ) @@ -55,5 +55,5 @@ class ServiceNotAvailableHTTPException(HTTPException): def __init__(self, msg: str): super().__init__( status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail=msg if msg else "Service not available", + detail=msg or "Service not available", ) diff --git a/app/models/shakespeare.py b/app/models/shakespeare.py index 88d9558..262bd35 100644 --- a/app/models/shakespeare.py +++ b/app/models/shakespeare.py @@ -133,5 +133,4 @@ class Paragraph(Base): async def find(cls, db_session: AsyncSession, character: str): stmt = select(cls).join(Character).join(Chapter).join(Work).where(Character.name == character) result = await db_session.execute(stmt) - instance = result.scalars().all() - return instance + return result.scalars().all() diff --git a/app/services/auth.py b/app/services/auth.py index e8cf692..10d22f4 100644 --- a/app/services/auth.py +++ b/app/services/auth.py @@ -10,10 +10,7 @@ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials async def verify_jwt(request: Request, token: str) -> bool: _payload = await request.app.state.redis.get(token) - if _payload: - return True - else: - return False + return bool(_payload) class AuthBearer(HTTPBearer): @@ -22,14 +19,13 @@ class AuthBearer(HTTPBearer): async def __call__(self, request: Request): credentials: HTTPAuthorizationCredentials = await super().__call__(request) - if credentials: - if not credentials.scheme == "Bearer": - raise HTTPException(status_code=403, detail="Invalid authentication scheme.") - if not await verify_jwt(request, credentials.credentials): - raise HTTPException(status_code=403, detail="Invalid token or expired token.") - return credentials.credentials - else: + if not credentials: raise HTTPException(status_code=403, detail="Invalid authorization code.") + if credentials.scheme != "Bearer": + raise HTTPException(status_code=403, detail="Invalid authentication scheme.") + if not await verify_jwt(request, credentials.credentials): + raise HTTPException(status_code=403, detail="Invalid token or expired token.") + return credentials.credentials async def create_access_token(user: User, request: Request):