mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
Merge pull request #120 from grillazz/2-add-model-relations-in-async-environment
2 add model relations in async environment
This commit is contained in:
commit
5f27bc28bf
6
Makefile
6
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.
|
||||
@ -57,3 +57,7 @@ docker-feed-database: ## create database objects and insert data
|
||||
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
|
||||
|
||||
.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
|
||||
|
@ -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
|
||||
|
||||
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
||||
|
||||
@ -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:
|
||||
|
||||
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
||||
|
||||
|
@ -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",
|
||||
)
|
||||
|
@ -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()
|
||||
|
@ -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":
|
||||
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
|
||||
else:
|
||||
raise HTTPException(status_code=403, detail="Invalid authorization code.")
|
||||
|
||||
|
||||
async def create_access_token(user: User, request: Request):
|
||||
|
Loading…
x
Reference in New Issue
Block a user