feat: Add new labels and foods for en-US language

Update all locale seeding files and seeding logic to parse the new format Only add new labels, units, and foods during seeding (checking against existing names)
This commit is contained in:
Cameronwyatt 2025-01-31 21:17:43 -05:00 committed by GitHub
parent ba26378abc
commit fb08a11ffe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 46705 additions and 28356 deletions

View file

@ -0,0 +1,70 @@
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

@ -1050,8 +1050,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 200+ 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 ~2700 common foods that can be used to organize your database. Foods are translated via a community effort.",
"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.", "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.",
"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,12 +3,17 @@ 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 (
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, labels, units from .resources import foods, units
class MultiPurposeLabelSeeder(AbstractSeeder): class MultiPurposeLabelSeeder(AbstractSeeder):
@ -17,20 +22,24 @@ 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 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)
seen_label_names = set() current_label_names = {label.name for label in self.get_all_labels()}
for label in json.loads(file.read_text(encoding="utf-8")): # load from the foods locale file and remove any empty strings
if label["name"] in seen_label_names: seed_label_names = set(filter(None, json.loads(file.read_text(encoding="utf-8")).keys())) # type: set[str]
continue # only seed new labels
to_seed_labels = seed_label_names - current_label_names
seen_label_names.add(label["name"]) for label in to_seed_labels:
yield MultiPurposeLabelSave( yield MultiPurposeLabelSave(
name=label["name"], name=label,
group_id=self.repos.group_id, group_id=self.repos.group_id,
) )
@ -48,10 +57,13 @@ 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 = set() seen_unit_names = {unit.name for unit in self.get_all_units()}
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
@ -80,21 +92,32 @@ 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)
seed_foods_names = set() # get all current unique foods
for food in json.loads(file.read_text(encoding="utf-8")).values(): seen_foods_names = {food.name for food in self.get_all_foods()}
if food["name"] in seed_foods_names: for label, values in json.loads(file.read_text(encoding="utf-8")).items():
continue label_out = self.get_label(label)
seed_foods_names.add(food["name"]) for food_name, attributes in values["foods"].items():
yield SaveIngredientFood( if food_name in seen_foods_names:
group_id=self.repos.group_id, continue
name=food["name"],
plural_name=food.get("plural_name"), seen_foods_names.add(food_name)
description="", yield SaveIngredientFood(
) 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 = 214 CREATED_FOODS = 2687
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