This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
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 (#15)
* 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. * Add full text search for items. * Update project README.md with tips about how to start with backend.
- Loading branch information
Showing
32 changed files
with
801 additions
and
126 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
11 changes: 6 additions & 5 deletions
11
{{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,9 +1,10 @@ | ||
from fastapi import APIRouter | ||
|
||
from app.api.api_v1.endpoints import role, token, user, utils | ||
from app.api.api_v1.endpoints import items, login, roles, users, utils | ||
|
||
api_router = APIRouter() | ||
api_router.include_router(role.router) | ||
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(roles.router, prefix="/roles", tags=["roles"]) | ||
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"]) |
134 changes: 134 additions & 0 deletions
134
{{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,134 @@ | ||
from typing import List | ||
|
||
from fastapi import APIRouter, Depends, HTTPException | ||
|
||
from app import crud | ||
from app.api.utils.security import get_current_active_user | ||
from app.db.database import get_default_bucket | ||
from app.models.item import Item, ItemCreate, ItemUpdate | ||
from app.models.user import UserInDB | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/", response_model=List[Item]) | ||
def read_items( | ||
skip: int = 0, | ||
limit: int = 100, | ||
current_user: UserInDB = Depends(get_current_active_user), | ||
): | ||
""" | ||
Retrieve items. | ||
If superuser, all the items. | ||
If normal user, the items owned by this user. | ||
""" | ||
bucket = get_default_bucket() | ||
if crud.user.is_superuser(current_user): | ||
docs = crud.item.get_multi(bucket, skip=skip, limit=limit) | ||
else: | ||
docs = crud.item.get_multi_by_owner( | ||
bucket=bucket, owner_username=current_user.username, skip=skip, limit=limit | ||
) | ||
return docs | ||
|
||
|
||
@router.get("/search/", response_model=List[Item]) | ||
def search_items( | ||
q: str, | ||
skip: int = 0, | ||
limit: int = 100, | ||
current_user: UserInDB = Depends(get_current_active_user), | ||
): | ||
""" | ||
Search items, use Bleve Query String syntax: | ||
http://blevesearch.com/docs/Query-String-Query/ | ||
For typeahead suffix with `*`. For example, a query with: `title:foo*` will match | ||
items containing `football`, `fool proof`, etc. | ||
""" | ||
bucket = get_default_bucket() | ||
if crud.user.is_superuser(current_user): | ||
docs = crud.item.search(bucket=bucket, query_string=q, skip=skip, limit=limit) | ||
else: | ||
docs = crud.item.search_with_owner( | ||
bucket=bucket, | ||
query_string=q, | ||
username=current_user.username, | ||
skip=skip, | ||
limit=limit, | ||
) | ||
return docs | ||
|
||
|
||
@router.post("/", response_model=Item) | ||
def create_item( | ||
*, item_in: ItemCreate, current_user: UserInDB = Depends(get_current_active_user) | ||
): | ||
""" | ||
Create new item. | ||
""" | ||
bucket = get_default_bucket() | ||
id = crud.utils.generate_new_id() | ||
doc = crud.item.upsert( | ||
bucket=bucket, id=id, doc_in=item_in, owner_username=current_user.username | ||
) | ||
return doc | ||
|
||
|
||
@router.put("/{id}", response_model=Item) | ||
def update_item( | ||
*, | ||
id: str, | ||
item_in: ItemUpdate, | ||
current_user: UserInDB = Depends(get_current_active_user), | ||
): | ||
""" | ||
Update an item. | ||
""" | ||
bucket = get_default_bucket() | ||
doc = crud.item.get(bucket=bucket, id=id) | ||
if not doc: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
if not crud.user.is_superuser(current_user) and ( | ||
doc.owner_username != current_user.username | ||
): | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
doc = crud.item.update( | ||
bucket=bucket, id=id, doc_in=item_in, owner_username=doc.owner_username | ||
) | ||
return doc | ||
|
||
|
||
@router.get("/{id}", response_model=Item) | ||
def read_item(id: str, current_user: UserInDB = Depends(get_current_active_user)): | ||
""" | ||
Get item by ID. | ||
""" | ||
bucket = get_default_bucket() | ||
doc = crud.item.get(bucket=bucket, id=id) | ||
if not doc: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
if not crud.user.is_superuser(current_user) and ( | ||
doc.owner_username != current_user.username | ||
): | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
return doc | ||
|
||
|
||
@router.delete("/{id}", response_model=Item) | ||
def delete_item(id: str, current_user: UserInDB = Depends(get_current_active_user)): | ||
""" | ||
Delete an item by ID. | ||
""" | ||
bucket = get_default_bucket() | ||
doc = crud.item.get(bucket=bucket, id=id) | ||
if not doc: | ||
raise HTTPException(status_code=404, detail="Item not found") | ||
if not crud.user.is_superuser(current_user) and ( | ||
doc.owner_username != current_user.username | ||
): | ||
raise HTTPException(status_code=400, detail="Not enough permissions") | ||
doc = crud.item.remove(bucket=bucket, id=id) | ||
return doc |
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.