forked from fastapi/full-stack-fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor backend, settings, DB sessions, types, configs, plugins (f…
…astapi#158) * ♻️ Refactor backend, update DB session handling * ✨ Add mypy config and plugins * ➕ Use Python-jose instead of PyJWT as it has some extra functionalities and features * ✨ Add/update scripts for test, lint, format * 🔧 Update lint and format configs * 🎨 Update import format, comments, and types * 🎨 Add types to config * ✨ Add types for all the code, and small fixes * 🎨 Use global imports to simplify exploring with Jupyter * ♻️ Import schemas and models, instead of each class * 🚚 Rename db_session to db for simplicity * 📌 Update dependencies installation for testing
- Loading branch information
Showing
59 changed files
with
545 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[flake8] | ||
max-line-length = 88 | ||
exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.mypy_cache | ||
.coverage | ||
htmlcov |
69 changes: 32 additions & 37 deletions
69
{{cookiecutter.project_slug}}/backend/app/app/api/api_v1/endpoints/items.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,99 @@ | ||
from typing import List | ||
from typing import Any, List | ||
|
||
from fastapi import APIRouter, Depends, HTTPException | ||
from sqlalchemy.orm import Session | ||
|
||
from app import crud | ||
from app.api.utils.db import get_db | ||
from app.api.utils.security import get_current_active_user | ||
from app.models.user import User as DBUser | ||
from app.schemas.item import Item, ItemCreate, ItemUpdate | ||
from app import crud, models, schemas | ||
from app.api import deps | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/", response_model=List[Item]) | ||
@router.get("/", response_model=List[schemas.Item]) | ||
def read_items( | ||
db: Session = Depends(get_db), | ||
db: Session = Depends(deps.get_db), | ||
skip: int = 0, | ||
limit: int = 100, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
current_user: models.User = Depends(deps.get_current_active_user), | ||
) -> Any: | ||
""" | ||
Retrieve items. | ||
""" | ||
if crud.user.is_superuser(current_user): | ||
items = crud.item.get_multi(db, skip=skip, limit=limit) | ||
else: | ||
items = crud.item.get_multi_by_owner( | ||
db_session=db, owner_id=current_user.id, skip=skip, limit=limit | ||
db=db, owner_id=current_user.id, skip=skip, limit=limit | ||
) | ||
return items | ||
|
||
|
||
@router.post("/", response_model=Item) | ||
@router.post("/", response_model=schemas.Item) | ||
def create_item( | ||
*, | ||
db: Session = Depends(get_db), | ||
item_in: ItemCreate, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
db: Session = Depends(deps.get_db), | ||
item_in: schemas.ItemCreate, | ||
current_user: models.User = Depends(deps.get_current_active_user), | ||
) -> Any: | ||
""" | ||
Create new item. | ||
""" | ||
item = crud.item.create_with_owner( | ||
db_session=db, obj_in=item_in, owner_id=current_user.id | ||
) | ||
item = crud.item.create_with_owner(db=db, obj_in=item_in, owner_id=current_user.id) | ||
return item | ||
|
||
|
||
@router.put("/{id}", response_model=Item) | ||
@router.put("/{id}", response_model=schemas.Item) | ||
def update_item( | ||
*, | ||
db: Session = Depends(get_db), | ||
db: Session = Depends(deps.get_db), | ||
id: int, | ||
item_in: ItemUpdate, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
item_in: schemas.ItemUpdate, | ||
current_user: models.User = Depends(deps.get_current_active_user), | ||
) -> Any: | ||
""" | ||
Update an item. | ||
""" | ||
item = crud.item.get(db_session=db, id=id) | ||
item = crud.item.get(db=db, id=id) | ||
if not item: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id): | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
item = crud.item.update(db_session=db, db_obj=item, obj_in=item_in) | ||
item = crud.item.update(db=db, db_obj=item, obj_in=item_in) | ||
return item | ||
|
||
|
||
@router.get("/{id}", response_model=Item) | ||
@router.get("/{id}", response_model=schemas.Item) | ||
def read_item( | ||
*, | ||
db: Session = Depends(get_db), | ||
db: Session = Depends(deps.get_db), | ||
id: int, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
current_user: models.User = Depends(deps.get_current_active_user), | ||
) -> Any: | ||
""" | ||
Get item by ID. | ||
""" | ||
item = crud.item.get(db_session=db, id=id) | ||
item = crud.item.get(db=db, id=id) | ||
if not item: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id): | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
return item | ||
|
||
|
||
@router.delete("/{id}", response_model=Item) | ||
@router.delete("/{id}", response_model=schemas.Item) | ||
def delete_item( | ||
*, | ||
db: Session = Depends(get_db), | ||
db: Session = Depends(deps.get_db), | ||
id: int, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
current_user: models.User = Depends(deps.get_current_active_user), | ||
) -> Any: | ||
""" | ||
Delete an item. | ||
""" | ||
item = crud.item.get(db_session=db, id=id) | ||
item = crud.item.get(db=db, id=id) | ||
if not item: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
if not crud.user.is_superuser(current_user) and (item.owner_id != current_user.id): | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
item = crud.item.remove(db_session=db, id=id) | ||
item = crud.item.remove(db=db, id=id) | ||
return item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.