mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-29 05:25:30 -05:00
* fixed missing migration name * added unique constraints to all m2m tables * fixed bug trying to create duplicate tags * added more unique constraints * fixed duplicate seeder data * updated tests * fixed seed rollback error
99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
import json
|
|
import pathlib
|
|
from collections.abc import Generator
|
|
from functools import cached_property
|
|
|
|
from mealie.schema.labels import MultiPurposeLabelSave
|
|
from mealie.schema.recipe.recipe_ingredient import SaveIngredientFood, SaveIngredientUnit
|
|
from mealie.services.group_services.labels_service import MultiPurposeLabelService
|
|
|
|
from ._abstract_seeder import AbstractSeeder
|
|
from .resources import foods, labels, units
|
|
|
|
|
|
class MultiPurposeLabelSeeder(AbstractSeeder):
|
|
@cached_property
|
|
def service(self):
|
|
return MultiPurposeLabelService(self.repos, self.group_id)
|
|
|
|
def get_file(self, locale: str | None = None) -> pathlib.Path:
|
|
locale_path = self.resources / "labels" / "locales" / f"{locale}.json"
|
|
return locale_path if locale_path.exists() else labels.en_US
|
|
|
|
def load_data(self, locale: str | None = None) -> Generator[MultiPurposeLabelSave, None, None]:
|
|
file = self.get_file(locale)
|
|
|
|
seen_label_names = set()
|
|
for label in json.loads(file.read_text(encoding="utf-8")):
|
|
if label["name"] in seen_label_names:
|
|
continue
|
|
|
|
seen_label_names.add(label["name"])
|
|
yield MultiPurposeLabelSave(
|
|
name=label["name"],
|
|
group_id=self.group_id,
|
|
)
|
|
|
|
def seed(self, locale: str | None = None) -> None:
|
|
self.logger.info("Seeding MultiPurposeLabel")
|
|
for label in self.load_data(locale):
|
|
try:
|
|
self.service.create_one(label)
|
|
except Exception as e:
|
|
self.logger.error(e)
|
|
|
|
|
|
class IngredientUnitsSeeder(AbstractSeeder):
|
|
def get_file(self, locale: str | None = None) -> pathlib.Path:
|
|
locale_path = self.resources / "units" / "locales" / f"{locale}.json"
|
|
return locale_path if locale_path.exists() else units.en_US
|
|
|
|
def load_data(self, locale: str | None = None) -> Generator[SaveIngredientUnit, None, None]:
|
|
file = self.get_file(locale)
|
|
|
|
seen_unit_names = set()
|
|
for unit in json.loads(file.read_text(encoding="utf-8")).values():
|
|
if unit["name"] in seen_unit_names:
|
|
continue
|
|
|
|
seen_unit_names.add(unit["name"])
|
|
yield SaveIngredientUnit(
|
|
group_id=self.group_id,
|
|
name=unit["name"],
|
|
description=unit["description"],
|
|
abbreviation=unit["abbreviation"],
|
|
)
|
|
|
|
def seed(self, locale: str | None = None) -> None:
|
|
self.logger.info("Seeding Ingredient Units")
|
|
for unit in self.load_data(locale):
|
|
try:
|
|
self.repos.ingredient_units.create(unit)
|
|
except Exception as e:
|
|
self.logger.error(e)
|
|
|
|
|
|
class IngredientFoodsSeeder(AbstractSeeder):
|
|
def get_file(self, locale: str | None = None) -> pathlib.Path:
|
|
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[SaveIngredientFood, None, None]:
|
|
file = self.get_file(locale)
|
|
|
|
seed_foods: dict[str, str] = json.loads(file.read_text(encoding="utf-8"))
|
|
for food in set(seed_foods.values()):
|
|
yield SaveIngredientFood(
|
|
group_id=self.group_id,
|
|
name=food,
|
|
description="",
|
|
)
|
|
|
|
def seed(self, locale: str | None = None) -> None:
|
|
self.logger.info("Seeding Ingredient Foods")
|
|
for food in self.load_data(locale):
|
|
try:
|
|
self.repos.ingredient_foods.create(food)
|
|
except Exception as e:
|
|
self.logger.error(e)
|