mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-23 06:45:22 -07:00
refactor: set scaled_amount in request body
This commit is contained in:
parent
628d8533b8
commit
b94eb30ebc
5 changed files with 17 additions and 15 deletions
|
@ -6,7 +6,7 @@ const prefix = "/api";
|
||||||
const routes = {
|
const routes = {
|
||||||
groupRecipeActions: `${prefix}/households/recipe-actions`,
|
groupRecipeActions: `${prefix}/households/recipe-actions`,
|
||||||
groupRecipeActionsId: (id: string | number) => `${prefix}/households/recipe-actions/${id}`,
|
groupRecipeActionsId: (id: string | number) => `${prefix}/households/recipe-actions/${id}`,
|
||||||
groupRecipeActionsIdTriggerRecipeSlug: (id: string | number, recipeSlug: string, scaledAmount: number) => `${prefix}/households/recipe-actions/${id}/trigger/${recipeSlug}/${scaledAmount}`,
|
groupRecipeActionsIdTriggerRecipeSlug: (id: string | number, recipeSlug: string) => `${prefix}/households/recipe-actions/${id}/trigger/${recipeSlug}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
export class GroupRecipeActionsAPI extends BaseCRUDAPI<CreateGroupRecipeAction, GroupRecipeActionOut> {
|
export class GroupRecipeActionsAPI extends BaseCRUDAPI<CreateGroupRecipeAction, GroupRecipeActionOut> {
|
||||||
|
@ -14,6 +14,6 @@ const routes = {
|
||||||
itemRoute = routes.groupRecipeActionsId;
|
itemRoute = routes.groupRecipeActionsId;
|
||||||
|
|
||||||
async triggerAction(id: string | number, recipeSlug: string, scaledAmount: number) {
|
async triggerAction(id: string | number, recipeSlug: string, scaledAmount: number) {
|
||||||
return await this.requests.post(routes.groupRecipeActionsIdTriggerRecipeSlug(id, recipeSlug, scaledAmount), {});
|
return await this.requests.post(routes.groupRecipeActionsIdTriggerRecipeSlug(id, recipeSlug), {scaledAmount});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status
|
from fastapi import APIRouter, BackgroundTasks, Body, Depends, HTTPException, status
|
||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
from pydantic import UUID4
|
from pydantic import UUID4
|
||||||
|
|
||||||
|
@ -67,8 +67,10 @@ class GroupRecipeActionController(BaseUserController):
|
||||||
# ==================================================================================================================
|
# ==================================================================================================================
|
||||||
# Actions
|
# Actions
|
||||||
|
|
||||||
@router.post("/{item_id}/trigger/{recipe_slug}/{scaled_amount}", status_code=202)
|
@router.post("/{item_id}/trigger/{recipe_slug}", status_code=202)
|
||||||
def trigger_action(self, item_id: UUID4, recipe_slug: str, scaled_amount: float, bg_tasks: BackgroundTasks) -> None:
|
def trigger_action(
|
||||||
|
self, item_id: UUID4, recipe_slug: str, bg_tasks: BackgroundTasks, scaled_amount: float = Body(..., embed=True)
|
||||||
|
) -> None:
|
||||||
recipe_action = self.repos.group_recipe_actions.get_one(item_id)
|
recipe_action = self.repos.group_recipe_actions.get_one(item_id)
|
||||||
if not recipe_action:
|
if not recipe_action:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
@ -93,12 +95,10 @@ class GroupRecipeActionController(BaseUserController):
|
||||||
detail=ErrorResponse.respond(message="Not found."),
|
detail=ErrorResponse.respond(message="Not found."),
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
payload = GroupRecipeActionPayload(
|
payload = GroupRecipeActionPayload(action=recipe_action, content=recipe, scaled_amount=scaled_amount)
|
||||||
action=recipe_action, content=recipe, scaled_amount=scaled_amount
|
|
||||||
).model_dump()
|
|
||||||
bg_tasks.add_task(
|
bg_tasks.add_task(
|
||||||
task_action,
|
task_action,
|
||||||
url=recipe_action.url,
|
url=recipe_action.url,
|
||||||
json=jsonable_encoder(payload),
|
json=jsonable_encoder(payload.model_dump()),
|
||||||
timeout=15,
|
timeout=15,
|
||||||
)
|
)
|
||||||
|
|
|
@ -44,4 +44,4 @@ class GroupRecipeActionPagination(PaginationBase):
|
||||||
class GroupRecipeActionPayload(MealieModel):
|
class GroupRecipeActionPayload(MealieModel):
|
||||||
action: GroupRecipeActionOut
|
action: GroupRecipeActionOut
|
||||||
content: Any
|
content: Any
|
||||||
scaled_amount: float | None = None
|
scaled_amount: float
|
||||||
|
|
|
@ -171,8 +171,9 @@ def test_group_recipe_actions_trigger_post(
|
||||||
recipe_slug = recipe.slug
|
recipe_slug = recipe.slug
|
||||||
|
|
||||||
response = api_client.post(
|
response = api_client.post(
|
||||||
api_routes.households_recipe_actions_item_id_trigger_recipe_slug(action_id, recipe_slug, 1.0),
|
api_routes.households_recipe_actions_item_id_trigger_recipe_slug(action_id, recipe_slug),
|
||||||
headers=unique_user.token,
|
headers=unique_user.token,
|
||||||
|
json={"scaled_amount": 1.0},
|
||||||
)
|
)
|
||||||
|
|
||||||
if missing_action or missing_recipe:
|
if missing_action or missing_recipe:
|
||||||
|
@ -187,8 +188,9 @@ def test_group_recipe_actions_trigger_invalid_type(api_client: TestClient, uniqu
|
||||||
recipe = unique_user.repos.recipes.create(new_recipe(unique_user))
|
recipe = unique_user.repos.recipes.create(new_recipe(unique_user))
|
||||||
|
|
||||||
response = api_client.post(
|
response = api_client.post(
|
||||||
api_routes.households_recipe_actions_item_id_trigger_recipe_slug(recipe_action.id, recipe.id, 1.0),
|
api_routes.households_recipe_actions_item_id_trigger_recipe_slug(recipe_action.id, recipe.id),
|
||||||
headers=unique_user.token,
|
headers=unique_user.token,
|
||||||
|
json={"scaled_amount": 1.0},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
|
|
|
@ -365,9 +365,9 @@ def households_recipe_actions_item_id(item_id):
|
||||||
return f"{prefix}/households/recipe-actions/{item_id}"
|
return f"{prefix}/households/recipe-actions/{item_id}"
|
||||||
|
|
||||||
|
|
||||||
def households_recipe_actions_item_id_trigger_recipe_slug(item_id, recipe_slug, scaled_amount):
|
def households_recipe_actions_item_id_trigger_recipe_slug(item_id, recipe_slug):
|
||||||
"""`/api/households/recipe-actions/{item_id}/trigger/{recipe_slug}/{scaled_amount}`"""
|
"""`/api/households/recipe-actions/{item_id}/trigger/{recipe_slug}`"""
|
||||||
return f"{prefix}/households/recipe-actions/{item_id}/trigger/{recipe_slug}/{scaled_amount}"
|
return f"{prefix}/households/recipe-actions/{item_id}/trigger/{recipe_slug}"
|
||||||
|
|
||||||
|
|
||||||
def households_self_recipes_recipe_slug(recipe_slug):
|
def households_self_recipes_recipe_slug(recipe_slug):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue