|
|
|
|
@ -34,10 +34,11 @@ class Command(BaseCommand): |
|
|
|
|
self.stdout.write(f'Created color: {color_data["name"]}') |
|
|
|
|
else: |
|
|
|
|
# Update existing colors with secondary color if needed |
|
|
|
|
if color.secondary_hex_color != color_data['secondary_hex']: |
|
|
|
|
if color.colorHex != color_data['hex'] or color.secondary_hex_color != color_data['secondary_hex']: |
|
|
|
|
color.colorHex = color_data['hex'] |
|
|
|
|
color.secondary_hex_color = color_data['secondary_hex'] |
|
|
|
|
color.save() |
|
|
|
|
self.stdout.write(f'Updated color: {color_data["name"]} with secondary color') |
|
|
|
|
self.stdout.write(f'Updated color: {color_data["name"]}') |
|
|
|
|
else: |
|
|
|
|
self.stdout.write(f'Color already exists: {color_data["name"]}') |
|
|
|
|
|
|
|
|
|
@ -58,38 +59,46 @@ class Command(BaseCommand): |
|
|
|
|
self.stdout.write('Creating products...') |
|
|
|
|
products = [ |
|
|
|
|
{ |
|
|
|
|
'sku': 'PC001', |
|
|
|
|
'title': 'Tennis Racket Pro', |
|
|
|
|
'description': 'Professional grade tennis racket with advanced stability control.', |
|
|
|
|
'price': 99.99, |
|
|
|
|
'ordering_value': 1, |
|
|
|
|
'cut': 2, # Men |
|
|
|
|
'colors': ['Black', 'White', 'Red', 'Black/White'], |
|
|
|
|
'colors': ['Black/White'], |
|
|
|
|
'sizes': ['M', 'L', 'XL'], |
|
|
|
|
'image_filename': 'hat.jpg' # Just the filename |
|
|
|
|
'image_filename': 'hat.jpg' |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
'sku': 'PC002', |
|
|
|
|
'title': 'Sports T-Shirt', |
|
|
|
|
'description': 'Breathable sports t-shirt made with moisture-wicking fabric.', |
|
|
|
|
'price': 29.99, |
|
|
|
|
'ordering_value': 2, |
|
|
|
|
'cut': 1, # Women |
|
|
|
|
'colors': ['Black', 'White', 'Blue', 'Red', 'Red/Blue'], |
|
|
|
|
'sizes': ['XS', 'S', 'M', 'L', 'XL'], |
|
|
|
|
'image_filename': 'tshirt.jpg' # Just the filename |
|
|
|
|
'image_filename': 'tshirt.jpg' |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
'sku': 'PC003', |
|
|
|
|
'title': 'Kids Tennis Shorts', |
|
|
|
|
'description': 'Comfortable tennis shorts for kids with elastic waistband.', |
|
|
|
|
'price': 19.99, |
|
|
|
|
'ordering_value': 3, |
|
|
|
|
'cut': 3, # Kids |
|
|
|
|
'colors': ['Blue', 'White', 'Green/Yellow'], |
|
|
|
|
'sizes': ['XS', 'S', 'M'], |
|
|
|
|
'image_filename': 'kids_shorts.jpg' # Just the filename |
|
|
|
|
'image_filename': 'kids_shorts.jpg' |
|
|
|
|
} |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
for product_data in products: |
|
|
|
|
product, created = Product.objects.get_or_create( |
|
|
|
|
title=product_data['title'], |
|
|
|
|
product, created = Product.objects.update_or_create( |
|
|
|
|
sku=product_data['sku'], |
|
|
|
|
defaults={ |
|
|
|
|
'title': product_data['title'], |
|
|
|
|
'description': product_data.get('description', ''), |
|
|
|
|
'price': product_data['price'], |
|
|
|
|
'ordering_value': product_data['ordering_value'], |
|
|
|
|
'cut': product_data['cut'] |
|
|
|
|
@ -97,35 +106,30 @@ class Command(BaseCommand): |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if created: |
|
|
|
|
self.stdout.write(f'Created product: {product_data["title"]}') |
|
|
|
|
|
|
|
|
|
# Add colors |
|
|
|
|
for color_name in product_data['colors']: |
|
|
|
|
product.colors.add(color_objects[color_name]) |
|
|
|
|
|
|
|
|
|
# Add sizes |
|
|
|
|
for size_name in product_data['sizes']: |
|
|
|
|
product.sizes.add(size_objects[size_name]) |
|
|
|
|
self.stdout.write(f'Created product: {product_data["sku"]} - {product_data["title"]}') |
|
|
|
|
else: |
|
|
|
|
self.stdout.write(f'Updated product: {product_data["sku"]} - {product_data["title"]}') |
|
|
|
|
|
|
|
|
|
# Construct the full path for storage |
|
|
|
|
if 'image_filename' in product_data and product_data['image_filename']: |
|
|
|
|
# Construct the URL path to the image |
|
|
|
|
# This uses STATIC_URL from your settings |
|
|
|
|
image_path = f"{settings.STATIC_URL}shop/images/products/{product_data['image_filename']}" |
|
|
|
|
print(image_path) |
|
|
|
|
# Store this path in the database |
|
|
|
|
# Handle the image path |
|
|
|
|
if 'image_filename' in product_data and product_data['image_filename']: |
|
|
|
|
image_path = f"{settings.STATIC_URL}shop/images/products/{product_data['image_filename']}" |
|
|
|
|
if product.image != image_path: |
|
|
|
|
product.image = image_path |
|
|
|
|
product.save() |
|
|
|
|
self.stdout.write(f'Updated image path to "{image_path}" for: {product_data["sku"]}') |
|
|
|
|
|
|
|
|
|
self.stdout.write(f'Added image path "{image_path}" for: {product_data["title"]}') |
|
|
|
|
else: |
|
|
|
|
self.stdout.write(f'Product already exists: {product_data["title"]}') |
|
|
|
|
# Update colors - first clear existing then add new ones |
|
|
|
|
product.colors.clear() |
|
|
|
|
for color_name in product_data['colors']: |
|
|
|
|
if color_name in color_objects: |
|
|
|
|
product.colors.add(color_objects[color_name]) |
|
|
|
|
self.stdout.write(f'Updated colors for: {product_data["sku"]}') |
|
|
|
|
|
|
|
|
|
# Update existing products with new colors if needed |
|
|
|
|
existing_colors = set(product.colors.all().values_list('name', flat=True)) |
|
|
|
|
for color_name in product_data['colors']: |
|
|
|
|
if color_name not in existing_colors: |
|
|
|
|
product.colors.add(color_objects[color_name]) |
|
|
|
|
self.stdout.write(f'Added color {color_name} to existing product: {product_data["title"]}') |
|
|
|
|
# Update sizes - first clear existing then add new ones |
|
|
|
|
product.sizes.clear() |
|
|
|
|
for size_name in product_data['sizes']: |
|
|
|
|
if size_name in size_objects: |
|
|
|
|
product.sizes.add(size_objects[size_name]) |
|
|
|
|
self.stdout.write(f'Updated sizes for: {product_data["sku"]}') |
|
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully created initial shop data')) |
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully created/updated shop data')) |
|
|
|
|
|