diff --git a/mealie/db/fixes/fix_slug_foods.py b/mealie/db/fixes/fix_slug_foods.py new file mode 100644 index 00000000..85e864c3 --- /dev/null +++ b/mealie/db/fixes/fix_slug_foods.py @@ -0,0 +1,27 @@ +import json + +from mealie.core import root_logger +from mealie.repos.repository_factory import AllRepositories +from mealie.repos.seed.resources import foods as food_resources + + +def fix_slug_food_names(db: AllRepositories): + check_for_food = "dairy-products-and-dairy-substitutes" + + food = db.ingredient_foods.get_one(check_for_food, "name") + + logger = root_logger.get_logger("init_db") + + if not food: + logger.info("No food found with slug: '{}' skipping fix".format(check_for_food)) + return + + all_foods = db.ingredient_foods.get_all() + + seed_foods: dict[str, str] = json.loads(food_resources.en_us.read_text()) + + for food in all_foods: + if food.name in seed_foods: + food.name = seed_foods[food.name] + logger.info("Updating food: {}".format(food.name)) + db.ingredient_foods.update(food.id, food) diff --git a/mealie/db/init_db.py b/mealie/db/init_db.py index d1a5e74d..0c519013 100644 --- a/mealie/db/init_db.py +++ b/mealie/db/init_db.py @@ -1,4 +1,5 @@ from pathlib import Path +from typing import Callable from sqlalchemy import engine @@ -8,6 +9,7 @@ from alembic.runtime import migration from mealie.core import root_logger from mealie.core.config import get_app_settings from mealie.db.db_setup import create_session +from mealie.db.fixes.fix_slug_foods import fix_slug_food_names from mealie.repos.all_repositories import get_repositories from mealie.repos.repository_factory import AllRepositories from mealie.repos.seed.init_users import default_user_init @@ -55,6 +57,13 @@ def db_is_at_head(alembic_cfg: config.Config) -> bool: return set(context.get_current_heads()) == set(directory.get_heads()) +def safe_try(name: str, func: Callable): + try: + func() + except Exception as e: + logger.error(f"Error calling '{name}': {e}") + + def main(): alembic_cfg = Config(str(PROJECT_DIR / "alembic.ini")) if db_is_at_head(alembic_cfg): @@ -73,6 +82,8 @@ def main(): logger.info("Database contains no users, initializing...") init_db(db) + safe_try("fix slug food names", lambda: fix_slug_food_names(db)) + if __name__ == "__main__": main() diff --git a/mealie/repos/seed/resources/__init__.py b/mealie/repos/seed/resources/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mealie/repos/seed/resources/foods/__init__.py b/mealie/repos/seed/resources/foods/__init__.py new file mode 100644 index 00000000..7977165c --- /dev/null +++ b/mealie/repos/seed/resources/foods/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +CWD = Path(__file__).parent + +en_us = CWD / "en-us.json" diff --git a/mealie/repos/seed/resources/labels/__init__.py b/mealie/repos/seed/resources/labels/__init__.py new file mode 100644 index 00000000..7977165c --- /dev/null +++ b/mealie/repos/seed/resources/labels/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +CWD = Path(__file__).parent + +en_us = CWD / "en-us.json" diff --git a/mealie/repos/seed/resources/units/__init__.py b/mealie/repos/seed/resources/units/__init__.py new file mode 100644 index 00000000..7977165c --- /dev/null +++ b/mealie/repos/seed/resources/units/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +CWD = Path(__file__).parent + +en_us = CWD / "en-us.json" diff --git a/mealie/repos/seed/seeders.py b/mealie/repos/seed/seeders.py index 6cc0ef50..87678764 100644 --- a/mealie/repos/seed/seeders.py +++ b/mealie/repos/seed/seeders.py @@ -49,7 +49,8 @@ class IngredientUnitsSeeder(AbstractSeeder): class IngredientFoodsSeeder(AbstractSeeder): def load_data(self) -> Generator[SaveIngredientFood, None, None]: file = self.resources / "foods" / "en-us.json" - for food in json.loads(file.read_text()): + foods: dict[str, str] = json.loads(file.read_text()) + for food in foods.values(): yield SaveIngredientFood( group_id=self.group_id, name=food, diff --git a/mealie/services/scraper/scraper.py b/mealie/services/scraper/scraper.py index dd2b3392..2de9aeae 100644 --- a/mealie/services/scraper/scraper.py +++ b/mealie/services/scraper/scraper.py @@ -42,14 +42,14 @@ def create_from_url(url: str) -> Recipe: try: recipe_data_service.scrape_image(new_recipe.image) - new_recipe.name = slugify(new_recipe.name) + new_recipe.slug = slugify(new_recipe.name) new_recipe.image = cache.new_key(4) except Exception as e: recipe_data_service.logger.exception(f"Error Scraping Image: {e}") new_recipe.image = "no image" if new_recipe.name is None or new_recipe.name == "": - new_recipe.name = "No Recipe Name Found - " + str(uuid4()) + new_recipe.name = f"No Recipe Name Found - {str(uuid4())}" new_recipe.slug = slugify(new_recipe.name) return new_recipe