51 lines
2.1 KiB
Python
51 lines
2.1 KiB
Python
from django.core.management.base import BaseCommand
|
|
from core.models import Category, Product
|
|
from core.pexels import fetch_first
|
|
import random
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Seeds the database with initial categories and products'
|
|
|
|
def handle(self, *args, **kwargs):
|
|
categories_data = [
|
|
{'name': 'Tech', 'description': 'Latest gadgets and hardware.'},
|
|
{'name': 'Lifestyle', 'description': 'Modern living essentials.'},
|
|
{'name': 'Minimalist', 'description': 'Simple and clean aesthetics.'},
|
|
]
|
|
|
|
for cat_data in categories_data:
|
|
category, created = Category.objects.get_or_create(
|
|
name=cat_data['name'],
|
|
defaults={'description': cat_data['description']}
|
|
)
|
|
if created:
|
|
self.stdout.write(f"Created category: {category.name}")
|
|
|
|
# Sample products
|
|
products_data = [
|
|
('Minimal Desk Lamp', 'Tech', 89.99, 'minimalist desk lamp'),
|
|
('Mechanical Keyboard', 'Tech', 159.00, 'mechanical keyboard'),
|
|
('Premium Notebook', 'Minimalist', 25.50, 'aesthetic notebook'),
|
|
('Leather Wallet', 'Lifestyle', 45.00, 'leather wallet'),
|
|
('Wireless Earbuds', 'Tech', 129.00, 'modern wireless earbuds'),
|
|
('Ceramic Coffee Mug', 'Minimalist', 18.00, 'ceramic mug minimalist'),
|
|
]
|
|
|
|
for title, cat_name, price, query in products_data:
|
|
category = Category.objects.get(name=cat_name)
|
|
if not Product.objects.filter(title=title).exists():
|
|
img_data = fetch_first(query)
|
|
img_url = ""
|
|
if img_data:
|
|
img_url = img_data['local_path']
|
|
|
|
Product.objects.create(
|
|
title=title,
|
|
category=category,
|
|
price=price,
|
|
description=f"This is a premium {title.lower()} designed for quality and style.",
|
|
stock=random.randint(10, 50),
|
|
image_url=img_url
|
|
)
|
|
self.stdout.write(f"Created product: {title}")
|