diff --git a/dev/scripts/all_recipes_stress_test.py b/dev/scripts/all_recipes_stress_test.py index 4b1fb0c0d..40135a251 100644 --- a/dev/scripts/all_recipes_stress_test.py +++ b/dev/scripts/all_recipes_stress_test.py @@ -40,7 +40,9 @@ def populate_data(token): def time_request(url, headers): start = time.time() - _ = requests.get(url, headers=headers) + r = requests.get(url, headers=headers) + + print(f"Total Recipes {len(r.json())}") end = time.time() print(end - start) diff --git a/frontend/components/Domain/Recipe/RecipeChips.vue b/frontend/components/Domain/Recipe/RecipeChips.vue index 229e274c4..06cda9db7 100644 --- a/frontend/components/Domain/Recipe/RecipeChips.vue +++ b/frontend/components/Domain/Recipe/RecipeChips.vue @@ -3,15 +3,15 @@

{{ title }}

- {{ truncateText(category) }} + {{ truncateText(category.name) }} @@ -56,7 +56,7 @@ export default { return this.$store.getters.getAllTags || []; }, urlParam() { - return this.isCategory ? "category" : "tag"; + return this.isCategory ? "categories" : "tags"; }, }, methods: { diff --git a/frontend/pages/recipe/_slug.vue b/frontend/pages/recipe/_slug.vue index f92a3589d..b4ae7cac6 100644 --- a/frontend/pages/recipe/_slug.vue +++ b/frontend/pages/recipe/_slug.vue @@ -182,7 +182,7 @@ @@ -200,7 +200,7 @@ { - const includesTags = this.check(this.includeTags, recipe.tags, this.tagFilter.matchAny, this.tagFilter.exclude); + const includesTags = this.check( + this.includeTags, + recipe.tags.map((x) => x.name), + this.tagFilter.matchAny, + this.tagFilter.exclude + ); const includesCats = this.check( this.includeCategories, - recipe.recipeCategory, + recipe.recipeCategory.map((x) => x.name), this.catFilter.matchAny, this.catFilter.exclude ); diff --git a/mealie/db/data_access_layer/access_model_factory.py b/mealie/db/data_access_layer/access_model_factory.py index 1698e0242..8e54f0c94 100644 --- a/mealie/db/data_access_layer/access_model_factory.py +++ b/mealie/db/data_access_layer/access_model_factory.py @@ -81,11 +81,11 @@ class Database: @cached_property def categories(self) -> CategoryDataAccessModel: - return CategoryDataAccessModel(self.session, pk_id, Category, RecipeCategoryResponse) + return CategoryDataAccessModel(self.session, pk_slug, Category, RecipeCategoryResponse) @cached_property def tags(self) -> TagsDataAccessModel: - return TagsDataAccessModel(self.session, pk_id, Tag, RecipeTagResponse) + return TagsDataAccessModel(self.session, pk_slug, Tag, RecipeTagResponse) # ================================================================ # Site Items diff --git a/mealie/db/data_access_layer/recipe_access_model.py b/mealie/db/data_access_layer/recipe_access_model.py index d8e70de41..26f8e6495 100644 --- a/mealie/db/data_access_layer/recipe_access_model.py +++ b/mealie/db/data_access_layer/recipe_access_model.py @@ -1,4 +1,7 @@ from random import randint +from typing import Any + +from sqlalchemy.orm import joinedload from mealie.db.models.recipe.recipe import RecipeModel from mealie.db.models.recipe.settings import RecipeSettings @@ -57,3 +60,13 @@ class RecipeDataAccessModel(AccessModel[Recipe, RecipeModel]): count=count, override_schema=override_schema, ) + + def summary(self, group_id, start=0, limit=99999) -> Any: + return ( + self.session.query(RecipeModel) + .options(joinedload(RecipeModel.recipe_category), joinedload(RecipeModel.tags)) + .filter(RecipeModel.group_id == group_id) + .offset(start) + .limit(limit) + .all() + ) diff --git a/mealie/routes/recipe/recipe_crud_routes.py b/mealie/routes/recipe/recipe_crud_routes.py index 338c5e5cf..385fd3136 100644 --- a/mealie/routes/recipe/recipe_crud_routes.py +++ b/mealie/routes/recipe/recipe_crud_routes.py @@ -2,6 +2,8 @@ from zipfile import ZipFile from fastapi import Depends, File from fastapi.datastructures import UploadFile +from fastapi.encoders import jsonable_encoder +from fastapi.responses import JSONResponse from scrape_schema_recipe import scrape_url from sqlalchemy.orm.session import Session from starlette.responses import FileResponse @@ -22,7 +24,8 @@ logger = get_logger() @user_router.get("", response_model=list[RecipeSummary]) async def get_all(start=0, limit=None, service: RecipeService = Depends(RecipeService.private)): - return service.get_all(start, limit) + json_compatible_item_data = jsonable_encoder(service.get_all(start, limit)) + return JSONResponse(content=json_compatible_item_data) @user_router.post("", status_code=201, response_model=str) diff --git a/mealie/schema/recipe/recipe.py b/mealie/schema/recipe/recipe.py index 6a7340967..864bc9f75 100644 --- a/mealie/schema/recipe/recipe.py +++ b/mealie/schema/recipe/recipe.py @@ -30,6 +30,18 @@ class CreateRecipe(CamelModel): name: str +class RecipeTag(CamelModel): + name: str + slug: str + + class Config: + orm_mode = True + + +class RecipeCategory(RecipeTag): + pass + + class RecipeSummary(CamelModel): id: Optional[int] @@ -39,11 +51,18 @@ class RecipeSummary(CamelModel): name: Optional[str] slug: str = "" image: Optional[Any] + recipe_yield: Optional[str] + + total_time: Optional[str] = None + prep_time: Optional[str] = None + cook_time: Optional[str] = None + perform_time: Optional[str] = None description: Optional[str] = "" - recipe_category: Optional[list[str]] = [] - tags: Optional[list[str]] = [] + recipe_category: Optional[list[RecipeTag]] = [] + tags: Optional[list[RecipeTag]] = [] rating: Optional[int] + org_url: Optional[str] = Field(None, alias="orgURL") date_added: Optional[datetime.date] date_updated: Optional[datetime.datetime] @@ -51,31 +70,29 @@ class RecipeSummary(CamelModel): class Config: orm_mode = True - @classmethod - def getter_dict(_cls, name_orm: RecipeModel): - return { - **GetterDict(name_orm), - "recipe_category": [x.name for x in name_orm.recipe_category], - "tags": [x.name for x in name_orm.tags], - } + @validator("tags", always=True, pre=True) + def validate_tags(cats: list[Any], values): + if isinstance(cats, list) and cats and isinstance(cats[0], str): + return [RecipeTag(name=c, slug=slugify(c)) for c in cats] + return cats + + @validator("recipe_category", always=True, pre=True) + def validate_categories(cats: list[Any], values): + if isinstance(cats, list) and cats and isinstance(cats[0], str): + return [RecipeCategory(name=c, slug=slugify(c)) for c in cats] + return cats class Recipe(RecipeSummary): - recipe_yield: Optional[str] recipe_ingredient: Optional[list[RecipeIngredient]] = [] recipe_instructions: Optional[list[RecipeStep]] = [] nutrition: Optional[Nutrition] tools: Optional[list[str]] = [] - total_time: Optional[str] = None - prep_time: Optional[str] = None - perform_time: Optional[str] = None - # Mealie Specific settings: Optional[RecipeSettings] = RecipeSettings() assets: Optional[list[RecipeAsset]] = [] notes: Optional[list[RecipeNote]] = [] - org_url: Optional[str] = Field(None, alias="orgURL") extras: Optional[dict] = {} comments: Optional[list[CommentOut]] = [] @@ -110,8 +127,8 @@ class Recipe(RecipeSummary): return { **GetterDict(name_orm), # "recipe_ingredient": [x.note for x in name_orm.recipe_ingredient], - "recipe_category": [x.name for x in name_orm.recipe_category], - "tags": [x.name for x in name_orm.tags], + # "recipe_category": [x.name for x in name_orm.recipe_category], + # "tags": [x.name for x in name_orm.tags], "tools": [x.tool for x in name_orm.tools], "extras": {x.key_name: x.value for x in name_orm.extras}, } diff --git a/mealie/services/recipe/recipe_service.py b/mealie/services/recipe/recipe_service.py index cd68ef0cb..a3ee95951 100644 --- a/mealie/services/recipe/recipe_service.py +++ b/mealie/services/recipe/recipe_service.py @@ -55,12 +55,8 @@ class RecipeService(CrudHttpMixins[CreateRecipe, Recipe, Recipe], UserHttpServic # CRUD METHODS def get_all(self, start=0, limit=None): - return self.db.recipes.multi_query( - {"group_id": self.user.group_id}, - start=start, - limit=limit, - override_schema=RecipeSummary, - ) + items = self.db.recipes.summary(self.user.group_id, start=start, limit=limit) + return [RecipeSummary.construct(**x.__dict__) for x in items] def create_one(self, create_data: Union[Recipe, CreateRecipe]) -> Recipe: create_data = recipe_creation_factory(self.user, name=create_data.name, additional_attrs=create_data.dict()) diff --git a/tests/integration_tests/user_recipe_tests/test_recipe_crud.py b/tests/integration_tests/user_recipe_tests/test_recipe_crud.py index d33ce9eca..6349c5750 100644 --- a/tests/integration_tests/user_recipe_tests/test_recipe_crud.py +++ b/tests/integration_tests/user_recipe_tests/test_recipe_crud.py @@ -41,7 +41,11 @@ def test_read_update( recipe["notes"] = test_notes recipe["tools"] = ["one tool", "two tool"] - test_categories = ["one", "two", "three"] + test_categories = [ + {"name": "one", "slug": "one"}, + {"name": "two", "slug": "two"}, + {"name": "three", "slug": "three"}, + ] recipe["recipeCategory"] = test_categories response = api_client.put(recipe_url, json=recipe, headers=unique_user.token) @@ -54,7 +58,12 @@ def test_read_update( recipe = json.loads(response.text) assert recipe["notes"] == test_notes - assert recipe["recipeCategory"].sort() == test_categories.sort() + + assert len(recipe["recipeCategory"]) == len(test_categories) + + test_name = [x["name"] for x in test_categories] + for cats in zip(recipe["recipeCategory"], test_categories): + assert cats[0]["name"] in test_name @pytest.mark.parametrize("recipe_data", recipe_test_data)