mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-31 06:10:31 -05:00
feature/recipe-patch-improvements (#382)
* automated docs update * recipe rating component * recipe partial updates - closes #25 * use Vue.delete to update store * format * arrow functions * fix tests * format * initial context menu * localize * add confirmation dialog * context menu * fix bare exception * update line length * format all file with prettier * update changelog * download as json * update python dependencies * update javascript dependencies Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
from typing import List
|
||||
|
||||
from mealie.core.root_logger import get_logger
|
||||
from mealie.db.models.model_base import SqlAlchemyBase
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import load_only
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
class BaseDocument:
|
||||
def __init__(self) -> None:
|
||||
@@ -115,7 +118,7 @@ class BaseDocument:
|
||||
|
||||
return self.schema.from_orm(new_document)
|
||||
|
||||
def update(self, session: Session, match_value: str, new_data: str) -> BaseModel:
|
||||
def update(self, session: Session, match_value: str, new_data: dict) -> BaseModel:
|
||||
"""Update a database entry.
|
||||
Args: \n
|
||||
session (Session): Database Session
|
||||
@@ -132,8 +135,22 @@ class BaseDocument:
|
||||
session.commit()
|
||||
return self.schema.from_orm(entry)
|
||||
|
||||
def patch(self, session: Session, match_value: str, new_data: dict) -> BaseModel:
|
||||
entry = self._query_one(session=session, match_value=match_value)
|
||||
|
||||
if not entry:
|
||||
return
|
||||
|
||||
entry_as_dict = self.schema.from_orm(entry).dict()
|
||||
entry_as_dict.update(new_data)
|
||||
|
||||
return self.update(session, match_value, entry_as_dict)
|
||||
|
||||
def delete(self, session: Session, primary_key_value) -> dict:
|
||||
result = session.query(self.sql_model).filter_by(**{self.primary_key: primary_key_value}).one()
|
||||
results_as_model = self.schema.from_orm(result)
|
||||
|
||||
session.delete(result)
|
||||
session.commit()
|
||||
|
||||
return results_as_model
|
||||
|
||||
@@ -66,20 +66,15 @@ def update_recipe(
|
||||
@router.patch("/{recipe_slug}")
|
||||
def patch_recipe(
|
||||
recipe_slug: str,
|
||||
data: dict,
|
||||
data: Recipe,
|
||||
session: Session = Depends(generate_session),
|
||||
current_user=Depends(get_current_user),
|
||||
):
|
||||
""" Updates a recipe by existing slug and data. """
|
||||
|
||||
existing_entry: Recipe = db.recipes.get(session, recipe_slug)
|
||||
|
||||
entry_dict = existing_entry.dict()
|
||||
entry_dict.update(data)
|
||||
updated_entry = Recipe(**entry_dict) # ! Surely there's a better way?
|
||||
|
||||
recipe: Recipe = db.recipes.update(session, recipe_slug, updated_entry.dict())
|
||||
|
||||
recipe: Recipe = db.recipes.patch(
|
||||
session, recipe_slug, new_data=data.dict(exclude_unset=True, exclude_defaults=True)
|
||||
)
|
||||
if recipe_slug != recipe.slug:
|
||||
rename_image(original_slug=recipe_slug, new_slug=recipe.slug)
|
||||
|
||||
@@ -95,8 +90,10 @@ def delete_recipe(
|
||||
""" Deletes a recipe by slug """
|
||||
|
||||
try:
|
||||
db.recipes.delete(session, recipe_slug)
|
||||
delete_data = db.recipes.delete(session, recipe_slug)
|
||||
delete_image(recipe_slug)
|
||||
|
||||
return delete_data
|
||||
except Exception:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user