All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 51s
Market CRUD in blog now delegates to services.market.create_marketplace() and soft_delete_marketplace() instead of importing MarketPlace directly. Adds TODO comments on Calendar imports in admin routes (deferred to admin UI rework). Updates shared submodule. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
62 lines
2.0 KiB
Python
62 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
import re
|
|
import unicodedata
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from shared.models.page_config import PageConfig
|
|
from shared.contracts.dtos import MarketPlaceDTO
|
|
from shared.services.registry import services
|
|
|
|
|
|
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) -> MarketPlaceDTO:
|
|
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.")
|
|
|
|
try:
|
|
return await services.market.create_marketplace(sess, "page", post_id, name, slug)
|
|
except ValueError as e:
|
|
raise MarketError(str(e)) from e
|
|
|
|
|
|
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
|
|
|
|
return await services.market.soft_delete_marketplace(sess, "page", post.id, market_slug)
|