1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-19 13:19:41 +02:00

fix: Revert "feat: Update seeding to use new foods list format - round 2" (#5208)

This commit is contained in:
Hayden 2025-03-12 23:27:18 -05:00 committed by GitHub
parent 72668e2881
commit 974d848ee2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 28347 additions and 46702 deletions

View file

@ -1,70 +0,0 @@
import glob
import json
import pathlib
def get_seed_locale_names() -> set[str]:
"""Find all locales in the seed/resources/ folder
Returns:
A set of every file name where there's both a seed label and seed food file
"""
LABELS_PATH = "/workspaces/mealie/mealie/repos/seed/resources/labels/locales/"
FOODS_PATH = "/workspaces/mealie/mealie/repos/seed/resources/foods/locales/"
label_locales = glob.glob("*.json", root_dir=LABELS_PATH)
foods_locales = glob.glob("*.json", root_dir=FOODS_PATH)
# ensure that a locale has both a label and a food seed file
return set(label_locales).intersection(foods_locales)
def get_labels_from_file(locale: str) -> list[str]:
"""Query a locale to get all of the labels so that they can be added to the new foods seed format
Returns:
All of the labels found within the seed file for a given locale
"""
locale_path = pathlib.Path("/workspaces/mealie/mealie/repos/seed/resources/labels/locales/" + locale)
label_names = [label["name"] for label in json.loads(locale_path.read_text(encoding="utf-8"))]
return label_names
def transform_foods(locale: str):
"""
Convert the current food seed file for a locale into a new format which maps each food to a label
Existing format of foods seed file is a dictionary where each key is a food name and the values are a dictionary
of attributes such as name and plural_name
New format maps each food to a label. The top-level dictionary has each key as a label e.g. "Fruits".
Each label key as a value that is a dictionary with an element called "foods"
"Foods" is a dictionary of each food for that label, with a key of the english food name e.g. "baking-soda"
and a value of attributes, including the translated name of the item e.g. "bicarbonate of soda" for en-GB.
"""
locale_path = pathlib.Path("/workspaces/mealie/mealie/repos/seed/resources/foods/locales/" + locale)
with open(locale_path, encoding="utf-8") as infile:
data = json.load(infile)
transformed_data = {"": {"foods": dict(data.items())}}
# Seeding for labels now pulls from the foods file and parses the labels from there (as top-level keys),
# thus we need to add all of the existing labels to the new food seed file and give them an empty foods dictionary
label_names = get_labels_from_file(locale)
for label in label_names:
transformed_data[label] = {"foods": {}}
with open(locale_path, "w", encoding="utf-8") as outfile:
json.dump(transformed_data, outfile, indent=4, ensure_ascii=False)
def main():
for locale in get_seed_locale_names():
transform_foods(locale)
if __name__ == "__main__":
main()

View file

@ -1048,8 +1048,8 @@
"foods": { "foods": {
"merge-dialog-text": "Combining the selected foods will merge the source food and target food into a single food. The source food will be deleted and all of the references to the source food will be updated to point to the target food.", "merge-dialog-text": "Combining the selected foods will merge the source food and target food into a single food. The source food will be deleted and all of the references to the source food will be updated to point to the target food.",
"merge-food-example": "Merging {food1} into {food2}", "merge-food-example": "Merging {food1} into {food2}",
"seed-dialog-text": "Seed the database with foods based on your local language. This will create ~2700 common foods that can be used to organize your database. Foods are translated via a community effort.", "seed-dialog-text": "Seed the database with foods based on your local language. This will create 200+ common foods that can be used to organize your database. Foods are translated via a community effort.",
"seed-dialog-warning": "You already have some items in your database. A new item will not be added if an item with the same name already exists.", "seed-dialog-warning": "You have already have some items in your database. This action will not reconcile duplicates, you will have to manage them manually.",
"combine-food": "Combine Food", "combine-food": "Combine Food",
"source-food": "Source Food", "source-food": "Source Food",
"target-food": "Target Food", "target-food": "Target Food",

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -3,17 +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 MultiPurposeLabelOut, MultiPurposeLabelSave from mealie.schema.labels import MultiPurposeLabelSave
from mealie.schema.recipe.recipe_ingredient import ( from mealie.schema.recipe.recipe_ingredient import SaveIngredientFood, SaveIngredientUnit
IngredientFood,
IngredientUnit,
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, units from .resources import foods, labels, units
class MultiPurposeLabelSeeder(AbstractSeeder): class MultiPurposeLabelSeeder(AbstractSeeder):
@ -22,24 +17,20 @@ 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:
# Get the labels from the foods seed file now locale_path = self.resources / "labels" / "locales" / f"{locale}.json"
locale_path = self.resources / "foods" / "locales" / f"{locale}.json" return locale_path if locale_path.exists() else labels.en_US
return locale_path if locale_path.exists() else foods.en_US
def get_all_labels(self) -> list[MultiPurposeLabelOut]:
return self.repos.group_multi_purpose_labels.get_all()
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)
current_label_names = {label.name for label in self.get_all_labels()} seen_label_names = set()
# load from the foods locale file and remove any empty strings for label in json.loads(file.read_text(encoding="utf-8")):
seed_label_names = set(filter(None, json.loads(file.read_text(encoding="utf-8")).keys())) # type: set[str] if label["name"] in seen_label_names:
# only seed new labels continue
to_seed_labels = seed_label_names - current_label_names
for label in to_seed_labels: seen_label_names.add(label["name"])
yield MultiPurposeLabelSave( yield MultiPurposeLabelSave(
name=label, name=label["name"],
group_id=self.repos.group_id, group_id=self.repos.group_id,
) )
@ -57,13 +48,10 @@ class IngredientUnitsSeeder(AbstractSeeder):
locale_path = self.resources / "units" / "locales" / f"{locale}.json" locale_path = self.resources / "units" / "locales" / f"{locale}.json"
return locale_path if locale_path.exists() else units.en_US return locale_path if locale_path.exists() else units.en_US
def get_all_units(self) -> list[IngredientUnit]:
return self.repos.ingredient_units.get_all()
def load_data(self, locale: str | None = None) -> Generator[SaveIngredientUnit, None, None]: def load_data(self, locale: str | None = None) -> Generator[SaveIngredientUnit, None, None]:
file = self.get_file(locale) file = self.get_file(locale)
seen_unit_names = {unit.name for unit in self.get_all_units()} seen_unit_names = set()
for unit in json.loads(file.read_text(encoding="utf-8")).values(): for unit in json.loads(file.read_text(encoding="utf-8")).values():
if unit["name"] in seen_unit_names: if unit["name"] in seen_unit_names:
continue continue
@ -92,32 +80,21 @@ 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 get_all_foods(self) -> list[IngredientFood]:
return self.repos.ingredient_foods.get_all()
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)
# get all current unique foods seed_foods_names = set()
seen_foods_names = {food.name for food in self.get_all_foods()} for food in json.loads(file.read_text(encoding="utf-8")).values():
for label, values in json.loads(file.read_text(encoding="utf-8")).items(): if food["name"] in seed_foods_names:
label_out = self.get_label(label) continue
for food_name, attributes in values["foods"].items(): seed_foods_names.add(food["name"])
if food_name in seen_foods_names: yield SaveIngredientFood(
continue group_id=self.repos.group_id,
name=food["name"],
seen_foods_names.add(food_name) plural_name=food.get("plural_name"),
yield SaveIngredientFood( description="",
group_id=self.repos.group_id, )
name=attributes["name"],
plural_name=attributes.get("plural_name"),
description="", # description expected to be empty string by UnitFoodBase class
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 = 2687 CREATED_FOODS = 214
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 = 32 CREATED_LABELS = 21
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 = 32 CREATED_LABELS = 21
database = unique_user.repos database = unique_user.repos
# create a list with some labels # create a list with some labels