feat: add multi-platform CMS architecture (Phase 1)

Implement the foundation for multi-platform support allowing independent
business offerings (OMS, Loyalty, etc.) with their own CMS pages.

Database Models:
- Add Platform model for business offerings (domain, branding, config)
- Add VendorPlatform junction table for many-to-many relationship
- Update SubscriptionTier with platform_id and CMS limits
- Update ContentPage with platform_id, is_platform_page for three-tier hierarchy
- Add CMS feature codes (cms_basic, cms_custom_pages, cms_templates, etc.)

Three-Tier Content Resolution:
1. Vendor override (platform_id + vendor_id + slug)
2. Vendor default (platform_id + vendor_id=NULL + is_platform_page=False)
3. Platform marketing pages (is_platform_page=True)

New Components:
- PlatformContextMiddleware for detecting platform from domain/path
- ContentPageService updated with full three-tier resolution
- Platform folder structure (app/platforms/oms/, app/platforms/loyalty/)
- Alembic migration with backfill for existing data

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-18 19:49:44 +01:00
parent 4c9b3c4e4b
commit 408019dbb3
24 changed files with 2049 additions and 287 deletions

View File

@@ -17,6 +17,8 @@ from .architecture_scan import (
from .base import Base
from .company import Company
from .content_page import ContentPage
from .platform import Platform
from .vendor_platform import VendorPlatform
from .customer import Customer, CustomerAddress
from .password_reset_token import PasswordResetToken
from .email import EmailCategory, EmailLog, EmailStatus, EmailTemplate
@@ -106,6 +108,9 @@ __all__ = [
"VendorTheme",
# Content
"ContentPage",
# Platform
"Platform",
"VendorPlatform",
# Customer & Auth
"Customer",
"CustomerAddress",

View File

@@ -3,15 +3,27 @@
Content Page Model
Manages static content pages (About, FAQ, Contact, Shipping, Returns, etc.)
with platform-level defaults and vendor-specific overrides.
with a three-tier hierarchy:
1. Platform Marketing Pages (is_platform_page=True, vendor_id=NULL)
- Homepage, pricing, platform about, contact
- Describes the platform/business offering itself
2. Vendor Default Pages (is_platform_page=False, vendor_id=NULL)
- Generic storefront pages that all vendors inherit
- About Us, Shipping Policy, Return Policy, etc.
3. Vendor Override/Custom Pages (is_platform_page=False, vendor_id=set)
- Vendor-specific customizations
- Either overrides a default or is a completely custom page
Features:
- Platform-level default content
- Vendor-specific overrides
- Multi-platform support (each platform has its own pages)
- Three-tier content resolution
- Rich text content (HTML/Markdown)
- SEO metadata
- Published/Draft status
- Version history support
- Navigation placement (header, footer, legal)
"""
from datetime import UTC, datetime
@@ -34,25 +46,50 @@ from app.core.database import Base
class ContentPage(Base):
"""
Content pages for shops (About, FAQ, Contact, etc.)
Content pages with three-tier hierarchy.
Two-tier system:
1. Platform-level defaults (vendor_id=NULL)
2. Vendor-specific overrides (vendor_id=123)
Page Types:
1. Platform Marketing Page: platform_id=X, vendor_id=NULL, is_platform_page=True
- Platform's own pages (homepage, pricing, about)
2. Vendor Default Page: platform_id=X, vendor_id=NULL, is_platform_page=False
- Fallback pages for vendors who haven't customized
3. Vendor Override/Custom: platform_id=X, vendor_id=Y, is_platform_page=False
- Vendor-specific content
Lookup logic:
1. Check for vendor-specific page (vendor_id + slug)
2. If not found, use platform default (slug only)
3. If neither exists, show 404 or default template
Resolution Logic:
1. Check for vendor override (platform_id + vendor_id + slug)
2. Fall back to vendor default (platform_id + vendor_id=NULL + is_platform_page=False)
3. If neither exists, return 404
"""
__tablename__ = "content_pages"
id = Column(Integer, primary_key=True, index=True)
# Vendor association (NULL = platform default)
# Platform association (REQUIRED - determines which platform this page belongs to)
platform_id = Column(
Integer,
ForeignKey("platforms.id", ondelete="CASCADE"),
nullable=False,
index=True,
comment="Platform this page belongs to",
)
# Vendor association (NULL = platform page or vendor default)
vendor_id = Column(
Integer, ForeignKey("vendors.id", ondelete="CASCADE"), nullable=True, index=True
Integer,
ForeignKey("vendors.id", ondelete="CASCADE"),
nullable=True,
index=True,
comment="Vendor this page belongs to (NULL for platform/default pages)",
)
# Distinguish platform marketing pages from vendor defaults
is_platform_page = Column(
Boolean,
default=False,
nullable=False,
comment="True = platform marketing page (homepage, pricing); False = vendor default or override",
)
# Page identification
@@ -106,18 +143,22 @@ class ContentPage(Base):
)
# Relationships
platform = relationship("Platform", back_populates="content_pages")
vendor = relationship("Vendor", back_populates="content_pages")
creator = relationship("User", foreign_keys=[created_by])
updater = relationship("User", foreign_keys=[updated_by])
# Constraints
__table_args__ = (
# Unique combination: vendor can only have one page per slug
# Platform defaults (vendor_id=NULL) can only have one page per slug
UniqueConstraint("vendor_id", "slug", name="uq_vendor_slug"),
# Unique combination: platform + vendor + slug
# Platform pages: platform_id + vendor_id=NULL + is_platform_page=True
# Vendor defaults: platform_id + vendor_id=NULL + is_platform_page=False
# Vendor overrides: platform_id + vendor_id + slug
UniqueConstraint("platform_id", "vendor_id", "slug", name="uq_platform_vendor_slug"),
# Indexes for performance
Index("idx_vendor_published", "vendor_id", "is_published"),
Index("idx_slug_published", "slug", "is_published"),
Index("idx_platform_vendor_published", "platform_id", "vendor_id", "is_published"),
Index("idx_platform_slug_published", "platform_id", "slug", "is_published"),
Index("idx_platform_page_type", "platform_id", "is_platform_page"),
)
def __repr__(self):
@@ -125,19 +166,31 @@ class ContentPage(Base):
return f"<ContentPage(id={self.id}, vendor={vendor_name}, slug={self.slug}, title={self.title})>"
@property
def is_platform_default(self):
"""Check if this is a platform-level default page."""
return self.vendor_id is None
def is_vendor_default(self):
"""Check if this is a vendor default page (fallback for all vendors)."""
return self.vendor_id is None and not self.is_platform_page
@property
def is_vendor_override(self):
"""Check if this is a vendor-specific override."""
"""Check if this is a vendor-specific override or custom page."""
return self.vendor_id is not None
@property
def page_tier(self) -> str:
"""Get the tier level of this page for display purposes."""
if self.is_platform_page:
return "platform"
elif self.vendor_id is None:
return "vendor_default"
else:
return "vendor_override"
def to_dict(self):
"""Convert to dictionary for API responses."""
return {
"id": self.id,
"platform_id": self.platform_id,
"platform_code": self.platform.code if self.platform else None,
"vendor_id": self.vendor_id,
"vendor_name": self.vendor.name if self.vendor else None,
"slug": self.slug,
@@ -155,8 +208,10 @@ class ContentPage(Base):
"show_in_footer": self.show_in_footer,
"show_in_header": self.show_in_header,
"show_in_legal": self.show_in_legal,
"is_platform_default": self.is_platform_default,
"is_platform_page": self.is_platform_page,
"is_vendor_default": self.is_vendor_default,
"is_vendor_override": self.is_vendor_override,
"page_tier": self.page_tier,
"created_at": self.created_at.isoformat() if self.created_at else None,
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
"created_by": self.created_by,

View File

@@ -33,6 +33,7 @@ class FeatureCategory(str, enum.Enum):
TEAM = "team"
BRANDING = "branding"
CUSTOMERS = "customers"
CMS = "cms"
class FeatureUILocation(str, enum.Enum):
@@ -189,3 +190,11 @@ class FeatureCode:
CUSTOMER_VIEW = "customer_view"
CUSTOMER_EXPORT = "customer_export"
CUSTOMER_MESSAGING = "customer_messaging"
# CMS
CMS_BASIC = "cms_basic" # Basic CMS functionality (override defaults)
CMS_CUSTOM_PAGES = "cms_custom_pages" # Create custom pages beyond defaults
CMS_UNLIMITED_PAGES = "cms_unlimited_pages" # No page limit
CMS_TEMPLATES = "cms_templates" # Access to page templates
CMS_SEO = "cms_seo" # Advanced SEO features
CMS_SCHEDULING = "cms_scheduling" # Schedule page publish/unpublish

218
models/database/platform.py Normal file
View File

@@ -0,0 +1,218 @@
# models/database/platform.py
"""
Platform model representing a business offering/product line.
Platforms are independent business products (e.g., OMS, Loyalty Program, Site Builder)
that can have their own:
- Marketing pages (homepage, pricing, about)
- Vendor default pages (fallback storefront pages)
- Subscription tiers with platform-specific features
- Branding and configuration
Each vendor can belong to multiple platforms via the VendorPlatform junction table.
"""
from sqlalchemy import (
JSON,
Boolean,
Column,
Index,
Integer,
String,
Text,
)
from sqlalchemy.orm import relationship
from app.core.database import Base
from models.database.base import TimestampMixin
class Platform(Base, TimestampMixin):
"""
Represents a business offering/product line.
Examples:
- Wizamart OMS (Order Management System)
- Loyalty+ (Loyalty Program Platform)
- Site Builder (Website Builder for Local Businesses)
Each platform has:
- Its own domain (production) or path prefix (development)
- Independent CMS pages (marketing pages + vendor defaults)
- Platform-specific subscription tiers
- Custom branding and theme
"""
__tablename__ = "platforms"
id = Column(Integer, primary_key=True, index=True)
# ========================================================================
# Identity
# ========================================================================
code = Column(
String(50),
unique=True,
nullable=False,
index=True,
comment="Unique platform identifier (e.g., 'oms', 'loyalty', 'sites')",
)
name = Column(
String(100),
nullable=False,
comment="Display name (e.g., 'Wizamart OMS')",
)
description = Column(
Text,
nullable=True,
comment="Platform description for admin/marketing purposes",
)
# ========================================================================
# Domain Routing
# ========================================================================
domain = Column(
String(255),
unique=True,
nullable=True,
index=True,
comment="Production domain (e.g., 'oms.lu', 'loyalty.lu')",
)
path_prefix = Column(
String(50),
unique=True,
nullable=True,
index=True,
comment="Development path prefix (e.g., 'oms' for localhost:9999/oms/*)",
)
# ========================================================================
# Branding
# ========================================================================
logo = Column(
String(500),
nullable=True,
comment="Logo URL for light mode",
)
logo_dark = Column(
String(500),
nullable=True,
comment="Logo URL for dark mode",
)
favicon = Column(
String(500),
nullable=True,
comment="Favicon URL",
)
theme_config = Column(
JSON,
nullable=True,
default=dict,
comment="Theme configuration (colors, fonts, etc.)",
)
# ========================================================================
# Localization
# ========================================================================
default_language = Column(
String(5),
default="fr",
nullable=False,
comment="Default language code (e.g., 'fr', 'en', 'de')",
)
supported_languages = Column(
JSON,
default=["fr", "de", "en"],
nullable=False,
comment="List of supported language codes",
)
# ========================================================================
# Status
# ========================================================================
is_active = Column(
Boolean,
default=True,
nullable=False,
comment="Whether the platform is active and accessible",
)
is_public = Column(
Boolean,
default=True,
nullable=False,
comment="Whether the platform is visible in public listings",
)
# ========================================================================
# Configuration
# ========================================================================
settings = Column(
JSON,
nullable=True,
default=dict,
comment="Platform-specific settings and feature flags",
)
# ========================================================================
# Relationships
# ========================================================================
# Content pages belonging to this platform
content_pages = relationship(
"ContentPage",
back_populates="platform",
cascade="all, delete-orphan",
)
# Vendors on this platform (via junction table)
vendor_platforms = relationship(
"VendorPlatform",
back_populates="platform",
cascade="all, delete-orphan",
)
# Subscription tiers for this platform
subscription_tiers = relationship(
"SubscriptionTier",
back_populates="platform",
foreign_keys="SubscriptionTier.platform_id",
)
# ========================================================================
# Indexes
# ========================================================================
__table_args__ = (
Index("idx_platform_active", "is_active"),
Index("idx_platform_public", "is_public", "is_active"),
)
# ========================================================================
# Properties
# ========================================================================
@property
def base_url(self) -> str:
"""Get the base URL for this platform (for link generation)."""
if self.domain:
return f"https://{self.domain}"
if self.path_prefix:
return f"/{self.path_prefix}"
return "/"
def __repr__(self) -> str:
return f"<Platform(code='{self.code}', name='{self.name}')>"

View File

@@ -84,12 +84,26 @@ class SubscriptionTier(Base, TimestampMixin):
Database-driven tier definitions with Stripe integration.
Replaces the hardcoded TIER_LIMITS dict for dynamic tier management.
Can be:
- Global tier (platform_id=NULL): Available to all platforms
- Platform-specific tier (platform_id set): Only for that platform
"""
__tablename__ = "subscription_tiers"
id = Column(Integer, primary_key=True, index=True)
code = Column(String(30), unique=True, nullable=False, index=True)
# Platform association (NULL = global tier available to all platforms)
platform_id = Column(
Integer,
ForeignKey("platforms.id", ondelete="CASCADE"),
nullable=True,
index=True,
comment="Platform this tier belongs to (NULL = global tier)",
)
code = Column(String(30), nullable=False, index=True)
name = Column(String(100), nullable=False)
description = Column(Text, nullable=True)
@@ -103,6 +117,18 @@ class SubscriptionTier(Base, TimestampMixin):
team_members = Column(Integer, nullable=True)
order_history_months = Column(Integer, nullable=True)
# CMS Limits (null = unlimited)
cms_pages_limit = Column(
Integer,
nullable=True,
comment="Total CMS pages limit (NULL = unlimited)",
)
cms_custom_pages_limit = Column(
Integer,
nullable=True,
comment="Custom pages limit, excluding overrides (NULL = unlimited)",
)
# Features (JSON array of feature codes)
features = Column(JSON, default=list)
@@ -116,8 +142,21 @@ class SubscriptionTier(Base, TimestampMixin):
is_active = Column(Boolean, default=True, nullable=False)
is_public = Column(Boolean, default=True, nullable=False) # False for enterprise
# Relationship to Platform
platform = relationship(
"Platform",
back_populates="subscription_tiers",
foreign_keys=[platform_id],
)
# Unique constraint: tier code must be unique per platform (or globally if NULL)
__table_args__ = (
Index("idx_tier_platform_active", "platform_id", "is_active"),
)
def __repr__(self):
return f"<SubscriptionTier(code='{self.code}', name='{self.name}')>"
platform_info = f", platform_id={self.platform_id}" if self.platform_id else ""
return f"<SubscriptionTier(code='{self.code}', name='{self.name}'{platform_info})>"
def to_dict(self) -> dict:
"""Convert tier to dictionary (compatible with TIER_LIMITS format)."""
@@ -129,6 +168,8 @@ class SubscriptionTier(Base, TimestampMixin):
"products_limit": self.products_limit,
"team_members": self.team_members,
"order_history_months": self.order_history_months,
"cms_pages_limit": self.cms_pages_limit,
"cms_custom_pages_limit": self.cms_custom_pages_limit,
"features": self.features or [],
}

View File

@@ -245,6 +245,13 @@ class Vendor(Base, TimestampMixin):
cascade="all, delete-orphan",
)
# Platform memberships (many-to-many via junction table)
vendor_platforms = relationship(
"VendorPlatform",
back_populates="vendor",
cascade="all, delete-orphan",
)
def __repr__(self):
"""String representation of the Vendor object."""
return f"<Vendor(id={self.id}, vendor_code='{self.vendor_code}', name='{self.name}', subdomain='{self.subdomain}')>"

