-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed_data.py
More file actions
350 lines (335 loc) · 10.4 KB
/
Copy pathseed_data.py
File metadata and controls
350 lines (335 loc) · 10.4 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#/ Seed demo data: admin, categories, products, stores, exchange rates, spec fields."""
#/ этот скрипт позволяет запустить миграцию данных в базу данных и создать администратора по умолчанию + создаем заранее нужные категории и товары к ним
import os
import sys
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
django.setup()
from django.contrib.auth import get_user_model
from apps.catalog.models import Category, Product, ExchangeRate, CategorySpecField
from apps.stores.models import Store, StoreStaff, StoreProduct
User = get_user_model()
# ── Admin/ Администратор ──
#* создаем админа/ create admin
if not User.objects.filter(username='admin').exists():
User.objects.create_superuser('admin', 'admin@megashop.local', 'admin123', email_confirmed=True)
print('✓ Admin: admin / admin123')
else:
print('✓ Admin already exists')
# ── Categories/ Категории для первого запуска ──
cats = {
'laptops': {'name': 'Laptops', 'icon': 'bi-laptop'},
'smartphones': {'name': 'Smartphones', 'icon': 'bi-phone'},
'gaming': {'name': 'Gaming', 'icon': 'bi-controller'},
'audio': {'name': 'Audio', 'icon': 'bi-headphones'},
'accessories': {'name': 'Accessories', 'icon': 'bi-mouse'},
'tablets': {'name': 'Tablets', 'icon': 'bi-tablet'},
'consoles': {'name': 'Consoles', 'icon': 'bi-controller'},
}
created_cats = {}
for slug, data in cats.items():
cat, created = Category.objects.get_or_create(slug=slug, defaults={
'name': data['name'], 'is_active': True,
})
created_cats[slug] = cat
if created:
print(f' ✓ Category: {data["name"]}')
# ── Products/ Продукты для первого запуска ──
products = [
{
'name': 'MacBook Pro 16" M4 Max',
'slug': 'macbook-pro-16-m4',
'cat': 'laptops',
'price': 4499,
'stock': 15,
'specs': {
"Screen size": '16.2"',
"Processor": 'M4 Max',
"RAM": '48 GB',
"Storage": '1 TB SSD',
"GPU": '40-core',
"Battery life": '22h',
"Weight": '2.14 kg'
}
},
{
'name': 'ThinkPad X1 Carbon Gen 12',
'slug': 'thinkpad-x1-carbon-gen-12',
'cat': 'laptops',
'price': 3299,
'stock': 10,
'specs': {
"Screen size": '14"',
"Processor": 'Intel Ultra 7',
"RAM": '32 GB',
"Storage": '512 GB SSD',
"GPU": 'Intel Arc',
"Battery life": '15h',
"Weight": '1.08 kg'
}
},
{
'name': 'iPhone 16 Pro Max',
'slug': 'iphone-16-pro-max',
'cat': 'smartphones',
'price': 1199,
'stock': 50,
'specs': {
"Screen size": '6.9"',
"Processor": 'A18 Pro',
"RAM": '8 GB',
"Storage": '256 GB',
"Camera": '48 MP main',
"Battery": '4685 mAh'
}
},
{
'name': 'Samsung Galaxy S25 Ultra',
'slug': 'samsung-galaxy-s25-ultra',
'cat': 'smartphones',
'price': 1299,
'stock': 40,
'specs': {
"Screen size": '6.9"',
"Processor": 'Snapdragon 8 Elite', "RAM": '12 GB',
"Storage": '256 GB',
"Camera": '200 MP main',
"Battery": '5000 mAh'
}
},
{
'name': 'Google Pixel 9 Pro XL',
'slug': 'google-pixel-9-pro-xl',
'cat': 'smartphones',
'price': 1099,
'stock': 25,
'specs': {
"Screen size": '6.8"',
"Processor": 'Tensor G4',
"RAM": '16 GB',
"Storage": '128 GB',
"Camera": '50 MP main',
"Battery": '5060 mAh'
}
},
{
'name': 'PlayStation 5 Pro',
'slug': 'ps5-pro',
'cat': 'consoles',
'price': 699,
'stock': 20,
'specs': {
"GPU": '16.7 TFLOPS RDNA 4',
"Storage": '2 TB SSD',
"RAM": '16 GB GDDR6',
"Ray tracing": True,
"Upscaling": 'PSSR AI-driven'
}
},
{
'name': 'Xbox Series X',
'slug': 'xbox-series-x',
'cat': 'consoles',
'price': 479,
'stock': 18,
'specs': {
"GPU": '12 TFLOPS RDNA 2',
"Storage": '1 TB SSD',
"RAM": '16 GB GDDR6',
"Quick Resume": True
}
},
{
'name': 'Nintendo Switch 2',
'slug': 'nintendo-switch-2',
'cat': 'gaming',
'price': 449,
'stock': 30,
'specs': {
"Screen": '8" LCD',
"Storage": '256 GB',
"Joy-Con": 'Magnetic',
"Backward compatible": True
}
},
{
'name': 'Sony WH-1000XM6',
'slug': 'sony-wh-1000xm6',
'cat': 'audio',
'price': 349,
'stock': 35,
'specs': {
"Driver": '30 mm',
"Noise cancelling": 'Auto NC Optimizer',
"Battery": '40h',
"Codec": 'LDAC'
}
},
{
'name': 'AirPods Pro 3',
'slug': 'airpods-pro-3',
'cat': 'audio',
'price': 249,
'stock': 45,
'specs': {
"Driver": 'Custom Apple',
"Noise cancelling": 'Adaptive ANC',
"Battery": '6h',
"Chip": 'H3'
}
},
{
'name': 'Logitech MX Master 3S',
'slug': 'logitech-mx-master-3s',
'cat': 'accessories',
'price': 99,
'stock': 60,
'specs': {
"Sensor": '8000 DPI',
"Buttons": '7',
"Battery": '70 days',
"Connection": 'Bluetooth + USB-C'
}
},
{
'name': 'Samsung Galaxy Tab S10 Ultra',
'slug': 'samsung-galaxy-tab-s10-ultra',
'cat': 'tablets',
'price': 1199,
'stock': 12,
'specs': {
"Screen": '14.6" Dynamic AMOLED',
"Processor": 'MediaTek Dimensity 9300+',
"RAM": '12 GB', "Storage": '256 GB'
}
},
]
for p in products:
cat = created_cats[p['cat']]
_, created = Product.objects.get_or_create(
slug=p['slug'],
defaults={
'name': p['name'],
'category': cat,
'price': p['price'],
'stock': p['stock'],
'specifications': p['specs'],
'is_available': True,
},
)
if created:
print(f' ✓ Product: {p["name"]}')
# ── Spec fields per category/ Поля характеристик категорий ──
spec_defs = {
'laptops':[
('Screen size', 'text'),
('Refresh rate', 'text'),
('Processor', 'text'),
('RAM', 'text'),
('Storage', 'text'),
('GPU', 'text'),
('Battery life', 'text'),
('Weight', 'text'),
],
'smartphones':[
('Screen size', 'text'),
('Processor', 'text'),
('RAM', 'text'),
('Storage', 'text'),
('Camera', 'text'),
('Battery', 'text'),
],
'gaming':[
('Console type', 'text'),
('GPU', 'text'),
('Storage', 'text'),
('RAM', 'text'),
('Backward compatible', 'boolean'),
],
'audio':[
('Driver', 'text'),
('Noise cancelling', 'text'),
('Battery', 'text'),
('Connection', 'text'),
],
'accessories':[
('Compatibility', 'text'),
('Connection', 'text'),
('Weight', 'text'),
],
'tablets':[
('Screen', 'text'),
('Processor', 'text'),
('RAM', 'text'),
('Storage', 'text'),
],
'consoles':[
('GPU', 'text'),
('Storage', 'text'),
('RAM', 'text'),
('Ray tracing', 'boolean'),
('Upscaling', 'text'),
],
}
for slug, fields in spec_defs.items():
cat = created_cats.get(slug)
if not cat:
continue
for idx, (name, ftype) in enumerate(fields):
_, created = CategorySpecField.objects.get_or_create(
category=cat, field_name=name,
defaults={'field_type': ftype, 'sort_order': idx},
)
if created:
print(f' ✓ Spec field: {cat.name} → {name}')
# ── Exchange rates/ Курсы валют ──
for code, rate, symbol in [('EUR', 0.92, '€'), ('GBP', 0.79, '£')]:
_, created = ExchangeRate.objects.get_or_create(
currency=code,
defaults={'rate': rate, 'symbol': symbol, 'is_active': True},
)
if created:
print(f' [+]Exchange rate: {code} = {rate}')
print(f' [+]Валюта: {code} = {rate}')
# ── Stores/ Магазины ──
stores = [
{
'name': 'MegaShop London Oxford St',
'slug': 'london-oxford',
'city': 'London',
'address': '200 Oxford Street, London W1D 1NU',
'phone': '+44 20 7946 0958'
},
{
'name': 'MegaShop Manchester',
'slug': 'manchester',
'city': 'Manchester',
'address': '20 Market Street, Manchester M1 1WR',
'phone': '+44 161 234 5678'
},
{
'name': 'MegaShop Birmingham',
'slug': 'birmingham',
'city': 'Birmingham',
'address': '150 High Street, Birmingham B4 7TA',
'phone': '+44 121 345 6789'
},
]
for s in stores:
store, created = Store.objects.get_or_create(
slug=s['slug'],
defaults={
'name': s['name'],
'city': s['city'],
'address': s['address'],
'phone': s['phone'],
'is_active': True,
},
)
if created:
print(f'[+]Store: {store.name}')
print('\n Seed complete. Admin: admin / admin123')
print(' Login at http://localhost:8000/accounts/login/')
print('\n Первые данные созданы. Администратор: admin / admin123')
print(' Перейдите по http://localhost:8000/accounts/login/')