hisham-cmd commited on
Commit
37b93fb
·
verified ·
1 Parent(s): d54b072

Upload app/api/seed.py

Browse files
Files changed (1) hide show
  1. app/api/seed.py +272 -0
app/api/seed.py ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends
2
+ from sqlalchemy.orm import Session
3
+
4
+ from app.db.base import get_db
5
+ from app.models.product import Product, ProductImage, Category
6
+ from app.schemas.auth import AuthResponse
7
+
8
+ router = APIRouter(prefix="/seed", tags=["Seed Data"])
9
+
10
+
11
+ DEMO_CATEGORIES = [
12
+ {"name_ar": "إلكترونيات", "name_en": "Electronics", "icon": "💻", "sort_order": 1},
13
+ {"name_ar": "ملابس", "name_en": "Clothing", "icon": "👕", "sort_order": 2},
14
+ {"name_ar": "أحذية", "name_en": "Shoes", "icon": "👟", "sort_order": 3},
15
+ {"name_ar": "ساعات", "name_en": "Watches", "icon": "⌚", "sort_order": 4},
16
+ {"name_ar": "حقائب", "name_en": "Bags", "icon": "👜", "sort_order": 5},
17
+ {"name_ar": "إكسسوارات", "name_en": "Accessories", "icon": "💎", "sort_order": 6},
18
+ ]
19
+
20
+ DEMO_PRODUCTS = [
21
+ {
22
+ "name_ar": "سماعات لاسلكية برو",
23
+ "name_en": "Wireless Pro Headphones",
24
+ "description_ar": "سماعات رأس لاسلكية عالية الجودة مع إلغاء الضوضاء النشط وعمر بطارية يصل إلى 30 ساعة. صوت محيطي 360 درجة مع ميكروفون مدمج للمكالمات.",
25
+ "description_en": "Premium wireless over-ear headphones with active noise cancellation and up to 30 hours battery life. Features 360° spatial audio and built-in microphone for crystal-clear calls.",
26
+ "price": 299.99,
27
+ "compare_price": 399.99,
28
+ "stock": 50,
29
+ "category_idx": 0,
30
+ "rating": 4.8,
31
+ "rating_count": 234,
32
+ "is_featured": True,
33
+ "images": [
34
+ "https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=600",
35
+ "https://images.unsplash.com/photo-1484704849700-f032a568e944?w=600",
36
+ ],
37
+ },
38
+ {
39
+ "name_ar": "ساعة ذكية ألترا",
40
+ "name_en": "Ultra Smart Watch",
41
+ "description_ar": "ساعة ذكية متقدمة مع شاشة AMOLED ومقاومة للماء حتى 50 متر. تتبع اللياقة البدنية ومراقبة معدل ضربات القلب والنوم.",
42
+ "description_en": "Advanced smartwatch with AMOLED display and 50m water resistance. Comprehensive fitness tracking, heart rate monitoring, sleep analysis, and GPS navigation.",
43
+ "price": 449.99,
44
+ "compare_price": 599.99,
45
+ "stock": 30,
46
+ "category_idx": 3,
47
+ "rating": 4.6,
48
+ "rating_count": 156,
49
+ "is_featured": True,
50
+ "images": [
51
+ "https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=600",
52
+ "https://images.unsplash.com/photo-1546868871-af0de0ae72be?w=600",
53
+ ],
54
+ },
55
+ {
56
+ "name_ar": "حقيبة ظهر جلدية",
57
+ "name_en": "Leather Backpack",
58
+ "description_ar": "حقيبة ظهر من الجلد الطبيعي الفاخر مع جيوب متعددة وبطانة مقاومة للماء. مثالية للعمل والسفر.",
59
+ "description_en": "Premium genuine leather backpack with multiple compartments and water-resistant lining. Perfect for work and travel with laptop sleeve up to 15 inches.",
60
+ "price": 189.99,
61
+ "compare_price": 249.99,
62
+ "stock": 45,
63
+ "category_idx": 4,
64
+ "rating": 4.5,
65
+ "rating_count": 89,
66
+ "is_featured": True,
67
+ "images": [
68
+ "https://images.unsplash.com/photo-1553062407-98eeb64c6a62?w=600",
69
+ "https://images.unsplash.com/photo-1622560480605-d83c853bc5c3?w=600",
70
+ ],
71
+ },
72
+ {
73
+ "name_ar": "حذاء رياضي إير ماكس",
74
+ "name_en": "Air Max Running Shoes",
75
+ "description_ar": "حذاء رياضي خفيف الوزن مع تقنية التهوية المتقدمة ونعل مرن يوفر أقصى درجات الراحة أثناء الجري.",
76
+ "description_en": "Lightweight running shoes with advanced ventilation technology and flexible sole for maximum comfort during runs. Responsive cushioning and premium mesh upper.",
77
+ "price": 159.99,
78
+ "compare_price": 199.99,
79
+ "stock": 80,
80
+ "category_idx": 2,
81
+ "rating": 4.7,
82
+ "rating_count": 312,
83
+ "is_featured": True,
84
+ "images": [
85
+ "https://images.unsplash.com/photo-1542291026-7eec264c27ff?w=600",
86
+ "https://images.unsplash.com/photo-1608231387042-66d1773070a5?w=600",
87
+ ],
88
+ },
89
+ {
90
+ "name_ar": "لابتوب ألتراسليم برو",
91
+ "name_en": "UltraSlim Pro Laptop",
92
+ "description_ar": "لابتوب احترافي بشاشة 14 بوصة 4K ومعالج من الجيل الأخير. خفيف الوزن 1.2 كغ مع بطارية تدوم 18 ساعة.",
93
+ "description_en": "Professional laptop with 14-inch 4K display and latest-gen processor. Ultra-lightweight at 1.2kg with 18-hour battery life. 16GB RAM, 512GB SSD.",
94
+ "price": 1299.99,
95
+ "compare_price": 1599.99,
96
+ "stock": 20,
97
+ "category_idx": 0,
98
+ "rating": 4.9,
99
+ "rating_count": 178,
100
+ "is_featured": True,
101
+ "images": [
102
+ "https://images.unsplash.com/photo-1496181133206-80ce9b88a853?w=600",
103
+ "https://images.unsplash.com/photo-1525547719571-a2d4ac8945e2?w=600",
104
+ ],
105
+ },
106
+ {
107
+ "name_ar": "نظارات شمسية كلاسيك",
108
+ "name_en": "Classic Sunglasses",
109
+ "description_ar": "نظارات شمسية بتصميم كلاسيكي أنيق مع عدسات بولارايزد توفر حماية كاملة من أشعة UV. إطار معدني خفيف.",
110
+ "description_en": "Classic design sunglasses with polarized lenses providing full UV protection. Lightweight metal frame with adjustable nose pads for perfect fit.",
111
+ "price": 79.99,
112
+ "compare_price": 129.99,
113
+ "stock": 100,
114
+ "category_idx": 5,
115
+ "rating": 4.4,
116
+ "rating_count": 67,
117
+ "is_featured": False,
118
+ "images": [
119
+ "https://images.unsplash.com/photo-1572635196237-14b3f281503f?w=600",
120
+ ],
121
+ },
122
+ {
123
+ "name_ar": "قميص قطن بريميوم",
124
+ "name_en": "Premium Cotton Shirt",
125
+ "description_ar": "قميص من القطن المصري الفاخر بتصميم عصري مريح. مناسب للمناسبات الرسمية وغير الرسمية.",
126
+ "description_en": "Premium Egyptian cotton shirt with modern comfortable design. Suitable for both formal and casual occasions. Breathable fabric with wrinkle-resistant finish.",
127
+ "price": 89.99,
128
+ "compare_price": None,
129
+ "stock": 120,
130
+ "category_idx": 1,
131
+ "rating": 4.3,
132
+ "rating_count": 45,
133
+ "is_featured": False,
134
+ "images": [
135
+ "https://images.unsplash.com/photo-1602810318383-e386cc2a3ccf?w=600",
136
+ ],
137
+ },
138
+ {
139
+ "name_ar": "سماعات أذن لاسلكية",
140
+ "name_en": "Wireless Earbuds",
141
+ "description_ar": "سماعات أذن لاسلكية مع علبة شحن توفر 24 ساعة من الاستخدام. مقاومة للماء والعرق مع صوت عالي الدقة.",
142
+ "description_en": "True wireless earbuds with charging case providing 24 hours of use. Water and sweat resistant (IPX5) with high-definition sound and active noise cancellation.",
143
+ "price": 129.99,
144
+ "compare_price": 179.99,
145
+ "stock": 65,
146
+ "category_idx": 0,
147
+ "rating": 4.5,
148
+ "rating_count": 198,
149
+ "is_featured": False,
150
+ "images": [
151
+ "https://images.unsplash.com/photo-1590658268037-6bf12f032f55?w=600",
152
+ ],
153
+ },
154
+ {
155
+ "name_ar": "محفظة جلدية فاخرة",
156
+ "name_en": "Luxury Leather Wallet",
157
+ "description_ar": "محفظة من الجلد الإيطالي الفاخر مع حماية RFID. تصميم أنيق مع فتحات متعددة للبطاقات وجيب للعملات.",
158
+ "description_en": "Premium Italian leather wallet with RFID protection. Elegant design with multiple card slots, bill compartment, and coin pocket. Gift box included.",
159
+ "price": 69.99,
160
+ "compare_price": 99.99,
161
+ "stock": 90,
162
+ "category_idx": 5,
163
+ "rating": 4.6,
164
+ "rating_count": 112,
165
+ "is_featured": False,
166
+ "images": [
167
+ "https://images.unsplash.com/photo-1627123424574-724758594e93?w=600",
168
+ ],
169
+ },
170
+ {
171
+ "name_ar": "جاكيت شتوي مقاوم للماء",
172
+ "name_en": "Waterproof Winter Jacket",
173
+ "description_ar": "جاكيت شتوي دافئ ومقاوم للماء والرياح. حشوة داون طبيعية مع قلنسوة قابلة للإزالة وجيوب متعددة.",
174
+ "description_en": "Warm waterproof and windproof winter jacket. Natural down insulation with removable hood and multiple pockets. Breathable membrane technology.",
175
+ "price": 249.99,
176
+ "compare_price": 349.99,
177
+ "stock": 35,
178
+ "category_idx": 1,
179
+ "rating": 4.7,
180
+ "rating_count": 76,
181
+ "is_featured": False,
182
+ "images": [
183
+ "https://images.unsplash.com/photo-1544923246-77307dd270b1?w=600",
184
+ ],
185
+ },
186
+ {
187
+ "name_ar": "كاميرا ميرورليس احترافية",
188
+ "name_en": "Professional Mirrorless Camera",
189
+ "description_ar": "كاميرا ميرورليس احترافية بدقة 45 ميجابكسل وتصوير فيديو 8K. نظام تركيز تلقائي متقدم مع تثبيت صورة 5 محاور.",
190
+ "description_en": "Professional mirrorless camera with 45MP sensor and 8K video recording. Advanced autofocus system with 5-axis image stabilization. Weather-sealed body.",
191
+ "price": 2499.99,
192
+ "compare_price": 2999.99,
193
+ "stock": 10,
194
+ "category_idx": 0,
195
+ "rating": 4.9,
196
+ "rating_count": 54,
197
+ "is_featured": True,
198
+ "images": [
199
+ "https://images.unsplash.com/photo-1516035069371-29a1b244cc32?w=600",
200
+ "https://images.unsplash.com/photo-1502920917128-1aa500764cbd?w=600",
201
+ ],
202
+ },
203
+ {
204
+ "name_ar": "حذاء رسمي جلدي",
205
+ "name_en": "Formal Leather Shoes",
206
+ "description_ar": "حذاء رسمي من الجلد الطبيعي المصقول يدوياً. نعل مطاطي مقاوم للانزلاق مع بطانة جلدية مريحة.",
207
+ "description_en": "Hand-polished genuine leather formal shoes. Non-slip rubber sole with comfortable leather lining. Classic Oxford design with Goodyear welt construction.",
208
+ "price": 199.99,
209
+ "compare_price": None,
210
+ "stock": 40,
211
+ "category_idx": 2,
212
+ "rating": 4.4,
213
+ "rating_count": 38,
214
+ "is_featured": False,
215
+ "images": [
216
+ "https://images.unsplash.com/photo-1614252369475-531eba835eb1?w=600",
217
+ ],
218
+ },
219
+ ]
220
+
221
+
222
+ @router.post("", response_model=AuthResponse)
223
+ async def seed_database(db: Session = Depends(get_db)):
224
+ """Populate database with demo categories and products."""
225
+ # Check if already seeded
226
+ existing = db.query(Category).count()
227
+ if existing > 0:
228
+ return AuthResponse(
229
+ isSuccess=True,
230
+ value={"message": "Database already has data. Skipping seed."},
231
+ statusCode=200,
232
+ )
233
+
234
+ # Create categories
235
+ categories = []
236
+ for cat_data in DEMO_CATEGORIES:
237
+ cat = Category(**cat_data)
238
+ db.add(cat)
239
+ categories.append(cat)
240
+ db.flush()
241
+
242
+ # Create products
243
+ for prod_data in DEMO_PRODUCTS:
244
+ images_data = prod_data.pop("images")
245
+ category_idx = prod_data.pop("category_idx")
246
+
247
+ product = Product(
248
+ **prod_data,
249
+ category_id=categories[category_idx].id,
250
+ )
251
+ db.add(product)
252
+ db.flush()
253
+
254
+ for i, img_url in enumerate(images_data):
255
+ img = ProductImage(
256
+ product_id=product.id,
257
+ image_url=img_url,
258
+ sort_order=i,
259
+ )
260
+ db.add(img)
261
+
262
+ db.commit()
263
+
264
+ return AuthResponse(
265
+ isSuccess=True,
266
+ value={
267
+ "message": "Database seeded successfully",
268
+ "categories_count": len(DEMO_CATEGORIES),
269
+ "products_count": len(DEMO_PRODUCTS),
270
+ },
271
+ statusCode=201,
272
+ )