View File

@@ -0,0 +1,189 @@
# models/database/vendor_platform.py
"""
VendorPlatform junction table for many-to-many relationship between Vendor and Platform.
A vendor CAN belong to multiple platforms (e.g., both OMS and Loyalty Program).
Each membership can have:
- Platform-specific subscription tier
- Custom subdomain for that platform
- Platform-specific settings
- Active/inactive status
"""
from datetime import UTC, datetime
from sqlalchemy import (
JSON,
Boolean,
Column,
DateTime,
ForeignKey,
Index,
Integer,
String,
UniqueConstraint,
)
from sqlalchemy.orm import relationship
from app.core.database import Base
from models.database.base import TimestampMixin
class VendorPlatform(Base, TimestampMixin):
"""
Junction table linking vendors to platforms.
Allows a vendor to:
- Subscribe to multiple platforms (OMS + Loyalty)
- Have different tiers per platform
- Have platform-specific subdomains
- Store platform-specific settings
Example:
- Vendor "WizaMart" is on OMS platform (Professional tier)
- Vendor "WizaMart" is also on Loyalty platform (Basic tier)
"""
__tablename__ = "vendor_platforms"
id = Column(Integer, primary_key=True, index=True)
# ========================================================================
# Foreign Keys
# ========================================================================
vendor_id = Column(
Integer,
ForeignKey("vendors.id", ondelete="CASCADE"),
nullable=False,
index=True,
comment="Reference to the vendor",
)
platform_id = Column(
Integer,
ForeignKey("platforms.id", ondelete="CASCADE"),
nullable=False,
index=True,
comment="Reference to the platform",
)
tier_id = Column(
Integer,
ForeignKey("subscription_tiers.id", ondelete="SET NULL"),
nullable=True,
index=True,
comment="Platform-specific subscription tier",
)
# ========================================================================
# Membership Status
# ========================================================================
is_active = Column(
Boolean,
default=True,
nullable=False,
comment="Whether the vendor is active on this platform",
)
is_primary = Column(
Boolean,
default=False,
nullable=False,
comment="Whether this is the vendor's primary platform",
)
# ========================================================================
# Platform-Specific Configuration
# ========================================================================
custom_subdomain = Column(
String(100),
nullable=True,
comment="Platform-specific subdomain (if different from main subdomain)",
)
settings = Column(
JSON,
nullable=True,
default=dict,
comment="Platform-specific vendor settings",
)
# ========================================================================
# Timestamps
# ========================================================================
joined_at = Column(
DateTime(timezone=True),
default=lambda: datetime.now(UTC),
nullable=False,
comment="When the vendor joined this platform",
)
# ========================================================================
# Relationships
# ========================================================================
vendor = relationship(
"Vendor",
back_populates="vendor_platforms",
)
platform = relationship(
"Platform",
back_populates="vendor_platforms",
)
tier = relationship(
"SubscriptionTier",
foreign_keys=[tier_id],
)
# ========================================================================
# Constraints & Indexes
# ========================================================================
__table_args__ = (
# Each vendor can only be on a platform once
UniqueConstraint(
"vendor_id",
"platform_id",
name="uq_vendor_platform",
),
# Performance indexes
Index(
"idx_vendor_platform_active",
"vendor_id",
"platform_id",
"is_active",
),
Index(
"idx_vendor_platform_primary",
"vendor_id",
"is_primary",
),
)
# ========================================================================
# Properties
# ========================================================================
@property
def tier_code(self) -> str | None:
"""Get the tier code for this platform membership."""
return self.tier.code if self.tier else None
@property
def tier_name(self) -> str | None:
"""Get the tier name for this platform membership."""
return self.tier.name if self.tier else None
def __repr__(self) -> str:
return (
f"<VendorPlatform("
f"vendor_id={self.vendor_id}, "
f"platform_id={self.platform_id}, "
f"is_active={self.is_active})>"
)