-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add Items (crud, models, endpoints), utils, refactor (#14)
* Update CRUD utils to use types better. * Simplify Pydantic model names, from `UserInCreate` to `UserCreate`, etc. * Upgrade packages. * Add new generic "Items" models, crud utils, endpoints, and tests. To facilitate re-using them to create new functionality. As they are simple and generic (not like Users), it's easier to copy-paste and adapt them to each use case. * Update endpoints/*path operations* to simplify code and use new utilities, prefix and tags in `include_router`. * Update testing utils. * Update linting rules, relax vulture to reduce false positives. * Update migrations to include new Items. * Update project README.md with tips about how to start with backend.
- Loading branch information
Showing
32 changed files
with
426 additions
and
1,091 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
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
1,022 changes: 0 additions & 1,022 deletions
1,022
{{cookiecutter.project_slug}}/backend/app/Pipfile.lock
This file was deleted.
Oops, something went wrong.
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
9 changes: 5 additions & 4 deletions
9
{{cookiecutter.project_slug}}/backend/app/app/api/api_v1/api.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,8 +1,9 @@ | ||
from fastapi import APIRouter | ||
|
||
from app.api.api_v1.endpoints import token, user, utils | ||
from app.api.api_v1.endpoints import items, login, users, utils | ||
|
||
api_router = APIRouter() | ||
api_router.include_router(token.router) | ||
api_router.include_router(user.router) | ||
api_router.include_router(utils.router) | ||
api_router.include_router(login.router, tags=["login"]) | ||
api_router.include_router(users.router, prefix="/users", tags=["users"]) | ||
api_router.include_router(utils.router, prefix="/utils", tags=["utils"]) | ||
api_router.include_router(items.router, prefix="/items", tags=["items"]) |
102 changes: 102 additions & 0 deletions
102
{{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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from typing import 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.db_models.user import User as DBUser | ||
from app.models.item import Item, ItemCreate, ItemUpdate | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/", response_model=List[Item]) | ||
def read_items( | ||
db: Session = Depends(get_db), | ||
skip: int = 0, | ||
limit: int = 100, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
""" | ||
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 | ||
) | ||
return items | ||
|
||
|
||
@router.post("/", response_model=Item) | ||
def create_item( | ||
*, | ||
db: Session = Depends(get_db), | ||
item_in: ItemCreate, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
""" | ||
Create new item. | ||
""" | ||
item = crud.item.create(db_session=db, item_in=item_in, owner_id=current_user.id) | ||
return item | ||
|
||
|
||
@router.put("/{id}", response_model=Item) | ||
def update_item( | ||
*, | ||
db: Session = Depends(get_db), | ||
id: int, | ||
item_in: ItemUpdate, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
""" | ||
Update an item. | ||
""" | ||
item = crud.item.get(db_session=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, item=item, item_in=item_in) | ||
return item | ||
|
||
|
||
@router.get("/{id}", response_model=Item) | ||
def read_user_me( | ||
*, | ||
db: Session = Depends(get_db), | ||
id: int, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
""" | ||
Get item by ID. | ||
""" | ||
item = crud.item.get(db_session=db, id=id) | ||
if not item: | ||
raise HTTPException(status_code=400, 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) | ||
def delete_item( | ||
*, | ||
db: Session = Depends(get_db), | ||
id: int, | ||
current_user: DBUser = Depends(get_current_active_user), | ||
): | ||
""" | ||
Delete an item. | ||
""" | ||
item = crud.item.get(db_session=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) | ||
return item |
File renamed without changes.
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
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 +1 @@ | ||
from . import user | ||
from . import item, user |
Oops, something went wrong.