Spaces:
Sleeping
Sleeping
File size: 3,203 Bytes
60470bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 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()
|