All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 50s
Replace direct Calendar, MarketPlace, and Post model queries with typed service calls (services.blog, services.calendar, services.market, services.cart). Blog registers all 4 services via domain_services_fn with has() guards for composable deployment. Key changes: - app.py: use domain_services_fn instead of inline service registration - admin routes: MarketPlace queries → services.market.marketplaces_for_container() - entry_associations: CalendarEntryPost → services.calendar.entry_ids_for_content() - markets service: Post query → services.blog.get_post_by_id/slug() - posts_data, post routes: use calendar/market/cart services - menu_items: glue imports → shared imports Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
97 lines
3.2 KiB
Python
97 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import unicodedata
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from shared.models.market_place import MarketPlace
|
|
from shared.models.page_config import PageConfig
|
|
from shared.browser.app.utils import utcnow
|
|
from shared.services.registry import services
|
|
from shared.services.relationships import attach_child, detach_child
|
|
|
|
|
|
class MarketError(ValueError):
|
|
"""Base error for market service operations."""
|
|
|
|
|
|
def slugify(value: str, max_len: int = 255) -> str:
|
|
if value is None:
|
|
value = ""
|
|
value = unicodedata.normalize("NFKD", value)
|
|
value = value.encode("ascii", "ignore").decode("ascii")
|
|
value = value.lower()
|
|
value = value.replace("/", "-")
|
|
value = re.sub(r"[^a-z0-9]+", "-", value)
|
|
value = re.sub(r"-{2,}", "-", value)
|
|
value = value.strip("-")[:max_len].strip("-")
|
|
return value or "market"
|
|
|
|
|
|
async def create_market(sess: AsyncSession, post_id: int, name: str) -> MarketPlace:
|
|
name = (name or "").strip()
|
|
if not name:
|
|
raise MarketError("Market name must not be empty.")
|
|
slug = slugify(name)
|
|
|
|
post = await services.blog.get_post_by_id(sess, post_id)
|
|
if not post:
|
|
raise MarketError(f"Post {post_id} does not exist.")
|
|
|
|
if not post.is_page:
|
|
raise MarketError("Markets can only be created on pages, not posts.")
|
|
|
|
pc = (await sess.execute(
|
|
select(PageConfig).where(PageConfig.container_type == "page", PageConfig.container_id == post_id)
|
|
)).scalar_one_or_none()
|
|
if pc is None or not (pc.features or {}).get("market"):
|
|
raise MarketError("Market feature is not enabled for this page. Enable it in page settings first.")
|
|
|
|
# Look for existing (including soft-deleted)
|
|
existing = (await sess.execute(
|
|
select(MarketPlace).where(MarketPlace.container_type == "page", MarketPlace.container_id == post_id, MarketPlace.slug == slug)
|
|
)).scalar_one_or_none()
|
|
|
|
if existing:
|
|
if existing.deleted_at is not None:
|
|
existing.deleted_at = None # revive
|
|
existing.name = name
|
|
await sess.flush()
|
|
await attach_child(sess, "page", post_id, "market", existing.id)
|
|
return existing
|
|
raise MarketError(f'Market with slug "{slug}" already exists for this page.')
|
|
|
|
market = MarketPlace(container_type="page", container_id=post_id, name=name, slug=slug)
|
|
sess.add(market)
|
|
await sess.flush()
|
|
await attach_child(sess, "page", post_id, "market", market.id)
|
|
return market
|
|
|
|
|
|
async def soft_delete_market(sess: AsyncSession, post_slug: str, market_slug: str) -> bool:
|
|
post = await services.blog.get_post_by_slug(sess, post_slug)
|
|
if not post:
|
|
return False
|
|
|
|
market = (
|
|
await sess.execute(
|
|
select(MarketPlace)
|
|
.where(
|
|
MarketPlace.container_type == "page",
|
|
MarketPlace.container_id == post.id,
|
|
MarketPlace.slug == market_slug,
|
|
MarketPlace.deleted_at.is_(None),
|
|
)
|
|
)
|
|
).scalar_one_or_none()
|
|
|
|
if not market:
|
|
return False
|
|
|
|
market.deleted_at = utcnow()
|
|
await sess.flush()
|
|
await detach_child(sess, "page", market.container_id, "market", market.id)
|
|
return True
|