mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
add proper behavior on rename
This commit is contained in:
parent
0c6a009f45
commit
93ef4b83e2
4 changed files with 20 additions and 10 deletions
|
@ -60,7 +60,7 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||||
|
|
||||||
@validates("name")
|
@validates("name")
|
||||||
def validate_name(self, key, name):
|
def validate_name(self, key, name):
|
||||||
assert not name == ""
|
assert name != ""
|
||||||
return name
|
return name
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -92,11 +92,7 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||||
self.image = image
|
self.image = image
|
||||||
self.recipeCuisine = recipeCuisine
|
self.recipeCuisine = recipeCuisine
|
||||||
|
|
||||||
if self.nutrition:
|
self.nutrition = Nutrition(**nutrition) if self.nutrition else Nutrition()
|
||||||
self.nutrition = Nutrition(**nutrition)
|
|
||||||
else:
|
|
||||||
self.nutrition = Nutrition()
|
|
||||||
|
|
||||||
self.tools = [Tool(tool=x) for x in tools] if tools else []
|
self.tools = [Tool(tool=x) for x in tools] if tools else []
|
||||||
|
|
||||||
self.recipeYield = recipeYield
|
self.recipeYield = recipeYield
|
||||||
|
|
|
@ -7,7 +7,7 @@ from mealie.db.db_setup import generate_session
|
||||||
from mealie.routes.deps import get_current_user
|
from mealie.routes.deps import get_current_user
|
||||||
from mealie.schema.recipe import Recipe, RecipeURLIn
|
from mealie.schema.recipe import Recipe, RecipeURLIn
|
||||||
from mealie.schema.snackbar import SnackResponse
|
from mealie.schema.snackbar import SnackResponse
|
||||||
from mealie.services.image.image import IMG_OPTIONS, delete_image, read_image, write_image
|
from mealie.services.image.image import IMG_OPTIONS, delete_image, read_image, rename_image, write_image
|
||||||
from mealie.services.scraper.scraper import create_from_url
|
from mealie.services.scraper.scraper import create_from_url
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
|
@ -61,6 +61,9 @@ def update_recipe(
|
||||||
|
|
||||||
recipe: Recipe = db.recipes.update(session, recipe_slug, data.dict())
|
recipe: Recipe = db.recipes.update(session, recipe_slug, data.dict())
|
||||||
|
|
||||||
|
if recipe_slug != recipe.slug:
|
||||||
|
rename_image(original_slug=recipe_slug, new_slug=recipe.slug)
|
||||||
|
|
||||||
return recipe.slug
|
return recipe.slug
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,6 @@ def read_image(recipe_slug: str, image_type: str = "original") -> Path:
|
||||||
Returns:
|
Returns:
|
||||||
Path: [description]
|
Path: [description]
|
||||||
"""
|
"""
|
||||||
print(image_type)
|
|
||||||
recipe_slug = recipe_slug.split(".")[0] # Incase of File Name
|
recipe_slug = recipe_slug.split(".")[0] # Incase of File Name
|
||||||
recipe_image_dir = app_dirs.IMG_DIR.joinpath(recipe_slug)
|
recipe_image_dir = app_dirs.IMG_DIR.joinpath(recipe_slug)
|
||||||
|
|
||||||
|
@ -39,6 +38,18 @@ def read_image(recipe_slug: str, image_type: str = "original") -> Path:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def rename_image(original_slug, new_slug) -> Path:
|
||||||
|
current_path = app_dirs.IMG_DIR.joinpath(original_slug)
|
||||||
|
new_path = app_dirs.IMG_DIR.joinpath(new_slug)
|
||||||
|
|
||||||
|
try:
|
||||||
|
new_path = current_path.rename(new_path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
logger.error(f"Image Directory {original_slug} Doesn't Exist")
|
||||||
|
|
||||||
|
return new_path
|
||||||
|
|
||||||
|
|
||||||
def write_image(recipe_slug: str, file_data: bytes, extension: str) -> Path.name:
|
def write_image(recipe_slug: str, file_data: bytes, extension: str) -> Path.name:
|
||||||
try:
|
try:
|
||||||
delete_image(recipe_slug)
|
delete_image(recipe_slug)
|
||||||
|
|
|
@ -2,7 +2,7 @@ import shutil
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from mealie.core.config import app_dirs
|
from mealie.core.config import app_dirs
|
||||||
from PIL import Image, UnidentifiedImageError
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
def minify_image(image_file: Path, min_dest: Path, tiny_dest: Path):
|
def minify_image(image_file: Path, min_dest: Path, tiny_dest: Path):
|
||||||
|
@ -24,7 +24,7 @@ def minify_image(image_file: Path, min_dest: Path, tiny_dest: Path):
|
||||||
tiny_image = crop_center(img)
|
tiny_image = crop_center(img)
|
||||||
tiny_image.save(tiny_dest, quality=70)
|
tiny_image.save(tiny_dest, quality=70)
|
||||||
|
|
||||||
except:
|
except Exception:
|
||||||
shutil.copy(image_file, min_dest)
|
shutil.copy(image_file, min_dest)
|
||||||
shutil.copy(image_file, tiny_dest)
|
shutil.copy(image_file, tiny_dest)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue