VortexCommerceBackend / seed_home_sections.py
hisham-cmd's picture
Optimized Sync: Differential Upload & Performance Tweak
60470bc
Raw
History Blame Contribute Delete
3.2 kB
import sys
import os
sys.path.append(os.getcwd())
from app.db.base import engine as base_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
from app.models.home import HomeSection
from app.models.product import Category
# For Supabase Pooler (6543), use NullPool to avoid session overhead
base_engine.pool = NullPool()
SessionLocal = sessionmaker(bind=base_engine)
db = SessionLocal()
def seed_home_sections():
print("Seeding Home Sections...")
# 1. Clear existing sections to avoid duplicates
db.query(HomeSection).delete()
db.commit()
# Get some category IDs for dynamic rules
categories = db.query(Category).all()
cat_map = {c.name_en: c.id for c in categories}
sections_data = [
{
"title_ar": "وصل حديثاً",
"title_en": "New Arrivals",
"subtitle_ar": "اكتشف أحدث المنتجات في متجرنا",
"subtitle_en": "Discover the latest additions to our store",
"section_type": "AUTOMATIC",
"rule": {"sort_by": "created_at", "sort_order": "desc", "limit": 8},
"display_order": 1,
"color_theme": "emerald",
"icon": "✨"
},
{
"title_ar": "الأكثر مبيعاً",
"title_en": "Best Sellers",
"subtitle_ar": "المنتجات الأكثر طلباً هذا الأسبوع",
"subtitle_en": "Most popular items this week",
"section_type": "AUTOMATIC",
"rule": {"is_featured": True, "limit": 8},
"display_order": 2,
"color_theme": "blue",
"icon": "🔥"
},
{
"title_ar": "عروض الأجهزة الإلكترونية",
"title_en": "Electronics Deals",
"subtitle_ar": "أفضل الأسعار على الجوالات والملحقات",
"subtitle_en": "Best prices on phones and accessories",
"section_type": "AUTOMATIC",
"rule": {
"category_id": cat_map.get("Electronics", 1),
"limit": 4,
"sort_by": "discount"
},
"display_order": 3,
"color_theme": "amber",
"icon": "📱"
},
{
"title_ar": "عالم الألعاب",
"title_en": "Gaming World",
"subtitle_ar": "اكتشف تشكيلة واسعة من مستلزمات الألعاب",
"subtitle_en": "Explore our wide range of gaming gear",
"section_type": "AUTOMATIC",
"rule": {
"category_id": cat_map.get("Gaming", 3),
"limit": 8
},
"display_order": 4,
"color_theme": "indigo",
"icon": "🎮"
}
]
for data in sections_data:
section = HomeSection(**data)
db.add(section)
db.commit()
print(f"Successfully seeded {len(sections_data)} home sections.")
if __name__ == "__main__":
try:
seed_home_sections()
except Exception as e:
print(f"Error seeding: {e}")
finally:
db.close()