feat: add MarketPlace model, migration, and market_url_for helper
Introduces per-page markets (Phase 2): - MarketPlace model with soft-delete and unique slug index - NavTop gains market_id FK to scope nav hierarchy per market - Migration with backfill creates default 'suma-market' for existing market page - market_url_for() cross-app URL helper Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ from .ghost_membership_entities import (
|
||||
|
||||
from .calendars import Calendar, CalendarEntry, Ticket
|
||||
from .page_config import PageConfig
|
||||
from .market_place import MarketPlace
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -172,6 +172,14 @@ class Post(Base):
|
||||
passive_deletes=True,
|
||||
)
|
||||
|
||||
markets: Mapped[List["MarketPlace"]] = relationship(
|
||||
"MarketPlace",
|
||||
back_populates="post",
|
||||
cascade="all, delete-orphan",
|
||||
passive_deletes=True,
|
||||
order_by="MarketPlace.name",
|
||||
)
|
||||
|
||||
class Author(Base):
|
||||
__tablename__ = "authors"
|
||||
|
||||
|
||||
@@ -186,11 +186,18 @@ class NavTop(Base):
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
label: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
slug: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
|
||||
market_id: Mapped[Optional[int]] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("market_places.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
||||
deleted_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True))
|
||||
|
||||
|
||||
listings: Mapped[List["Listing"]] = relationship(back_populates="top", cascade="all, delete-orphan")
|
||||
market = relationship("MarketPlace", back_populates="nav_tops")
|
||||
|
||||
__table_args__ = (UniqueConstraint("label", "slug", name="uq_nav_tops_label_slug"),)
|
||||
|
||||
|
||||
54
models/market_place.py
Normal file
54
models/market_place.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional, List
|
||||
|
||||
from sqlalchemy import (
|
||||
Integer, String, Text, DateTime, ForeignKey, Index, func, text,
|
||||
)
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from db.base import Base
|
||||
|
||||
|
||||
def utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class MarketPlace(Base):
|
||||
__tablename__ = "market_places"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
post_id: Mapped[int] = mapped_column(
|
||||
Integer,
|
||||
ForeignKey("posts.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
slug: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now(),
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), nullable=False, server_default=func.now(),
|
||||
)
|
||||
deleted_at: Mapped[Optional[datetime]] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True,
|
||||
)
|
||||
|
||||
post = relationship("Post", back_populates="markets")
|
||||
nav_tops: Mapped[List["NavTop"]] = relationship(
|
||||
"NavTop", back_populates="market",
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
Index("ix_market_places_post_id", "post_id"),
|
||||
Index(
|
||||
"ux_market_places_slug_active",
|
||||
func.lower(slug),
|
||||
unique=True,
|
||||
postgresql_where=text("deleted_at IS NULL"),
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user