mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-23 06:45:22 -07:00
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:
parent
431638c1ed
commit
74690f2466
4 changed files with 16363 additions and 714 deletions
File diff suppressed because it is too large
Load diff
|
@ -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,11 +77,17 @@ 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():
|
||||||
|
label_out = self.get_label(label)
|
||||||
|
|
||||||
|
for food in value["foods"]:
|
||||||
if food["name"] in seed_foods_names:
|
if food["name"] in seed_foods_names:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -92,8 +95,9 @@ class IngredientFoodsSeeder(AbstractSeeder):
|
||||||
yield SaveIngredientFood(
|
yield SaveIngredientFood(
|
||||||
group_id=self.repos.group_id,
|
group_id=self.repos.group_id,
|
||||||
name=food["name"],
|
name=food["name"],
|
||||||
plural_name=food.get("plural_name"),
|
plural_name=food["pluralName"],
|
||||||
description="",
|
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:
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue