mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-15 04:13:11 -05:00
refactor(backend): ♻️ Update tag naming and reorganized some routes. Still WIP
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import json
|
||||
import shutil
|
||||
from shutil import copyfileobj
|
||||
from zipfile import ZipFile
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, File, Form, HTTPException, status
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, File, HTTPException, status
|
||||
from fastapi.datastructures import UploadFile
|
||||
from mealie.core.config import settings
|
||||
from mealie.core.root_logger import get_logger
|
||||
@@ -11,20 +10,19 @@ from mealie.db.database import db
|
||||
from mealie.db.db_setup import generate_session
|
||||
from mealie.routes.deps import get_current_user, is_logged_in, temporary_zip_path
|
||||
from mealie.routes.routers import UserAPIRouter
|
||||
from mealie.schema.recipe import Recipe, RecipeAsset, RecipeImageTypes, RecipeURLIn
|
||||
from mealie.schema.recipe import Recipe, RecipeImageTypes, RecipeURLIn
|
||||
from mealie.schema.recipe.recipe import CreateRecipe
|
||||
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.image.image import write_image
|
||||
from mealie.services.recipe.media import check_assets, delete_assets
|
||||
from mealie.services.scraper.scraper import create_from_url
|
||||
from scrape_schema_recipe import scrape_url
|
||||
from slugify import slugify
|
||||
from sqlalchemy.orm.session import Session
|
||||
from starlette.responses import FileResponse
|
||||
|
||||
user_router = UserAPIRouter(prefix="/api/recipes", tags=["Recipe CRUD"])
|
||||
public_router = APIRouter(prefix="/api/recipes", tags=["Recipe CRUD"])
|
||||
user_router = UserAPIRouter()
|
||||
public_router = APIRouter()
|
||||
logger = get_logger()
|
||||
|
||||
|
||||
@@ -52,27 +50,6 @@ def create_from_name(
|
||||
return recipe.slug
|
||||
|
||||
|
||||
@user_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),
|
||||
) -> str:
|
||||
""" Takes in a JSON string and loads data into the database as a new entry"""
|
||||
recipe: Recipe = db.recipes.create(session, data.dict())
|
||||
|
||||
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
|
||||
|
||||
|
||||
@user_router.post("/test-scrape-url")
|
||||
def test_parse_recipe_url(url: RecipeURLIn):
|
||||
return scrape_url(url.url)
|
||||
@@ -217,53 +194,3 @@ def delete_recipe(
|
||||
)
|
||||
|
||||
return recipe
|
||||
|
||||
|
||||
@user_router.put("/{recipe_slug}/image")
|
||||
def update_recipe_image(
|
||||
recipe_slug: str,
|
||||
image: bytes = File(...),
|
||||
extension: str = Form(...),
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Removes an existing image and replaces it with the incoming file. """
|
||||
write_image(recipe_slug, image, extension)
|
||||
new_version = db.recipes.update_image(session, recipe_slug, extension)
|
||||
|
||||
return {"image": new_version}
|
||||
|
||||
|
||||
@user_router.post("/{recipe_slug}/image")
|
||||
def scrape_image_url(
|
||||
recipe_slug: str,
|
||||
url: RecipeURLIn,
|
||||
):
|
||||
""" Removes an existing image and replaces it with the incoming file. """
|
||||
|
||||
scrape_image(url.url, recipe_slug)
|
||||
|
||||
|
||||
@user_router.post("/{recipe_slug}/assets", response_model=RecipeAsset)
|
||||
def upload_recipe_asset(
|
||||
recipe_slug: str,
|
||||
name: str = Form(...),
|
||||
icon: str = Form(...),
|
||||
extension: str = Form(...),
|
||||
file: UploadFile = File(...),
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Upload a file to store as a recipe asset """
|
||||
file_name = slugify(name) + "." + extension
|
||||
asset_in = RecipeAsset(name=name, icon=icon, file_name=file_name)
|
||||
dest = Recipe(slug=recipe_slug).asset_dir.joinpath(file_name)
|
||||
|
||||
with dest.open("wb") as buffer:
|
||||
copyfileobj(file.file, buffer)
|
||||
|
||||
if not dest.is_file():
|
||||
raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
recipe: Recipe = db.recipes.get(session, recipe_slug)
|
||||
recipe.assets.append(asset_in)
|
||||
db.recipes.update(session, recipe_slug, recipe.dict())
|
||||
return asset_in
|
||||
|
||||
Reference in New Issue
Block a user