Use expanded foods and labels file for seeding. Load labels from the foods seed list instead of their own. Link foods to labels when seeding.

This commit is contained in:
Cameron Wyatt 2024-12-31 19:10:32 +00:00
commit 74690f2466
4 changed files with 16363 additions and 714 deletions

File diff suppressed because it is too large Load diff

View file

@ -3,12 +3,12 @@ import pathlib
from collections.abc import Generator from collections.abc import Generator
from functools import cached_property from functools import cached_property
from mealie.schema.labels import MultiPurposeLabelSave from mealie.schema.labels import MultiPurposeLabelOut, MultiPurposeLabelSave
from mealie.schema.recipe.recipe_ingredient import SaveIngredientFood, SaveIngredientUnit from mealie.schema.recipe.recipe_ingredient import SaveIngredientFood, SaveIngredientUnit
from mealie.services.group_services.labels_service import MultiPurposeLabelService from mealie.services.group_services.labels_service import MultiPurposeLabelService
from ._abstract_seeder import AbstractSeeder from ._abstract_seeder import AbstractSeeder
from .resources import foods, labels, units from .resources import foods, units
class MultiPurposeLabelSeeder(AbstractSeeder): class MultiPurposeLabelSeeder(AbstractSeeder):
@ -17,20 +17,17 @@ class MultiPurposeLabelSeeder(AbstractSeeder):
return MultiPurposeLabelService(self.repos) return MultiPurposeLabelService(self.repos)
def get_file(self, locale: str | None = None) -> pathlib.Path: def get_file(self, locale: str | None = None) -> pathlib.Path:
locale_path = self.resources / "labels" / "locales" / f"{locale}.json" # Get the labels from the foods seed file now
return locale_path if locale_path.exists() else labels.en_US locale_path = self.resources / "foods" / "locales" / f"{locale}.json"
return locale_path if locale_path.exists() else foods.en_US
def load_data(self, locale: str | None = None) -> Generator[MultiPurposeLabelSave, None, None]: def load_data(self, locale: str | None = None) -> Generator[MultiPurposeLabelSave, None, None]:
file = self.get_file(locale) file = self.get_file(locale)
seen_label_names = set() label_names = set(json.loads(file.read_text(encoding="utf-8")).keys())
for label in json.loads(file.read_text(encoding="utf-8")): for label in label_names:
if label["name"] in seen_label_names:
continue
seen_label_names.add(label["name"])
yield MultiPurposeLabelSave( yield MultiPurposeLabelSave(
name=label["name"], name=label,
group_id=self.repos.group_id, group_id=self.repos.group_id,
) )
@ -80,21 +77,28 @@ class IngredientFoodsSeeder(AbstractSeeder):
locale_path = self.resources / "foods" / "locales" / f"{locale}.json" locale_path = self.resources / "foods" / "locales" / f"{locale}.json"
return locale_path if locale_path.exists() else foods.en_US return locale_path if locale_path.exists() else foods.en_US
def get_label(self, value: str) -> MultiPurposeLabelOut | None:
return self.repos.group_multi_purpose_labels.get_one(value, "name")
def load_data(self, locale: str | None = None) -> Generator[SaveIngredientFood, None, None]: def load_data(self, locale: str | None = None) -> Generator[SaveIngredientFood, None, None]:
file = self.get_file(locale) file = self.get_file(locale)
seed_foods_names = set() seed_foods_names = set()
for food in json.loads(file.read_text(encoding="utf-8")).values(): for label, value in json.loads(file.read_text(encoding="utf-8")).items():
if food["name"] in seed_foods_names: label_out = self.get_label(label)
continue
seed_foods_names.add(food["name"]) for food in value["foods"]:
yield SaveIngredientFood( if food["name"] in seed_foods_names:
group_id=self.repos.group_id, continue
name=food["name"],
plural_name=food.get("plural_name"), seed_foods_names.add(food["name"])
description="", yield SaveIngredientFood(
) group_id=self.repos.group_id,
name=food["name"],
plural_name=food["pluralName"],
description="",
label_id=label_out.id if label_out and label_out.id else None,
)
def seed(self, locale: str | None = None) -> None: def seed(self, locale: str | None = None) -> None:
self.logger.info("Seeding Ingredient Foods") self.logger.info("Seeding Ingredient Foods")

View file

@ -12,7 +12,7 @@ def test_seed_invalid_locale(api_client: TestClient, unique_user: TestUser):
def test_seed_foods(api_client: TestClient, unique_user: TestUser): def test_seed_foods(api_client: TestClient, unique_user: TestUser):
CREATED_FOODS = 214 CREATED_FOODS = 2694
database = unique_user.repos database = unique_user.repos
# Check that the foods was created # Check that the foods was created
@ -44,7 +44,7 @@ def test_seed_units(api_client: TestClient, unique_user: TestUser):
def test_seed_labels(api_client: TestClient, unique_user: TestUser): def test_seed_labels(api_client: TestClient, unique_user: TestUser):
CREATED_LABELS = 21 CREATED_LABELS = 32
database = unique_user.repos database = unique_user.repos
# Check that the foods was created # Check that the foods was created

View file

@ -99,7 +99,7 @@ def test_new_label_creates_list_labels_in_all_households(
def test_seed_label_creates_list_labels(api_client: TestClient, unique_user: TestUser): def test_seed_label_creates_list_labels(api_client: TestClient, unique_user: TestUser):
CREATED_LABELS = 21 CREATED_LABELS = 32
database = unique_user.repos database = unique_user.repos
# create a list with some labels # create a list with some labels