Skip to content

Commit 7f7d2b0

Browse files
committed
docs updated, type hints fixed
1 parent 9e11e32 commit 7f7d2b0

File tree

3 files changed

+85
-77
lines changed

3 files changed

+85
-77
lines changed

README.md

Lines changed: 79 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -410,101 +410,109 @@ poetry run alembic upgrade head
410410
### 5.1 Project Structure
411411
First, you may want to take a look at the project structure and understand what each file is doing.
412412
```sh
413-
. # FastAPI-boilerplate folder. Rename it to suit your project name
414-
├── Dockerfile # Dockerfile for building the application container.
415-
├── LICENSE.md # License file for the project.
416-
├── README.md # Project README providing information and instructions.
417-
├── docker-compose.yml # Docker Compose file for defining multi-container applications.
413+
. # FastAPI-boilerplate folder. Rename it to suit your project name
414+
├── Dockerfile # Dockerfile for building the application container.
415+
├── LICENSE.md # License file for the project.
416+
├── README.md # Project README providing information and instructions.
417+
├── docker-compose.yml # Docker Compose file for defining multi-container applications.
418418
419-
└── src # Source code directory.
420-
├── __init__.py # Initialization file for the src package.
421-
├── alembic.ini # Configuration file for Alembic (database migration tool).
422-
├── poetry.lock # Poetry lock file specifying exact versions of dependencies.
423-
├── pyproject.toml # Configuration file for Poetry, lists project dependencies.
419+
└── src # Source code directory.
420+
├── __init__.py # Initialization file for the src package.
421+
├── alembic.ini # Configuration file for Alembic (database migration tool).
422+
├── poetry.lock # Poetry lock file specifying exact versions of dependencies.
423+
├── pyproject.toml # Configuration file for Poetry, lists project dependencies.
424424
425-
├── app # Main application directory.
426-
│ ├── __init__.py # Initialization file for the app package.
427-
│ ├── main.py # Entry point that imports and creates the FastAPI application instance.
428-
│ ├── worker.py # Worker script for handling background tasks.
425+
├── app # Main application directory.
426+
│ ├── __init__.py # Initialization file for the app package.
427+
│ ├── main.py # Entry point that imports and creates the FastAPI application instance.
428+
│ ├── worker.py # Worker script for handling background tasks.
429429
│ │
430-
│ ├── api # Folder containing API-related logic.
430+
│ ├── api # Folder containing API-related logic.
431431
│ │ ├── __init__.py
432-
│ │ ├── dependencies.py # Defines dependencies that can be reused across the API endpoints.
433-
│ │ ├── exceptions.py # Contains custom exceptions for the API.
434-
│ │ ├── paginated.py # Utilities for paginated responses in APIs.
432+
│ │ ├── dependencies.py # Defines dependencies that can be reused across the API endpoints.
433+
│ │ ├── exceptions.py # Contains custom exceptions for the API.
434+
│ │ ├── paginated.py # Utilities for paginated responses in APIs.
435435
│ │ │
436-
│ │ └── v1 # Version 1 of the API.
436+
│ │ └── v1 # Version 1 of the API.
437437
│ │ ├── __init__.py
438-
│ │ ├── login.py # API routes related to user login.
439-
│ │ ├── posts.py # API routes related to posts.
440-
│ │ ├── rate_limits.py # API routes for rate limiting features.
441-
│ │ ├── tasks.py # API routes related to background tasks.
442-
│ │ ├── tiers.py # API routes for handling different user tiers.
443-
│ │ └── users.py # API routes related to user management.
438+
│ │ ├── login.py # API routes related to user login.
439+
│ │ ├── logout.py # API routes related to user logout (token blacklist).
440+
│ │ ├── posts.py # API routes related to posts.
441+
│ │ ├── rate_limits.py # API routes for rate limiting features.
442+
│ │ ├── tasks.py # API routes related to background tasks.
443+
│ │ ├── tiers.py # API routes for handling different user tiers.
444+
│ │ └── users.py # API routes related to user management.
444445
│ │
445-
│ ├── core # Core utilities and configurations for the application.
446+
│ ├── core # Core utilities and configurations for the application.
446447
│ │ ├── __init__.py
447-
│ │ ├── cache.py # Utilities related to caching.
448-
│ │ ├── config.py # Application configuration settings.
449-
│ │ ├── database.py # Database connectivity and session management.
450-
│ │ ├── exceptions.py # Contains core custom exceptions for the application.
451-
│ │ ├── logger.py # Logging utilities.
452-
│ │ ├── models.py # Base models for the application.
453-
│ │ ├── queue.py # Utilities related to task queues.
454-
│ │ ├── rate_limit.py # Rate limiting utilities and configurations.
455-
│ │ ├── security.py # Security utilities like password hashing and token generation.
456-
│ │ └── setup.py # File defining settings and FastAPI application instance definition.
448+
│ │ ├── cache.py # Utilities related to caching.
449+
│ │ ├── config.py # Application configuration settings.
450+
│ │ ├── database.py # Database connectivity and session management.
451+
│ │ ├── exceptions.py # Contains core custom exceptions for the application.
452+
│ │ ├── logger.py # Logging utilities.
453+
│ │ ├── models.py # Base models for the application.
454+
│ │ ├── queue.py # Utilities related to task queues.
455+
│ │ ├── rate_limit.py # Rate limiting utilities and configurations.
456+
│ │ ├── security.py # Security utilities like password hashing and token generation.
457+
│ │ └── setup.py # File defining settings and FastAPI application instance definition.
457458
│ │
458-
│ ├── crud # CRUD operations for the application.
459+
│ ├── crud # CRUD operations for the application.
459460
│ │ ├── __init__.py
460-
│ │ ├── crud_base.py # Base CRUD operations class that can be extended by other CRUD modules.
461-
│ │ ├── crud_posts.py # CRUD operations for posts.
462-
│ │ ├── crud_rate_limit.py # CRUD operations for rate limiting configurations.
463-
│ │ ├── crud_tier.py # CRUD operations for user tiers.
464-
│ │ ├── crud_users.py # CRUD operations for users.
465-
│ │ └── helper.py # Helper functions for CRUD operations.
461+
│ │ ├── crud_base.py # Base CRUD operations class that can be extended by other CRUD modules.
462+
│ │ ├── crud_posts.py # CRUD operations for posts.
463+
│ │ ├── crud_rate_limit.py # CRUD operations for rate limiting configurations.
464+
│ │ ├── crud_tier.py # CRUD operations for user tiers.
465+
│ │ ├── crud_token_blaclist.py # CRUD operations for token blacklist.
466+
│ │ ├── crud_users.py # CRUD operations for users.
467+
│ │ └── helper.py # Helper functions for CRUD operations.
466468
│ │
467-
│ ├── models # ORM models for the application.
469+
│ ├── models # ORM models for the application.
468470
│ │ ├── __init__.py
469-
│ │ ├── post.py # ORM model for posts.
470-
│ │ ├── rate_limit.py # ORM model for rate limiting configurations.
471-
│ │ ├── tier.py # ORM model for user tiers.
472-
│ │ └── user.py # ORM model for users.
471+
│ │ ├── post.py # ORM model for posts.
472+
│ │ ├── rate_limit.py # ORM model for rate limiting configurations.
473+
│ │ ├── tier.py # ORM model for user tiers.
474+
│ │ ├── token_blacklist.py # ORM model for token blacklist.
475+
│ │ └── user.py # ORM model for users.
473476
│ │
474-
│ ├── schemas # Pydantic schemas for data validation.
477+
│ ├── schemas # Pydantic schemas for data validation.
475478
│ │ ├── __init__.py
476-
│ │ ├── job.py # Schemas related to background jobs.
477-
│ │ ├── post.py # Schemas related to posts.
478-
│ │ ├── rate_limit.py # Schemas for rate limiting configurations.
479-
│ │ ├── tier.py # Schemas for user tiers.
480-
│ │ └── user.py # Schemas related to users.
479+
│ │ ├── job.py # Schemas related to background jobs.
480+
│ │ ├── post.py # Schemas related to posts.
481+
│ │ ├── rate_limit.py # Schemas for rate limiting configurations.
482+
│ │ ├── tier.py # Schemas for user tiers.
483+
│ │ ├── token_blacklist.py # Schemas for token blacklist.
484+
│ │ └── user.py # Schemas related to users.
481485
│ │
482-
│ └── logs # Directory for log files.
483-
│ └── app.log # Application log file.
486+
│ └── logs # Directory for log files.
487+
│ └── app.log # Application log file.
484488
485-
├── migrations # Directory for Alembic migrations.
486-
│ ├── README # General info and guidelines for migrations.
487-
│ ├── env.py # Environment configurations for Alembic.
488-
│ ├── script.py.mako # Template script for migration generation.
489+
├── migrations # Directory for Alembic migrations.
490+
│ ├── README # General info and guidelines for migrations.
491+
│ ├── env.py # Environment configurations for Alembic.
492+
│ ├── script.py.mako # Template script for migration generation.
489493
│ │
490-
│ └── versions # Folder containing individual migration scripts.
494+
│ └── versions # Folder containing individual migration scripts.
491495
│ └── README.MD
492496
493-
├── scripts # Utility scripts for the project.
497+
├── scripts # Utility scripts for the project.
494498
│ ├── __init__.py
495-
│ ├── create_first_superuser.py # Script to create the first superuser in the application.
496-
│ └── create_first_tier.py # Script to create the first user tier in the application.
499+
│ ├── create_first_superuser.py # Script to create the first superuser in the application.
500+
│ └── create_first_tier.py # Script to create the first user tier in the application.
497501
498-
└── tests # Directory containing all the tests.
499-
├── __init__.py # Initialization file for the tests package.
500-
├── conftest.py # Configuration and fixtures for pytest.
501-
├── helper.py # Helper functions for writing tests.
502-
└── test_user.py # Tests related to the user model and endpoints.
502+
└── tests # Directory containing all the tests.
503+
├── __init__.py # Initialization file for the tests package.
504+
├── conftest.py # Configuration and fixtures for pytest.
505+
├── helper.py # Helper functions for writing tests.
506+
└── test_user.py # Tests related to the user model and endpoints.
503507
```
504508

