From c074d3a85ae2337effa582a186f1be1a40088fdf Mon Sep 17 00:00:00 2001 From: hay-kot Date: Mon, 3 May 2021 16:54:26 -0800 Subject: [PATCH] unorganized routes --- mealie/db/database.py | 20 ++++++++++++++++---- mealie/routes/backup_routes.py | 2 +- mealie/routes/recipe/all_recipe_routes.py | 12 +++++++++++- mealie/routes/recipe/recipe_crud_routes.py | 11 +++++++++-- mealie/routes/users/sign_up.py | 10 +++++----- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/mealie/db/database.py b/mealie/db/database.py index 60854b8fe..d94aa5dba 100644 --- a/mealie/db/database.py +++ b/mealie/db/database.py @@ -36,11 +36,23 @@ class _Recipes(BaseDocument): return f"{slug}.{extension}" - def count_uncategorized(self, session: Session) -> int: - return session.query(self.sql_model).filter(RecipeModel.recipe_category == None).count() # noqa: 711 + def count_uncategorized(self, session: Session, count=True, override_schema=None) -> int: + eff_schema = override_schema or self.schema + if count: + return session.query(self.sql_model).filter(RecipeModel.recipe_category == None).count() # noqa: 711 + else: + return [ + eff_schema.from_orm(x) for x in session.query(self.sql_model).filter(RecipeModel.tags == None).all() + ] # noqa: 711 - def count_untagged(self, session: Session) -> int: - return session.query(self.sql_model).filter(RecipeModel.tags == None).count() # noqa: 711 + def count_untagged(self, session: Session, count=True, override_schema=None) -> int: + eff_schema = override_schema or self.schema + if count: + return session.query(self.sql_model).filter(RecipeModel.tags == None).count() # noqa: 711 + else: + return [ + eff_schema.from_orm(x) for x in session.query(self.sql_model).filter(RecipeModel.tags == None).all() + ] # noqa: 711 class _Categories(BaseDocument): diff --git a/mealie/routes/backup_routes.py b/mealie/routes/backup_routes.py index 0245c6434..d4fef6c35 100644 --- a/mealie/routes/backup_routes.py +++ b/mealie/routes/backup_routes.py @@ -47,7 +47,7 @@ def export_database(data: BackupJob, session: Session = Depends(generate_session export_users=data.options.users, export_groups=data.options.groups, ) - create_backup_event("Manual Backup", f"Manual Backup Created '{Path(export_path).name}'", session) + create_backup_event("Database Backup", f"Manual Backup Created '{Path(export_path).name}'", session) return {"export_path": export_path} except Exception as e: logger.error(e) diff --git a/mealie/routes/recipe/all_recipe_routes.py b/mealie/routes/recipe/all_recipe_routes.py index 014815774..b9b3231e4 100644 --- a/mealie/routes/recipe/all_recipe_routes.py +++ b/mealie/routes/recipe/all_recipe_routes.py @@ -8,7 +8,7 @@ from sqlalchemy.orm.session import Session router = APIRouter(tags=["Query All Recipes"]) -@router.get("/api/recipes/summary") +@router.get("/api/recipes/summary", response_model=list[RecipeSummary]) async def get_recipe_summary( start=0, limit=9999, @@ -29,6 +29,16 @@ async def get_recipe_summary( return db.recipes.get_all(session, limit=limit, start=start, override_schema=RecipeSummary) +@router.get("/api/recipes/summary/untagged", response_model=list[RecipeSummary]) +async def get_untagged_recipes(session: Session = Depends(generate_session)): + return db.recipes.count_untagged(session, False, override_schema=RecipeSummary) + + +@router.get("/api/recipes/summary/uncategorized", response_model=list[RecipeSummary]) +async def get_untagged_recipes(session: Session = Depends(generate_session)): + return db.recipes.count_uncategorized(session, False, override_schema=RecipeSummary) + + @router.post("/api/recipes/category") def filter_by_category(categories: list, session: Session = Depends(generate_session)): """ pass a list of categories and get a list of recipes associated with those categories """ diff --git a/mealie/routes/recipe/recipe_crud_routes.py b/mealie/routes/recipe/recipe_crud_routes.py index 98bb58667..c7f964dcf 100644 --- a/mealie/routes/recipe/recipe_crud_routes.py +++ b/mealie/routes/recipe/recipe_crud_routes.py @@ -4,6 +4,7 @@ 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, RecipeURLIn +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 from mealie.services.scraper.scraper import create_from_url @@ -22,6 +23,8 @@ 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) + return recipe.slug @@ -35,6 +38,7 @@ def parse_recipe_url( recipe = create_from_url(url.url) recipe: Recipe = db.recipes.create(session, recipe.dict()) + create_recipe_event("Recipe Created (URL)", f"Recipe '{recipe.name}' created", session=session) return recipe.slug @@ -91,9 +95,12 @@ def delete_recipe( """ Deletes a recipe by slug """ try: - recipes = db.recipes.delete(session, recipe_slug) + recipe: Recipe = db.recipes.delete(session, recipe_slug) delete_assets(recipe_slug=recipe_slug) - return recipes + 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) diff --git a/mealie/routes/users/sign_up.py b/mealie/routes/users/sign_up.py index c62d4f204..9d6bdaf79 100644 --- a/mealie/routes/users/sign_up.py +++ b/mealie/routes/users/sign_up.py @@ -1,14 +1,14 @@ import uuid +from fastapi import APIRouter, Depends, HTTPException, status from mealie.core.security import get_password_hash from mealie.db.database import db from mealie.db.db_setup import generate_session -from fastapi import APIRouter, Depends from mealie.routes.deps import get_current_user from mealie.schema.sign_up import SignUpIn, SignUpOut, SignUpToken from mealie.schema.user import UserIn, UserInDB +from mealie.services.events import create_sign_up_event from sqlalchemy.orm.session import Session -from fastapi import HTTPException, status router = APIRouter(prefix="/api/users/sign-ups", tags=["User Signup"]) @@ -20,9 +20,7 @@ async def get_all_open_sign_ups( ): """ Returns a list of open sign up links """ - all_sign_ups = db.sign_ups.get_all(session) - - return all_sign_ups + return db.sign_ups.get_all(session) @router.post("", response_model=SignUpToken) @@ -41,6 +39,7 @@ async def create_user_sign_up_key( "name": key_data.name, "admin": key_data.admin, } + create_sign_up_event("Sign-up Token Created", f"Created by {current_user.full_name}", session=session) return db.sign_ups.create(session, sign_up) @@ -63,6 +62,7 @@ async def create_user_with_token( db.users.create(session, new_user.dict()) # DeleteToken + create_sign_up_event("Sign-up Token Used", f"New User {new_user.full_name}", session=session) db.sign_ups.delete(session, token)