Fix/fix slug names (#1026)
* fix food seeder to use value instead of keys * fix all recipe names being slugified * add helper path utilities * add fix script for database to rename foods * add safe calling for fixes
This commit is contained in:
parent
de6fd9472d
commit
2d1ef7173d
8 changed files with 57 additions and 3 deletions
27
mealie/db/fixes/fix_slug_foods.py
Normal file
27
mealie/db/fixes/fix_slug_foods.py
Normal file
|
@ -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)
|
|
@ -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()
|
||||
|
|
0
mealie/repos/seed/resources/__init__.py
Normal file
0
mealie/repos/seed/resources/__init__.py
Normal file
5
mealie/repos/seed/resources/foods/__init__.py
Normal file
5
mealie/repos/seed/resources/foods/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from pathlib import Path
|
||||
|
||||
CWD = Path(__file__).parent
|
||||
|
||||
en_us = CWD / "en-us.json"
|
5
mealie/repos/seed/resources/labels/__init__.py
Normal file
5
mealie/repos/seed/resources/labels/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from pathlib import Path
|
||||
|
||||
CWD = Path(__file__).parent
|
||||
|
||||
en_us = CWD / "en-us.json"
|
5
mealie/repos/seed/resources/units/__init__.py
Normal file
5
mealie/repos/seed/resources/units/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from pathlib import Path
|
||||
|
||||
CWD = Path(__file__).parent
|
||||
|
||||
en_us = CWD / "en-us.json"
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue