Skip to content
This repository was archived by the owner on Sep 28, 2023. It is now read-only.

Commit 7d5f0ff

Browse files
committed
updated fastapi to 0.65.2 (bug fix), also updated more used libraries. Added isort to the project, which is used to have beautiful sorted imports. updated readme and exported requirements.txt
1 parent b8b6641 commit 7d5f0ff

27 files changed

Lines changed: 199 additions & 201 deletions

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"python.formatting.provider": "black",
3-
"python.analysis.extraPaths": ["./fastapi_plan/template/{{cookiecutter.project_name}}"]
3+
"python.analysis.extraPaths": [
4+
"./fastapi_plan/template/{{cookiecutter.project_name}}"
5+
],
6+
"python.pythonPath": ".venv\\Scripts\\python.exe"
47
}

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastapi-plan)
44
![tests](https://github.com/rafsaf/fastapi-plan/actions/workflows/tests.yml/badge.svg)
55

6-
dead simple but powerful template manager for FastAPI applications.
6+
Dead simple but powerful template manager for FastAPI applications.
77

88
- [About](#about)
99
- [Quickstart](#quickstart)
@@ -17,7 +17,8 @@ dead simple but powerful template manager for FastAPI applications.
1717

1818
## About
1919

20-
features:
20+
Features:
21+
2122
- postgresql database with [Tortoise ORM](https://tortoise-orm.readthedocs.io/en/latest/index.html) as ORM
2223
- well organised, rock solid project structure (see section [Project structure](#project-structure))
2324
- ready-to-use user model, authentiaction system (JWT), hashing with Bcrypt
@@ -29,12 +30,12 @@ features:
2930
- poetry or pip
3031
- deployment ready docker-compose.prod.yml file with poetry, you will only need own domain
3132

32-
furthermore:
33+
Furthermore:
34+
3335
- full [project structure schema](#project-structure)
34-
- [high level overview](#high-level-overview) how this project is organised and why, questions like where do the settings live or what every variable in `.env` file
36+
- [high level overview](#high-level-overview) how this project is organised and why, questions like where do the settings live or what every variable in `.env` file is used for
3537
- step by step explanation [how to add new endpoint](#how-to-add-new-endpoint), from creating new model, adding schemas and routes to migrating database and writting tests (it's always better to have it and optionally adopt it, than wasting time trying to figure out the best dev path)
3638

37-
3839
## Quickstart
3940

4041
NOTE: you will need [docker](https://www.docker.com/get-started) and optional but recommended [poetry](https://python-poetry.org/docs/) installed!
@@ -66,7 +67,6 @@ pip install -r requirements.txt
6667

6768
### 1. DEVELOPMENT
6869

69-
7070
since we wanna use uvicorn in development, create only postgres container using docker-compose.yml file like that:
7171

7272
```bash
@@ -100,7 +100,6 @@ The diffrence between development approach is that web server automatically runs
100100
2. `FIRST_SUPER_USER_EMAIL` - first account email
101101
3. `DEBUG` - when it's false, the `POSTGRES_SERVER` is set to `localhost` for development, so change it to `DEBUG=true` to use `db` postgres server.
102102

103-
104103
### 3. PRODUCTION (https, own domain)
105104

106105
To make it available from https://your_domain.com on VM run
@@ -164,15 +163,15 @@ Plesae also note that to get no-test certificate, you should comment line `"--ce
164163

165164
## High level overview
166165

167-
This project strucutre is mostly based on the [official template](#https://github.com/tiangolo/full-stack-fastapi-postgresql) (but not only) which is really great but unfortunatly does not support Tortoise ORM and is... (too?) complicated. All the security or problematic stuff (`app/core/security.py` with `verify_password` function, login and token routes, JWT token schemas) are just copied from there, so you can be preety sure it will work as expected.
166+
This project strucutre is mostly based on the [official template](#https://github.com/tiangolo/full-stack-fastapi-postgresql) (but not only) which is really great but unfortunatly does not support Tortoise ORM and is... (too?) complicated. All the security or problematic stuff (`app/core/security.py` with `verify_password` function, login and token routes, JWT token schemas) are just copied from there, so you can be pretty sure it will work as expected.
168167

169168
The main thougts are:
170169

171-
- There two sorts of settings, first one located in `.env` file for the ENTIRE project, and python-specific settings which lives in `app/core/config.py`, the file is based on pydantic solution (using dotenv lib). Why? Well, that's simple, this is due to [12factor methodology](https://12factor.net/), python-specific settings inherit from `.env` file, so this is the only place where you actually change something. If you have any problems understanding mentioned `config.py` file, just refer to [pydantic - settings management](https://pydantic-docs.helpmanual.io/usage/settings/), it's preety clear.
170+
- There two sorts of settings, first one located in `.env` file for the ENTIRE project, and python-specific settings which lives in `app/core/config.py`, the file is based on pydantic solution (using dotenv lib). Why? Well, that's simple, this is due to [12factor methodology](https://12factor.net/), python-specific settings inherit from `.env` file, so this is the only place where you actually change something. If you have any problems understanding mentioned `config.py` file, just refer to [pydantic - settings management](https://pydantic-docs.helpmanual.io/usage/settings/), it's pretty clear.
172171

173172
- Models, crud, schemas, api routes, tests... it might be confusing how to actually ADD SOMETHING NEW here, but after following next section (learn by doing, step by step), it should be pretty easy
174173

175-
- Database-related stuff is very convinient, taken mostly from [Tortoise ORM](https://tortoise-orm.readthedocs.io/en/latest/index.html) docs and just *working*. There is `register_tortoise` function in `main.py`, `TORTOISE_ORM` variable in `app/core/config.py`. Please, be aware that if you don't run `initial_data.py` SOMEHOW (in development- you have to do it yourself, in debug/production it is handled by shell script `initial.sh`, which also runs tests and migrations), you won't be able to connect to database. `initial_data.py` is hearbly based on the same named file in **official template** mentioned earlier. It has two responsibilities, first is running `init` function from Tortoise to initialize connection, and the second - creating first superuser (defined in `.env`) if one doesn't yet exists.
174+
- Database-related stuff is very convinient, taken mostly from [Tortoise ORM](https://tortoise-orm.readthedocs.io/en/latest/index.html) docs and just _working_. There is `register_tortoise` function in `main.py`, `TORTOISE_ORM` variable in `app/core/config.py`. Please, be aware that if you don't run `initial_data.py` SOMEHOW (in development- you have to do it yourself, in debug/production it is handled by shell script `initial.sh`, which also runs tests and migrations), you won't be able to connect to database. `initial_data.py` is hearbly based on the same named file in **official template** mentioned earlier. It has two responsibilities, first is running `init` function from Tortoise to initialize connection, and the second - creating first superuser (defined in `.env`) if one doesn't yet exists.
176175

177176
- Migrations are also provided by Tortiose (the tool is aerich), docs can be found [here in aerich repo](https://github.com/tortoise/aerich). The default migration (default user model) file is already included. After changes in models (e.g. new model `Cars`), just run `aerich migrate`, `aerich upgrade` and you are good to go.
178177

@@ -287,7 +286,6 @@ dog = CRUDDog(Dog)
287286
from .crud_dog import dog # type: ignore
288287
```
289288

290-
291289
8. Create `dogs.py` with endpoints in `app/api/routers` folder
292290

293291
```python
@@ -494,4 +492,3 @@ def test_remove_all_user_dogs(event_loop: EventLoop, normal_user: models.User):
494492
```
495493

496494
13. And then `test_dogs.py` for endpoints in `app/tests/api` folder
497-

fastapi_plan/template/{{cookiecutter.project_name}}/app/api/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from app.api.routers import login, users
44

5-
65
api_router = APIRouter()
76
api_router.include_router(login.router, prefix="/login", tags=["login"])
87
api_router.include_router(users.router, prefix="/users", tags=["users"])

fastapi_plan/template/{{cookiecutter.project_name}}/app/api/deps.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from app.core import security
88
from app.core.config import settings
99

10-
1110
reusable_oauth2 = OAuth2PasswordBearer(
1211
tokenUrl=f"{settings.API_STR}/login/access-token"
1312
)

fastapi_plan/template/{{cookiecutter.project_name}}/app/api/routers/login.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from app.core import security
99
from app.core.config import settings
1010

11-
1211
router = APIRouter()
1312

1413

fastapi_plan/template/{{cookiecutter.project_name}}/app/api/routers/users.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import APIRouter, Depends, HTTPException, status
2+
23
from app import crud, models, schemas
34
from app.api import deps
45

fastapi_plan/template/{{cookiecutter.project_name}}/app/core/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from typing import Dict, List, Optional, Union
2-
from pydantic import AnyHttpUrl, BaseSettings, AnyUrl, validator, EmailStr
31
from pathlib import Path
2+
from typing import Dict, List, Optional, Union
3+
4+
from pydantic import AnyHttpUrl, AnyUrl, BaseSettings, EmailStr, validator
45

56
PROJECT_DIR = Path(__file__).parent.parent.parent
67

fastapi_plan/template/{{cookiecutter.project_name}}/app/crud/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Any, Dict, Generic, Optional, Type, TypeVar, Union
2-
from tortoise.models import Model
2+
33
from fastapi.encoders import jsonable_encoder
44
from pydantic import BaseModel
5-
from tortoise.queryset import QuerySet
65
from tortoise.exceptions import DoesNotExist
6+
from tortoise.models import Model
7+
from tortoise.queryset import QuerySet
78

89
ModelType = TypeVar("ModelType", bound=Model)
910
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)

fastapi_plan/template/{{cookiecutter.project_name}}/app/crud/crud_user.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Optional
22

3+
from tortoise.exceptions import DoesNotExist
4+
5+
from app import models, schemas
36
from app.core.security import get_password_hash, verify_password
47
from app.crud.base import CRUDBase
5-
from app import models, schemas
6-
from tortoise.exceptions import DoesNotExist
78

89

910
class CRUDUser(CRUDBase[models.User, schemas.UserCreateMe, schemas.UserUpdateMe]):

fastapi_plan/template/{{cookiecutter.project_name}}/app/initial_data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import logging
2-
from tortoise import run_async
3-
from tenacity import retry, after_log, before_log, retry, wait_fixed, stop_after_attempt
4-
from tortoise import Tortoise
2+
3+
from tenacity import (after_log, before_log, retry, stop_after_attempt,
4+
wait_fixed)
5+
from tortoise import Tortoise, run_async
56

67
try:
78
import app
89
except ModuleNotFoundError:
9-
import sys
1010
import os
1111
import pathlib
12+
import sys
1213

1314
app = pathlib.Path(os.path.dirname(__file__)).parent
1415
sys.path.append(str(app))
1516
from app import crud, schemas
16-
from app.core.config import settings
17-
from app.core.config import TORTOISE_ORM
17+
from app.core.config import TORTOISE_ORM, settings
1818

1919
logging.basicConfig(level=logging.INFO)
2020
logger = logging.getLogger(__name__)

0 commit comments

Comments
 (0)