Add DEMO feature to the project
This commit is contained in:
144
scripts/demo/seed_demo_tenants.py
Normal file
144
scripts/demo/seed_demo_tenants.py
Normal file
@@ -0,0 +1,144 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Seed Demo Tenants
|
||||
Creates base demo tenant templates with Spanish data
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
project_root = Path(__file__).parent.parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
import os
|
||||
os.environ.setdefault("TENANT_DATABASE_URL", os.getenv("TENANT_DATABASE_URL"))
|
||||
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy import select
|
||||
import structlog
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
# Demo tenant configurations
|
||||
DEMO_TENANTS = [
|
||||
{
|
||||
"id": "a1b2c3d4-e5f6-47a8-b9c0-d1e2f3a4b5c6",
|
||||
"name": "Panadería San Pablo - Demo",
|
||||
"subdomain": "demo-sanpablo",
|
||||
"business_type": "bakery",
|
||||
"business_model": "individual_bakery",
|
||||
"owner_id": "c1a2b3c4-d5e6-47a8-b9c0-d1e2f3a4b5c6", # María García
|
||||
"address": "Calle Mayor, 15",
|
||||
"city": "Madrid",
|
||||
"postal_code": "28013",
|
||||
"latitude": 40.4168,
|
||||
"longitude": -3.7038,
|
||||
"phone": "+34 912 345 678",
|
||||
"email": "contacto@panaderiasanpablo.com",
|
||||
"subscription_tier": "professional",
|
||||
"is_active": True,
|
||||
"is_demo": True,
|
||||
"is_demo_template": True,
|
||||
"ml_model_trained": True,
|
||||
},
|
||||
{
|
||||
"id": "b2c3d4e5-f6a7-48b9-c0d1-e2f3a4b5c6d7",
|
||||
"name": "Panadería La Espiga - Demo",
|
||||
"subdomain": "demo-laespiga",
|
||||
"business_type": "bakery",
|
||||
"business_model": "central_baker_satellite",
|
||||
"owner_id": "d2e3f4a5-b6c7-48d9-e0f1-a2b3c4d5e6f7", # Carlos Martínez
|
||||
"address": "Avenida de la Constitución, 42",
|
||||
"city": "Barcelona",
|
||||
"postal_code": "08001",
|
||||
"latitude": 41.3851,
|
||||
"longitude": 2.1734,
|
||||
"phone": "+34 913 456 789",
|
||||
"email": "contacto@panaderialaespiga.com",
|
||||
"subscription_tier": "enterprise",
|
||||
"is_active": True,
|
||||
"is_demo": True,
|
||||
"is_demo_template": True,
|
||||
"ml_model_trained": True,
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def seed_demo_tenants():
|
||||
"""Seed demo tenants into tenant database"""
|
||||
|
||||
database_url = os.getenv("TENANT_DATABASE_URL")
|
||||
if not database_url:
|
||||
logger.error("TENANT_DATABASE_URL environment variable not set")
|
||||
return False
|
||||
|
||||
logger.info("Connecting to tenant database", url=database_url.split("@")[-1])
|
||||
|
||||
engine = create_async_engine(database_url, echo=False)
|
||||
session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
try:
|
||||
async with session_factory() as session:
|
||||
try:
|
||||
from app.models.tenants import Tenant
|
||||
except ImportError:
|
||||
from services.tenant.app.models.tenants import Tenant
|
||||
|
||||
for tenant_data in DEMO_TENANTS:
|
||||
# Check if tenant already exists
|
||||
result = await session.execute(
|
||||
select(Tenant).where(Tenant.subdomain == tenant_data["subdomain"])
|
||||
)
|
||||
existing_tenant = result.scalar_one_or_none()
|
||||
|
||||
if existing_tenant:
|
||||
logger.info(f"Demo tenant already exists: {tenant_data['subdomain']}")
|
||||
continue
|
||||
|
||||
# Create new demo tenant
|
||||
tenant = Tenant(
|
||||
id=uuid.UUID(tenant_data["id"]),
|
||||
name=tenant_data["name"],
|
||||
subdomain=tenant_data["subdomain"],
|
||||
business_type=tenant_data["business_type"],
|
||||
business_model=tenant_data["business_model"],
|
||||
owner_id=uuid.UUID(tenant_data["owner_id"]),
|
||||
address=tenant_data["address"],
|
||||
city=tenant_data["city"],
|
||||
postal_code=tenant_data["postal_code"],
|
||||
latitude=tenant_data.get("latitude"),
|
||||
longitude=tenant_data.get("longitude"),
|
||||
phone=tenant_data.get("phone"),
|
||||
email=tenant_data.get("email"),
|
||||
subscription_tier=tenant_data["subscription_tier"],
|
||||
is_active=tenant_data["is_active"],
|
||||
is_demo=tenant_data["is_demo"],
|
||||
is_demo_template=tenant_data["is_demo_template"],
|
||||
ml_model_trained=tenant_data.get("ml_model_trained", False),
|
||||
created_at=datetime.now(timezone.utc),
|
||||
updated_at=datetime.now(timezone.utc)
|
||||
)
|
||||
|
||||
session.add(tenant)
|
||||
logger.info(f"Created demo tenant: {tenant_data['name']}")
|
||||
|
||||
await session.commit()
|
||||
logger.info("Demo tenants seeded successfully")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to seed demo tenants: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
result = asyncio.run(seed_demo_tenants())
|
||||
sys.exit(0 if result else 1)
|
||||
Reference in New Issue
Block a user