mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-04 15:03:10 -05:00
feat: bulk recipe settings update (#1557)
* extract switches from menu component * implement bulk updater for settings * fix browser cache api calls issue * add frontend for bulk settings modifications
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import contextlib
|
||||
import json
|
||||
from collections.abc import Callable
|
||||
from enum import Enum
|
||||
@@ -31,16 +32,15 @@ class MealieCrudRoute(APIRoute):
|
||||
original_route_handler = super().get_route_handler()
|
||||
|
||||
async def custom_route_handler(request: Request) -> Response:
|
||||
try:
|
||||
with contextlib.suppress(JSONDecodeError):
|
||||
response = await original_route_handler(request)
|
||||
response_body = json.loads(response.body)
|
||||
if type(response_body) == dict:
|
||||
if last_modified := response_body.get("updateAt"):
|
||||
response.headers["last-modified"] = last_modified
|
||||
|
||||
except JSONDecodeError:
|
||||
pass
|
||||
|
||||
# Force no-cache for all responses to prevent browser from caching API calls
|
||||
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
|
||||
return response
|
||||
|
||||
return custom_route_handler
|
||||
|
||||
@@ -7,7 +7,13 @@ from mealie.core.dependencies.dependencies import temporary_zip_path
|
||||
from mealie.core.security import create_file_token
|
||||
from mealie.routes._base import BaseUserController, controller
|
||||
from mealie.schema.group.group_exports import GroupDataExport
|
||||
from mealie.schema.recipe.recipe_bulk_actions import AssignCategories, AssignTags, DeleteRecipes, ExportRecipes
|
||||
from mealie.schema.recipe.recipe_bulk_actions import (
|
||||
AssignCategories,
|
||||
AssignSettings,
|
||||
AssignTags,
|
||||
DeleteRecipes,
|
||||
ExportRecipes,
|
||||
)
|
||||
from mealie.schema.response.responses import SuccessResponse
|
||||
from mealie.services.recipe.recipe_bulk_service import RecipeBulkActionsService
|
||||
|
||||
@@ -25,6 +31,10 @@ class RecipeBulkActionsController(BaseUserController):
|
||||
def bulk_tag_recipes(self, tag_data: AssignTags):
|
||||
self.service.assign_tags(tag_data.recipes, tag_data.tags)
|
||||
|
||||
@router.post("/settings")
|
||||
def bulk_settings_recipes(self, settings_data: AssignSettings):
|
||||
self.service.set_settings(settings_data.recipes, settings_data.settings)
|
||||
|
||||
@router.post("/categorize")
|
||||
def bulk_categorize_recipes(self, assign_cats: AssignCategories):
|
||||
self.service.assign_categories(assign_cats.recipes, assign_cats.categories)
|
||||
|
||||
@@ -2,6 +2,7 @@ import enum
|
||||
|
||||
from mealie.schema._mealie import MealieModel
|
||||
from mealie.schema.recipe.recipe_category import CategoryBase, TagBase
|
||||
from mealie.schema.recipe.recipe_settings import RecipeSettings
|
||||
|
||||
|
||||
class ExportTypes(str, enum.Enum):
|
||||
@@ -24,5 +25,9 @@ class AssignTags(ExportBase):
|
||||
tags: list[TagBase]
|
||||
|
||||
|
||||
class AssignSettings(ExportBase):
|
||||
settings: RecipeSettings
|
||||
|
||||
|
||||
class DeleteRecipes(ExportBase):
|
||||
pass
|
||||
|
||||
@@ -4,6 +4,7 @@ from mealie.repos.repository_factory import AllRepositories
|
||||
from mealie.schema.group.group_exports import GroupDataExport
|
||||
from mealie.schema.recipe import CategoryBase
|
||||
from mealie.schema.recipe.recipe_category import TagBase
|
||||
from mealie.schema.recipe.recipe_settings import RecipeSettings
|
||||
from mealie.schema.user.user import GroupInDB, PrivateUser
|
||||
from mealie.services._base_service import BaseService
|
||||
from mealie.services.exporter import Exporter, RecipeExporter
|
||||
@@ -47,6 +48,22 @@ class RecipeBulkActionsService(BaseService):
|
||||
|
||||
return exports_deleted
|
||||
|
||||
def set_settings(self, recipes: list[str], settings: RecipeSettings) -> None:
|
||||
for slug in recipes:
|
||||
recipe = self.repos.recipes.get_one(slug)
|
||||
|
||||
if recipe is None:
|
||||
self.logger.error(f"Failed to set settings for recipe {slug}, no recipe found")
|
||||
|
||||
settings.locked = recipe.settings.locked
|
||||
recipe.settings = settings
|
||||
|
||||
try:
|
||||
self.repos.recipes.update(slug, recipe)
|
||||
except Exception as e:
|
||||
self.logger.error(f"Failed to set settings for recipe {slug}")
|
||||
self.logger.error(e)
|
||||
|
||||
def assign_tags(self, recipes: list[str], tags: list[TagBase]) -> None:
|
||||
for slug in recipes:
|
||||
recipe = self.repos.recipes.get_one(slug)
|
||||
|
||||
Reference in New Issue
Block a user