|
|
|
@ -1,8 +1,6 @@ |
|
|
|
from django.core.management.base import BaseCommand |
|
|
|
from django.core.management.base import BaseCommand |
|
|
|
from shop.models import Color, Size, Product |
|
|
|
from shop.models import Color, Size, Product |
|
|
|
from django.core.files.base import ContentFile |
|
|
|
from django.conf import settings |
|
|
|
import os |
|
|
|
|
|
|
|
import urllib.request |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand): |
|
|
|
class Command(BaseCommand): |
|
|
|
help = 'Creates initial data for the shop' |
|
|
|
help = 'Creates initial data for the shop' |
|
|
|
@ -54,7 +52,7 @@ class Command(BaseCommand): |
|
|
|
'cut': 2, # Men |
|
|
|
'cut': 2, # Men |
|
|
|
'colors': ['Black', 'White', 'Red'], |
|
|
|
'colors': ['Black', 'White', 'Red'], |
|
|
|
'sizes': ['M', 'L', 'XL'], |
|
|
|
'sizes': ['M', 'L', 'XL'], |
|
|
|
'image_url': 'https://example.com/images/tennis_racket.jpg' |
|
|
|
'image_filename': 'hat.jpg' # Just the filename |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
'title': 'Sports T-Shirt', |
|
|
|
'title': 'Sports T-Shirt', |
|
|
|
@ -63,7 +61,7 @@ class Command(BaseCommand): |
|
|
|
'cut': 1, # Women |
|
|
|
'cut': 1, # Women |
|
|
|
'colors': ['Black', 'White', 'Blue', 'Red'], |
|
|
|
'colors': ['Black', 'White', 'Blue', 'Red'], |
|
|
|
'sizes': ['XS', 'S', 'M', 'L', 'XL'], |
|
|
|
'sizes': ['XS', 'S', 'M', 'L', 'XL'], |
|
|
|
'image_url': 'https://example.com/images/tshirt.jpg' |
|
|
|
'image_filename': 'tshirt.jpg' # Just the filename |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
'title': 'Kids Tennis Shorts', |
|
|
|
'title': 'Kids Tennis Shorts', |
|
|
|
@ -72,7 +70,7 @@ class Command(BaseCommand): |
|
|
|
'cut': 3, # Kids |
|
|
|
'cut': 3, # Kids |
|
|
|
'colors': ['Blue', 'White'], |
|
|
|
'colors': ['Blue', 'White'], |
|
|
|
'sizes': ['XS', 'S', 'M'], |
|
|
|
'sizes': ['XS', 'S', 'M'], |
|
|
|
'image_url': 'https://example.com/images/kids_shorts.jpg' |
|
|
|
'image_filename': 'kids_shorts.jpg' # Just the filename |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
@ -97,17 +95,17 @@ class Command(BaseCommand): |
|
|
|
for size_name in product_data['sizes']: |
|
|
|
for size_name in product_data['sizes']: |
|
|
|
product.sizes.add(size_objects[size_name]) |
|
|
|
product.sizes.add(size_objects[size_name]) |
|
|
|
|
|
|
|
|
|
|
|
# Add image (commented out, use if needed) |
|
|
|
# Construct the full path for storage |
|
|
|
""" |
|
|
|
if 'image_filename' in product_data and product_data['image_filename']: |
|
|
|
try: |
|
|
|
# Construct the URL path to the image |
|
|
|
result = urllib.request.urlretrieve(product_data['image_url']) |
|
|
|
# This uses STATIC_URL from your settings |
|
|
|
product.image.save( |
|
|
|
image_path = f"{settings.STATIC_URL}shop/images/products/{product_data['image_filename']}" |
|
|
|
os.path.basename(product_data['image_url']), |
|
|
|
print(image_path) |
|
|
|
ContentFile(open(result[0], 'rb').read()) |
|
|
|
# Store this path in the database |
|
|
|
) |
|
|
|
product.image = image_path |
|
|
|
except Exception as e: |
|
|
|
product.save() |
|
|
|
self.stdout.write(f'Error downloading image: {e}') |
|
|
|
|
|
|
|
""" |
|
|
|
self.stdout.write(f'Added image path "{image_path}" for: {product_data["title"]}') |
|
|
|
else: |
|
|
|
else: |
|
|
|
self.stdout.write(f'Product already exists: {product_data["title"]}') |
|
|
|
self.stdout.write(f'Product already exists: {product_data["title"]}') |
|
|
|
|
|
|
|
|
|
|
|
|