mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-07 08:23:12 -05:00
feat: Add Households to Mealie (#3970)
This commit is contained in:
@@ -3,46 +3,46 @@ from uuid import UUID
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import UUID4
|
||||
|
||||
from mealie.repos.all_repositories import get_repositories
|
||||
from mealie.routes._base import controller
|
||||
from mealie.routes._base.base_controllers import BasePublicExploreController
|
||||
from mealie.routes._base.base_controllers import BasePublicHouseholdExploreController
|
||||
from mealie.schema.cookbook.cookbook import ReadCookBook, RecipeCookBook
|
||||
from mealie.schema.make_dependable import make_dependable
|
||||
from mealie.schema.response.pagination import PaginationBase, PaginationQuery
|
||||
|
||||
router = APIRouter(prefix="/cookbooks/{group_slug}")
|
||||
router = APIRouter(prefix="/cookbooks")
|
||||
|
||||
|
||||
@controller(router)
|
||||
class PublicCookbooksController(BasePublicExploreController):
|
||||
class PublicCookbooksController(BasePublicHouseholdExploreController):
|
||||
@property
|
||||
def cookbooks(self):
|
||||
return self.repos.cookbooks.by_group(self.group.id)
|
||||
|
||||
@property
|
||||
def recipes(self):
|
||||
return self.repos.recipes.by_group(self.group.id)
|
||||
def cross_household_cookbooks(self):
|
||||
return self.cross_household_repos.cookbooks
|
||||
|
||||
@router.get("", response_model=PaginationBase[ReadCookBook])
|
||||
def get_all(
|
||||
self, q: PaginationQuery = Depends(make_dependable(PaginationQuery)), search: str | None = None
|
||||
self,
|
||||
q: PaginationQuery = Depends(make_dependable(PaginationQuery)),
|
||||
search: str | None = None,
|
||||
) -> PaginationBase[ReadCookBook]:
|
||||
public_filter = "public = TRUE"
|
||||
public_filter = "(household.preferences.privateHousehold = FALSE AND public = TRUE)"
|
||||
if q.query_filter:
|
||||
q.query_filter = f"({q.query_filter}) AND {public_filter}"
|
||||
else:
|
||||
q.query_filter = public_filter
|
||||
|
||||
response = self.cookbooks.page_all(
|
||||
response = self.cross_household_cookbooks.page_all(
|
||||
pagination=q,
|
||||
override=ReadCookBook,
|
||||
search=search,
|
||||
)
|
||||
|
||||
response.set_pagination_guides(router.url_path_for("get_all", group_slug=self.group.slug), q.model_dump())
|
||||
response.set_pagination_guides(self.get_explore_url_path(router.url_path_for("get_all")), q.model_dump())
|
||||
return response
|
||||
|
||||
@router.get("/{item_id}", response_model=RecipeCookBook)
|
||||
def get_one(self, item_id: UUID4 | str) -> RecipeCookBook:
|
||||
NOT_FOUND_EXCEPTION = HTTPException(404, "cookbook not found")
|
||||
if isinstance(item_id, UUID):
|
||||
match_attr = "id"
|
||||
else:
|
||||
@@ -51,12 +51,24 @@ class PublicCookbooksController(BasePublicExploreController):
|
||||
match_attr = "id"
|
||||
except ValueError:
|
||||
match_attr = "slug"
|
||||
cookbook = self.cookbooks.get_one(item_id, match_attr)
|
||||
cookbook = self.cross_household_cookbooks.get_one(item_id, match_attr)
|
||||
|
||||
if not cookbook or not cookbook.public:
|
||||
raise HTTPException(404, "cookbook not found")
|
||||
raise NOT_FOUND_EXCEPTION
|
||||
household = self.repos.households.get_one(cookbook.household_id)
|
||||
if not household or household.preferences.private_household:
|
||||
raise NOT_FOUND_EXCEPTION
|
||||
|
||||
recipes = self.recipes.page_all(
|
||||
PaginationQuery(page=1, per_page=-1, query_filter="settings.public = TRUE"), cookbook=cookbook
|
||||
# limit recipes to only the household the cookbook belongs to
|
||||
recipes_repo = get_repositories(
|
||||
self.session, group_id=self.group_id, household_id=cookbook.household_id
|
||||
).recipes
|
||||
recipes = recipes_repo.page_all(
|
||||
PaginationQuery(
|
||||
page=1,
|
||||
per_page=-1,
|
||||
query_filter="settings.public = TRUE",
|
||||
),
|
||||
cookbook=cookbook,
|
||||
)
|
||||
return cookbook.cast(RecipeCookBook, recipes=recipes.items)
|
||||
|
||||
Reference in New Issue
Block a user