Skip to content

Commit

Permalink
python upgrade, CRUDBase replaced with fastcrud
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed Jan 24, 2024
1 parent bd8963a commit b5babd3
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 653 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
- 🏬 Easy redis caching
- 👜 Easy client-side caching
- 🚦 ARQ integration for task queue
- ⚙️ Efficient querying (only queries what's needed) with support for joins
- ⎘ Out of the box pagination support
- ⚙️ Efficient and robust queries with <a href="https://github.com/igorbenav/fastcrud">fastcrud</a>
- ⎘ Out of the box offset and cursor pagination support with <a href="https://github.com/igorbenav/fastcrud">fastcrud</a>
- 🛑 Rate Limiter dependency
- 👮 FastAPI docs behind authentication and hidden based on the environment
- 🦾 Easily extendable
Expand Down Expand Up @@ -749,14 +749,15 @@ poetry run alembic upgrade head

### 5.6 CRUD

Inside `app/crud`, create a new `crud_entities.py` inheriting from `CRUDBase` for each new entity:
Inside `app/crud`, create a new `crud_entities.py` inheriting from `FastCRUD` for each new entity:

```python
from app.crud.crud_base import CRUDBase
from fastcrud import FastCRUD

from app.models.entity import Entity
from app.schemas.entity import EntityCreateInternal, EntityUpdate, EntityUpdateInternal, EntityDelete

CRUDEntity = CRUDBase[Entity, EntityCreateInternal, EntityUpdate, EntityUpdateInternal, EntityDelete]
CRUDEntity = FastCRUD[Entity, EntityCreateInternal, EntityUpdate, EntityUpdateInternal, EntityDelete]
crud_entity = CRUDEntity(Entity)
```

Expand All @@ -767,7 +768,7 @@ So, for users:
from app.model.user import User
from app.schemas.user import UserCreateInternal, UserUpdate, UserUpdateInternal, UserDelete

CRUDUser = CRUDBase[User, UserCreateInternal, UserUpdate, UserUpdateInternal, UserDelete]
CRUDUser = FastCRUD[User, UserCreateInternal, UserUpdate, UserUpdateInternal, UserDelete]
crud_users = CRUDUser(User)
```

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ redis = "^5.0.1"
arq = "^0.25.0"
gunicorn = "^21.2.0"
bcrypt = "^4.1.1"
fastcrud = "^0.1.5"


[build-system]
Expand Down
5 changes: 3 additions & 2 deletions src/app/core/db/crud_token_blacklist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ...crud.crud_base import CRUDBase
from fastcrud import FastCRUD

from ..db.token_blacklist import TokenBlacklist
from ..schemas import TokenBlacklistCreate, TokenBlacklistUpdate

CRUDTokenBlacklist = CRUDBase[TokenBlacklist, TokenBlacklistCreate, TokenBlacklistUpdate, TokenBlacklistUpdate, None]
CRUDTokenBlacklist = FastCRUD[TokenBlacklist, TokenBlacklistCreate, TokenBlacklistUpdate, TokenBlacklistUpdate, None]
crud_token_blacklist = CRUDTokenBlacklist(TokenBlacklist)
45 changes: 0 additions & 45 deletions src/app/core/exceptions/http_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,45 +0,0 @@
from http import HTTPStatus

from fastapi import HTTPException, status


class CustomException(HTTPException):
def __init__(self, status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR, detail: str | None = None):
if not detail:
detail = HTTPStatus(status_code).description
super().__init__(status_code=status_code, detail=detail)


class BadRequestException(CustomException):
def __init__(self, detail: str | None = None):
super().__init__(status_code=status.HTTP_400_BAD_REQUEST, detail=detail)


class NotFoundException(CustomException):
def __init__(self, detail: str | None = None):
super().__init__(status_code=status.HTTP_404_NOT_FOUND, detail=detail)


class ForbiddenException(CustomException):
def __init__(self, detail: str | None = None):
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)


class UnauthorizedException(CustomException):
def __init__(self, detail: str | None = None):
super().__init__(status_code=status.HTTP_401_UNAUTHORIZED, detail=detail)


class UnprocessableEntityException(CustomException):
def __init__(self, detail: str | None = None):
super().__init__(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=detail)


class DuplicateValueException(CustomException):
def __init__(self, detail: str | None = None):
super().__init__(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=detail)


class RateLimitException(CustomException):
def __init__(self, detail: str | None = None):
super().__init__(status_code=status.HTTP_429_TOO_MANY_REQUESTS, detail=detail)
1 change: 1 addition & 0 deletions src/app/core/worker/functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging

import uvloop
from arq.worker import Worker

Expand Down
3 changes: 2 additions & 1 deletion src/app/core/worker/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from arq.connections import RedisSettings

from ...core.config import settings
from .functions import sample_background_task, startup, shutdown
from .functions import sample_background_task, shutdown, startup

REDIS_QUEUE_HOST = settings.REDIS_QUEUE_HOST
REDIS_QUEUE_PORT = settings.REDIS_QUEUE_PORT
Expand Down
Loading

0 comments on commit b5babd3

Please # to comment.