mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-15 12:23:12 -05:00
Feature/event notifications (#399)
* additional server events * sort 'recent recipes' by updated * remove duplicate code * fixes #396 * set color * consolidate tag/category pages * set colors * list unorganized recipes * cleanup old code * remove flash message, switch to global snackbar * cancel to close * cleanup * notifications first pass * test notification * complete notification feature * use background tasks * add url param * update documentation Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
from shutil import copyfileobj
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, HTTPException, status
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, File, Form, HTTPException, status
|
||||
from fastapi.datastructures import UploadFile
|
||||
from mealie.core.config import settings
|
||||
from mealie.core.root_logger import get_logger
|
||||
from mealie.db.database import db
|
||||
from mealie.db.db_setup import generate_session
|
||||
from mealie.routes.deps import get_current_user
|
||||
from mealie.schema.recipe import Recipe, RecipeAsset, RecipeURLIn
|
||||
from mealie.schema.user import UserInDB
|
||||
from mealie.services.events import create_recipe_event
|
||||
from mealie.services.image.image import scrape_image, write_image
|
||||
from mealie.services.recipe.media import check_assets, delete_assets
|
||||
@@ -20,6 +22,7 @@ logger = get_logger()
|
||||
|
||||
@router.post("/create", status_code=201, response_model=str)
|
||||
def create_from_json(
|
||||
background_tasks: BackgroundTasks,
|
||||
data: Recipe,
|
||||
session: Session = Depends(generate_session),
|
||||
current_user=Depends(get_current_user),
|
||||
@@ -27,22 +30,36 @@ def create_from_json(
|
||||
""" Takes in a JSON string and loads data into the database as a new entry"""
|
||||
recipe: Recipe = db.recipes.create(session, data.dict())
|
||||
|
||||
create_recipe_event("Recipe Created", f"Recipe '{recipe.name}' created", session=session)
|
||||
background_tasks.add_task(
|
||||
create_recipe_event,
|
||||
"Recipe Created (URL)",
|
||||
f"'{recipe.name}' by {current_user.full_name} \n {settings.BASE_URL}/recipe/{recipe.slug}",
|
||||
session=session,
|
||||
attachment=recipe.image_dir.joinpath("min-original.webp"),
|
||||
)
|
||||
|
||||
return recipe.slug
|
||||
|
||||
|
||||
@router.post("/create-url", status_code=201, response_model=str)
|
||||
def parse_recipe_url(
|
||||
background_tasks: BackgroundTasks,
|
||||
url: RecipeURLIn,
|
||||
session: Session = Depends(generate_session),
|
||||
current_user=Depends(get_current_user),
|
||||
current_user: UserInDB = Depends(get_current_user),
|
||||
):
|
||||
""" Takes in a URL and attempts to scrape data and load it into the database """
|
||||
|
||||
recipe = create_from_url(url.url)
|
||||
recipe: Recipe = db.recipes.create(session, recipe.dict())
|
||||
create_recipe_event("Recipe Created (URL)", f"'{recipe.name}' by {current_user.full_name}", session=session)
|
||||
|
||||
background_tasks.add_task(
|
||||
create_recipe_event,
|
||||
"Recipe Created (URL)",
|
||||
f"'{recipe.name}' by {current_user.full_name} \n {settings.BASE_URL}/recipe/{recipe.slug}",
|
||||
session=session,
|
||||
attachment=recipe.image_dir.joinpath("min-original.webp"),
|
||||
)
|
||||
|
||||
return recipe.slug
|
||||
|
||||
@@ -64,7 +81,6 @@ def update_recipe(
|
||||
""" Updates a recipe by existing slug and data. """
|
||||
|
||||
recipe: Recipe = db.recipes.update(session, recipe_slug, data.dict())
|
||||
print(recipe.assets)
|
||||
|
||||
check_assets(original_slug=recipe_slug, recipe=recipe)
|
||||
|
||||
@@ -91,6 +107,7 @@ def patch_recipe(
|
||||
|
||||
@router.delete("/{recipe_slug}")
|
||||
def delete_recipe(
|
||||
background_tasks: BackgroundTasks,
|
||||
recipe_slug: str,
|
||||
session: Session = Depends(generate_session),
|
||||
current_user=Depends(get_current_user),
|
||||
@@ -100,7 +117,12 @@ def delete_recipe(
|
||||
try:
|
||||
recipe: Recipe = db.recipes.delete(session, recipe_slug)
|
||||
delete_assets(recipe_slug=recipe_slug)
|
||||
create_recipe_event("Recipe Deleted", f"'{recipe.name}' deleted by {current_user.full_name}", session=session)
|
||||
background_tasks.add_task(
|
||||
create_recipe_event,
|
||||
"Recipe Deleted",
|
||||
f"'{recipe.name}' deleted by {current_user.full_name}",
|
||||
session=session,
|
||||
)
|
||||
return recipe
|
||||
except Exception:
|
||||
raise HTTPException(status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
Reference in New Issue
Block a user