mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-10 12:25:14 -05:00
[Feat] ✨ Migrate from Pages to Cookbooks (#664)
* feat: ✨ Add Description to Cookbooks * feat(frontend): ✨ Cookbook view page * feat(frontend): 💄 Add final UI touches * fix(backend): 🐛 Add get by slug or id * fix linting issue * test(backend): ✅ Update tests from pages -> cookbooks * refactor(backend): 🔥 Delete old page files Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
@@ -10,6 +10,7 @@ class CookBook(SqlAlchemyBase, BaseMixins):
|
||||
id = Column(Integer, primary_key=True)
|
||||
position = Column(Integer, nullable=False)
|
||||
name = Column(String, nullable=False)
|
||||
description = Column(String, default="")
|
||||
slug = Column(String, nullable=False)
|
||||
categories = orm.relationship(Category, secondary=cookbooks_to_categories, single_parent=True)
|
||||
|
||||
|
||||
@@ -44,7 +44,6 @@ def export_database(background_tasks: BackgroundTasks, data: BackupJob, session:
|
||||
templates=data.templates,
|
||||
export_recipes=data.options.recipes,
|
||||
export_settings=data.options.settings,
|
||||
export_pages=data.options.pages,
|
||||
export_users=data.options.users,
|
||||
export_groups=data.options.groups,
|
||||
export_notifications=data.options.notifications,
|
||||
@@ -92,7 +91,6 @@ def import_database(
|
||||
archive=import_data.name,
|
||||
import_recipes=import_data.recipes,
|
||||
import_settings=import_data.settings,
|
||||
import_pages=import_data.pages,
|
||||
import_users=import_data.users,
|
||||
import_groups=import_data.groups,
|
||||
force_import=import_data.force,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from fastapi import Depends
|
||||
|
||||
from mealie.routes.routers import UserAPIRouter
|
||||
from mealie.schema.cookbook.cookbook import CreateCookBook, ReadCookBook
|
||||
from mealie.schema.cookbook.cookbook import CreateCookBook, ReadCookBook, RecipeCookBook
|
||||
from mealie.services.cookbook import CookbookService
|
||||
|
||||
user_router = UserAPIRouter(prefix="/groups/cookbooks", tags=["Groups: Cookbooks"])
|
||||
@@ -28,7 +28,7 @@ def update_many(data: list[ReadCookBook], cb_service: CookbookService = Depends(
|
||||
return cb_service.update_many(data)
|
||||
|
||||
|
||||
@user_router.get("/{id}", response_model=ReadCookBook)
|
||||
@user_router.get("/{id}", response_model=RecipeCookBook)
|
||||
def get_cookbook(cb_service: CookbookService = Depends(CookbookService.write_existing)):
|
||||
""" Get cookbook from the Database """
|
||||
# Get Item
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from . import custom_pages, site_settings
|
||||
from . import site_settings
|
||||
|
||||
settings_router = APIRouter()
|
||||
|
||||
settings_router.include_router(custom_pages.public_router)
|
||||
settings_router.include_router(custom_pages.admin_router)
|
||||
settings_router.include_router(site_settings.public_router)
|
||||
settings_router.include_router(site_settings.admin_router)
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm.session import Session
|
||||
|
||||
from mealie.db.database import db
|
||||
from mealie.db.db_setup import generate_session
|
||||
from mealie.routes.routers import AdminAPIRouter
|
||||
from mealie.schema.admin import CustomPageBase, CustomPageOut
|
||||
|
||||
public_router = APIRouter(prefix="/api/site-settings/custom-pages", tags=["Settings"])
|
||||
admin_router = AdminAPIRouter(prefix="/api/site-settings/custom-pages", tags=["Settings"])
|
||||
|
||||
|
||||
@public_router.get("")
|
||||
def get_custom_pages(session: Session = Depends(generate_session)):
|
||||
""" Returns the sites custom pages """
|
||||
|
||||
return db.custom_pages.get_all(session)
|
||||
|
||||
|
||||
@admin_router.post("")
|
||||
async def create_new_page(
|
||||
new_page: CustomPageBase,
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Creates a new Custom Page """
|
||||
|
||||
db.custom_pages.create(session, new_page.dict())
|
||||
|
||||
|
||||
@admin_router.put("")
|
||||
async def update_multiple_pages(pages: list[CustomPageOut], session: Session = Depends(generate_session)):
|
||||
""" Update multiple custom pages """
|
||||
for page in pages:
|
||||
db.custom_pages.update(session, page.id, page.dict())
|
||||
|
||||
|
||||
@public_router.get("/{id}")
|
||||
async def get_single_page(
|
||||
id: Union[int, str],
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Removes a custom page from the database """
|
||||
if isinstance(id, int):
|
||||
return db.custom_pages.get(session, id)
|
||||
elif isinstance(id, str):
|
||||
return db.custom_pages.get(session, id, "slug")
|
||||
|
||||
|
||||
@admin_router.put("/{id}")
|
||||
async def update_single_page(
|
||||
data: CustomPageOut,
|
||||
id: int,
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Removes a custom page from the database """
|
||||
|
||||
return db.custom_pages.update(session, id, data.dict())
|
||||
|
||||
|
||||
@admin_router.delete("/{id}")
|
||||
async def delete_custom_page(
|
||||
id: int,
|
||||
session: Session = Depends(generate_session),
|
||||
):
|
||||
""" Removes a custom page from the database """
|
||||
|
||||
db.custom_pages.delete(session, id)
|
||||
return
|
||||
@@ -7,7 +7,6 @@ from pydantic import BaseModel
|
||||
class BackupOptions(BaseModel):
|
||||
recipes: bool = True
|
||||
settings: bool = True
|
||||
pages: bool = True
|
||||
themes: bool = True
|
||||
groups: bool = True
|
||||
users: bool = True
|
||||
|
||||
@@ -2,11 +2,12 @@ from fastapi_camelcase import CamelModel
|
||||
from pydantic import validator
|
||||
from slugify import slugify
|
||||
|
||||
from ..recipe.recipe_category import CategoryBase
|
||||
from ..recipe.recipe_category import CategoryBase, RecipeCategoryResponse
|
||||
|
||||
|
||||
class CreateCookBook(CamelModel):
|
||||
name: str
|
||||
description: str = ""
|
||||
slug: str = None
|
||||
position: int = 1
|
||||
categories: list[CategoryBase] = []
|
||||
@@ -36,3 +37,11 @@ class ReadCookBook(UpdateCookBook):
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class RecipeCookBook(ReadCookBook):
|
||||
group_id: int
|
||||
categories: list[RecipeCategoryResponse]
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import List, Optional
|
||||
from typing import List
|
||||
|
||||
from fastapi_camelcase import CamelModel
|
||||
from pydantic.utils import GetterDict
|
||||
@@ -23,9 +23,10 @@ class CategoryBase(CategoryIn):
|
||||
|
||||
|
||||
class RecipeCategoryResponse(CategoryBase):
|
||||
recipes: Optional[List["Recipe"]]
|
||||
recipes: List["Recipe"] = []
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
schema_extra = {"example": {"id": 1, "name": "dinner", "recipes": [{}]}}
|
||||
|
||||
|
||||
|
||||
@@ -108,7 +108,6 @@ def backup_all(
|
||||
templates=None,
|
||||
export_recipes=True,
|
||||
export_settings=True,
|
||||
export_pages=True,
|
||||
export_users=True,
|
||||
export_groups=True,
|
||||
export_notifications=True,
|
||||
@@ -136,10 +135,6 @@ def backup_all(
|
||||
all_settings = db.settings.get_all(session)
|
||||
db_export.export_items(all_settings, "settings")
|
||||
|
||||
if export_pages:
|
||||
all_pages = db.custom_pages.get_all(session)
|
||||
db_export.export_items(all_pages, "pages")
|
||||
|
||||
if export_notifications:
|
||||
all_notifications = db.event_notifications.get_all(session)
|
||||
db_export.export_items(all_notifications, "notifications")
|
||||
|
||||
@@ -11,8 +11,6 @@ from mealie.core.config import app_dirs
|
||||
from mealie.db.database import db
|
||||
from mealie.schema.admin import (
|
||||
CommentImport,
|
||||
CustomPageImport,
|
||||
CustomPageOut,
|
||||
GroupImport,
|
||||
NotificationImport,
|
||||
RecipeImport,
|
||||
@@ -189,19 +187,6 @@ class ImportDatabase:
|
||||
|
||||
return [import_status]
|
||||
|
||||
def import_pages(self):
|
||||
pages_file = self.import_dir.joinpath("pages", "pages.json")
|
||||
pages = ImportDatabase.read_models_file(pages_file, CustomPageOut)
|
||||
|
||||
page_imports = []
|
||||
for page in pages:
|
||||
import_stats = self.import_model(
|
||||
db_table=db.custom_pages, model=page, return_model=CustomPageImport, name_attr="name", search_key="slug"
|
||||
)
|
||||
page_imports.append(import_stats)
|
||||
|
||||
return page_imports
|
||||
|
||||
def import_groups(self):
|
||||
groups_file = self.import_dir.joinpath("groups", "groups.json")
|
||||
groups = ImportDatabase.read_models_file(groups_file, UpdateGroup)
|
||||
@@ -326,7 +311,6 @@ def import_database(
|
||||
archive,
|
||||
import_recipes=True,
|
||||
import_settings=True,
|
||||
import_pages=True,
|
||||
import_users=True,
|
||||
import_groups=True,
|
||||
import_notifications=True,
|
||||
@@ -343,10 +327,6 @@ def import_database(
|
||||
if import_settings:
|
||||
settings_report = import_session.import_settings()
|
||||
|
||||
page_report = []
|
||||
if import_pages:
|
||||
page_report = import_session.import_pages()
|
||||
|
||||
group_report = []
|
||||
if import_groups:
|
||||
group_report = import_session.import_groups()
|
||||
@@ -367,7 +347,6 @@ def import_database(
|
||||
return {
|
||||
"recipeImports": recipe_report,
|
||||
"settingsImports": settings_report,
|
||||
"pageImports": page_report,
|
||||
"groupImports": group_report,
|
||||
"userImports": user_report,
|
||||
"notificationImports": notification_report,
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
from mealie.core.root_logger import get_logger
|
||||
from mealie.schema.cookbook.cookbook import CreateCookBook, ReadCookBook, SaveCookBook
|
||||
from mealie.schema.cookbook.cookbook import CreateCookBook, ReadCookBook, RecipeCookBook, SaveCookBook
|
||||
from mealie.services.base_http_service.base_http_service import BaseHttpService
|
||||
from mealie.services.events import create_group_event
|
||||
|
||||
logger = get_logger(module=__name__)
|
||||
|
||||
|
||||
class CookbookService(BaseHttpService[str, str]):
|
||||
class CookbookService(BaseHttpService[int, str]):
|
||||
"""
|
||||
Class Methods:
|
||||
`read_existing`: Reads an existing recipe from the database.
|
||||
@@ -39,8 +41,17 @@ class CookbookService(BaseHttpService[str, str]):
|
||||
if self.cookbook.group_id != self.group_id:
|
||||
raise HTTPException(status.HTTP_403_FORBIDDEN)
|
||||
|
||||
def populate_cookbook(self, id):
|
||||
self.cookbook = self.db.cookbooks.get(self.session, id)
|
||||
def populate_cookbook(self, id: int | str):
|
||||
try:
|
||||
id = int(id)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if isinstance(id, int):
|
||||
self.cookbook = self.db.cookbooks.get_one(self.session, id, override_schema=RecipeCookBook)
|
||||
|
||||
else:
|
||||
self.cookbook = self.db.cookbooks.get_one(self.session, id, key="slug", override_schema=RecipeCookBook)
|
||||
|
||||
def get_all(self) -> list[ReadCookBook]:
|
||||
items = self.db.cookbooks.get(self.session, self.group_id, "group_id", limit=999)
|
||||
|
||||
@@ -134,7 +134,9 @@ def insideParenthesis(token, tokens):
|
||||
return True
|
||||
else:
|
||||
line = " ".join(tokens)
|
||||
return re.match(r".*\(.*" + re.escape(token) + ".*\).*", line) is not None
|
||||
return (
|
||||
re.match(r".*\(.*" + re.escape(token) + ".*\).*", line) is not None # noqa: W605 - invalid dscape sequence
|
||||
)
|
||||
|
||||
|
||||
def displayIngredient(ingredient):
|
||||
@@ -222,7 +224,7 @@ def import_data(lines):
|
||||
|
||||
# turn B-NAME/123 back into "name"
|
||||
tag, confidence = re.split(r"/", columns[-1], 1)
|
||||
tag = re.sub("^[BI]\-", "", tag).lower()
|
||||
tag = re.sub("^[BI]\-", "", tag).lower() # noqa: W605 - invalid dscape sequence
|
||||
|
||||
# ---- DISPLAY ----
|
||||
# build a structure which groups each token by its tag, so we can
|
||||
|
||||
Reference in New Issue
Block a user