fix: consolidate CMS page seed scripts and fix 3 bugs
- Fix `ContentPage.store_id is None` (Python identity check, always False) → use `.is_(None)` for proper SQLAlchemy NULL filtering - Create pages for ALL platforms instead of only OMS - Merge create_platform_pages.py into create_default_content_pages.py (5 overlapping pages, only platform_homepage was unique) - Delete redundant create_platform_pages.py - Update Makefile, install.py, and docs references Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
"""
|
||||
Create Default Platform Content Pages (CMS)
|
||||
|
||||
This script creates platform-level default content pages that all stores inherit.
|
||||
These pages serve as the baseline content for:
|
||||
This script creates platform-level default content pages for ALL platforms.
|
||||
Pages include:
|
||||
- Platform Homepage (platform marketing page)
|
||||
- About Us
|
||||
- Contact
|
||||
- FAQ
|
||||
@@ -12,14 +13,16 @@ These pages serve as the baseline content for:
|
||||
- Privacy Policy
|
||||
- Terms of Service
|
||||
|
||||
Pages are created per-platform (unique constraint: platform_id + store_id + slug).
|
||||
Stores can override any of these pages with their own custom content.
|
||||
|
||||
Prerequisites:
|
||||
- Database migrations must be applied
|
||||
- content_pages table must exist
|
||||
- Platforms must exist (run init_production.py first)
|
||||
|
||||
Usage:
|
||||
python scripts/create_default_content_pages.py
|
||||
python scripts/seed/create_default_content_pages.py
|
||||
|
||||
# Or with make:
|
||||
make create-cms-defaults
|
||||
@@ -59,7 +62,32 @@ from app.modules.cms.models import ContentPage
|
||||
from app.modules.tenancy.models import Platform
|
||||
|
||||
# ============================================================================
|
||||
# DEFAULT PAGE CONTENT
|
||||
# PLATFORM HOMEPAGE (is_platform_page=True)
|
||||
# ============================================================================
|
||||
|
||||
PLATFORM_HOMEPAGE = {
|
||||
"slug": "platform_homepage",
|
||||
"title": "Welcome to Our Multi-Store Marketplace",
|
||||
"content": """
|
||||
<p class="lead">
|
||||
Connect stores with customers worldwide. Build your online store and reach millions of shoppers.
|
||||
</p>
|
||||
<p>
|
||||
Our platform empowers entrepreneurs to launch their own branded e-commerce stores
|
||||
with minimal effort and maximum impact.
|
||||
</p>
|
||||
""",
|
||||
"meta_description": "Leading multi-store marketplace platform. Connect with thousands of stores and discover millions of products.",
|
||||
"meta_keywords": "marketplace, multi-store, e-commerce, online shopping, platform",
|
||||
"show_in_footer": False,
|
||||
"show_in_header": False,
|
||||
"is_platform_page": True,
|
||||
"template": "modern",
|
||||
"display_order": 0,
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# DEFAULT STORE CONTENT PAGES (is_platform_page=False)
|
||||
# ============================================================================
|
||||
|
||||
DEFAULT_PAGES = [
|
||||
@@ -479,9 +507,58 @@ DEFAULT_PAGES = [
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def _page_exists(db: Session, platform_id: int, slug: str) -> bool:
|
||||
"""Check if a page already exists for the given platform and slug."""
|
||||
existing = db.execute(
|
||||
select(ContentPage).where(
|
||||
ContentPage.platform_id == platform_id,
|
||||
ContentPage.store_id.is_(None),
|
||||
ContentPage.slug == slug,
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
return existing is not None
|
||||
|
||||
|
||||
def _create_page(
|
||||
db: Session, platform_id: int, page_data: dict, *, is_platform_page: bool = False
|
||||
) -> bool:
|
||||
"""Create a single content page. Returns True if created, False if skipped."""
|
||||
slug = page_data["slug"]
|
||||
title = page_data["title"]
|
||||
|
||||
if _page_exists(db, platform_id, slug):
|
||||
print(f" Skipped: {title} (/{slug}) - already exists")
|
||||
return False
|
||||
|
||||
page = ContentPage(
|
||||
platform_id=platform_id,
|
||||
store_id=None,
|
||||
slug=slug,
|
||||
title=title,
|
||||
content=page_data["content"],
|
||||
content_format="html",
|
||||
template=page_data.get("template", "default"),
|
||||
meta_description=page_data["meta_description"],
|
||||
meta_keywords=page_data["meta_keywords"],
|
||||
is_platform_page=is_platform_page,
|
||||
is_published=True,
|
||||
published_at=datetime.now(UTC),
|
||||
show_in_footer=page_data.get("show_in_footer", True),
|
||||
show_in_header=page_data.get("show_in_header", False),
|
||||
show_in_legal=page_data.get("show_in_legal", False),
|
||||
display_order=page_data["display_order"],
|
||||
created_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC),
|
||||
)
|
||||
|
||||
db.add(page)
|
||||
print(f" Created: {title} (/{slug})")
|
||||
return True
|
||||
|
||||
|
||||
def create_default_pages(db: Session) -> None:
|
||||
"""
|
||||
Create default platform content pages.
|
||||
Create default platform content pages for ALL platforms.
|
||||
|
||||
This function is idempotent - it will skip pages that already exist.
|
||||
"""
|
||||
@@ -489,68 +566,52 @@ def create_default_pages(db: Session) -> None:
|
||||
print("Creating Default Platform Content Pages (CMS)")
|
||||
print("=" * 70 + "\n")
|
||||
|
||||
# Resolve OMS platform for platform_id
|
||||
oms_platform = db.execute(
|
||||
select(Platform).where(Platform.code == "oms")
|
||||
).scalar_one_or_none()
|
||||
if not oms_platform:
|
||||
print(" ⚠️ OMS platform not found. Run init_production.py first.")
|
||||
# Load all platforms
|
||||
platforms = db.execute(select(Platform)).scalars().all()
|
||||
if not platforms:
|
||||
print(" No platforms found. Run init_production.py first.")
|
||||
return
|
||||
platform_id = oms_platform.id
|
||||
|
||||
created_count = 0
|
||||
skipped_count = 0
|
||||
total_created = 0
|
||||
total_skipped = 0
|
||||
|
||||
for page_data in DEFAULT_PAGES:
|
||||
# Check if page already exists (platform default with this slug)
|
||||
existing = db.execute(
|
||||
select(ContentPage).where(
|
||||
ContentPage.store_id is None, ContentPage.slug == page_data["slug"]
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
for platform in platforms:
|
||||
print(f" Platform: {platform.name} (code={platform.code})")
|
||||
|
||||
if existing:
|
||||
print(
|
||||
f" ⏭️ Skipped: {page_data['title']} (/{page_data['slug']}) - already exists"
|
||||
)
|
||||
created_count = 0
|
||||
skipped_count = 0
|
||||
|
||||
# Create platform homepage
|
||||
if _create_page(db, platform.id, PLATFORM_HOMEPAGE, is_platform_page=True):
|
||||
created_count += 1
|
||||
else:
|
||||
skipped_count += 1
|
||||
continue
|
||||
|
||||
# Create new platform default page
|
||||
page = ContentPage(
|
||||
platform_id=platform_id,
|
||||
store_id=None, # Platform default
|
||||
slug=page_data["slug"],
|
||||
title=page_data["title"],
|
||||
content=page_data["content"],
|
||||
content_format="html",
|
||||
meta_description=page_data["meta_description"],
|
||||
meta_keywords=page_data["meta_keywords"],
|
||||
is_published=True,
|
||||
published_at=datetime.now(UTC),
|
||||
show_in_footer=page_data.get("show_in_footer", True),
|
||||
show_in_header=page_data.get("show_in_header", False),
|
||||
show_in_legal=page_data.get("show_in_legal", False),
|
||||
display_order=page_data["display_order"],
|
||||
created_at=datetime.now(UTC),
|
||||
updated_at=datetime.now(UTC),
|
||||
)
|
||||
# Create default store content pages
|
||||
for page_data in DEFAULT_PAGES:
|
||||
if _create_page(db, platform.id, page_data):
|
||||
created_count += 1
|
||||
else:
|
||||
skipped_count += 1
|
||||
|
||||
db.add(page)
|
||||
print(f" ✅ Created: {page_data['title']} (/{page_data['slug']})")
|
||||
created_count += 1
|
||||
print(f" --- {created_count} created, {skipped_count} skipped")
|
||||
print()
|
||||
|
||||
total_created += created_count
|
||||
total_skipped += skipped_count
|
||||
|
||||
db.commit()
|
||||
|
||||
print("\n" + "=" * 70)
|
||||
print("=" * 70)
|
||||
print("Summary:")
|
||||
print(f" Created: {created_count} pages")
|
||||
print(f" Skipped: {skipped_count} pages (already exist)")
|
||||
print(f" Total: {created_count + skipped_count} pages")
|
||||
print(f" Platforms: {len(platforms)}")
|
||||
print(f" Created: {total_created} pages")
|
||||
print(f" Skipped: {total_skipped} pages (already exist)")
|
||||
print(f" Total: {total_created + total_skipped} pages")
|
||||
print("=" * 70 + "\n")
|
||||
|
||||
if created_count > 0:
|
||||
print("✅ Default platform content pages created successfully!\n")
|
||||
if total_created > 0:
|
||||
print("Default platform content pages created successfully!\n")
|
||||
print("Next steps:")
|
||||
print(
|
||||
" 1. View pages at: /about, /contact, /faq, /shipping, /returns, /privacy, /terms"
|
||||
@@ -558,7 +619,7 @@ def create_default_pages(db: Session) -> None:
|
||||
print(" 2. Stores can override these pages through the store dashboard")
|
||||
print(" 3. Edit platform defaults through the admin panel\n")
|
||||
else:
|
||||
print("ℹ️ All default pages already exist. No changes made.\n")
|
||||
print("All default pages already exist. No changes made.\n")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
@@ -568,14 +629,14 @@ def create_default_pages(db: Session) -> None:
|
||||
|
||||
def main():
|
||||
"""Main execution function."""
|
||||
print("\n🚀 Starting Default Content Pages Creation Script...\n")
|
||||
print("\nStarting Default Content Pages Creation Script...\n")
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
create_default_pages(db)
|
||||
print("✅ Script completed successfully!\n")
|
||||
print("Script completed successfully!\n")
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error: {e}\n")
|
||||
print(f"\nError: {e}\n")
|
||||
db.rollback()
|
||||
raise
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user