505509
### 5.2 Database Model
506-
Create the new entities and relationships and add them to the model
507-
![diagram](https://user-images.githubusercontent.com/43156212/282272311-c7a36e26-dcd0-42cf-939d-6434b5579f29.png)
510+
Create the new entities and relationships and add them to the model <br>
511+
![diagram](https://user-images.githubusercontent.com/43156212/284426387-bdafc637-0473-4b71-890d-29e79da288cf.png)
512+
513+
#### 5.2.1 Token Blacklist
514+
Note that this table is used to blacklist the `JWT` tokens (it's how you log a user out) <br>
515+
![diagram](https://user-images.githubusercontent.com/43156212/284426382-b2f3c0ca-b8ea-4f20-b47e-de1bad2ca283.png)
508516

509517
### 5.3 SQLAlchemy Models
510518
Inside `app/models`, create a new `entity.py` for each new entity (replacing entity with the name) and define the attributes according to [SQLAlchemy 2.0 standards](https://docs.sqlalchemy.org/en/20/orm/mapping_styles.html#orm-mapping-styles):

src/app/api/dependencies.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
async def get_current_user(
3232
token: Annotated[str, Depends(oauth2_scheme)],
3333
db: Annotated[AsyncSession, Depends(async_get_db)]
34-
) -> User:
34+
) -> dict:
3535
try:
3636
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
3737
username_or_email: str = payload.get("sub")
@@ -56,7 +56,7 @@ async def get_current_user(
5656
async def get_current_user(
5757
token: Annotated[str, Depends(oauth2_scheme)],
5858
db: Annotated[AsyncSession, Depends(async_get_db)]
59-
) -> User:
59+
) -> dict:
6060
token_data = await verify_token(token, db)
6161
if token_data is None:
6262
raise credentials_exception
@@ -75,7 +75,7 @@ async def get_current_user(
7575
async def get_optional_user(
7676
request: Request,
7777
db: AsyncSession = Depends(async_get_db)
78-
) -> User | None:
78+
) -> dict | None:
7979
token = request.headers.get("Authorization")
8080
if not token:
8181
return None
@@ -101,7 +101,7 @@ async def get_optional_user(
101101
return None
102102

103103

104-
async def get_current_superuser(current_user: Annotated[User, Depends(get_current_user)]) -> User:
104+
async def get_current_superuser(current_user: Annotated[User, Depends(get_current_user)]) -> dict:
105105
if not current_user["is_superuser"]:
106106
raise privileges_exception
107107

src/app/crud/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from app.core.database import Base
66

7-
def _extract_matching_columns_from_schema(model: Type[Base], schema: Union[Type[BaseModel], List, None]) -> List[Any]:
7+
def _extract_matching_columns_from_schema(model: Type[Base], schema: Union[Type[BaseModel], list, None]) -> List[Any]:
88
"""
99
Retrieves a list of ORM column objects from a SQLAlchemy model that match the field names in a given Pydantic schema.
1010
@@ -46,7 +46,7 @@ def _extract_matching_columns_from_kwargs(model: Type[Base], kwargs: dict) -> Li
4646
return column_list
4747

4848

49-
def _extract_matching_columns_from_column_names(model: Type[Base], column_names: List) -> List[Any]:
49+
def _extract_matching_columns_from_column_names(model: Type[Base], column_names: list) -> List[Any]:
5050
column_list = []
5151
for column_name in column_names:
5252
if hasattr(model, column_name):

0 commit comments

Comments
 (0)