37 lines
1.7 KiB
Python
37 lines
1.7 KiB
Python
from django.core.management.base import BaseCommand
|
|
from core.models import Vendor, Product
|
|
from core.pexels import fetch_first
|
|
import os
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Fetches sample images from Pexels for existing products and vendors'
|
|
|
|
def handle(self, *args, **options):
|
|
self.stdout.write("Fetching images for vendors...")
|
|
for vendor in Vendor.objects.all():
|
|
if not vendor.logo:
|
|
self.stdout.write(f"Fetching logo for vendor: {vendor.name}")
|
|
result = fetch_first(f"shop logo {vendor.name}", orientation="square")
|
|
if result:
|
|
vendor.logo = result['local_path']
|
|
vendor.save()
|
|
self.stdout.write(self.style.SUCCESS(f"Updated logo for {vendor.name}"))
|
|
else:
|
|
self.stdout.write(self.style.WARNING(f"Could not fetch logo for {vendor.name}"))
|
|
|
|
self.stdout.write("Fetching images for products...")
|
|
for product in Product.objects.all():
|
|
if not product.image:
|
|
self.stdout.write(f"Fetching image for product: {product.name}")
|
|
# Use a specific query based on the product name and category
|
|
query = f"{product.category.name} {product.name}"
|
|
result = fetch_first(query, orientation="landscape")
|
|
if result:
|
|
product.image = result['local_path']
|
|
product.save()
|
|
self.stdout.write(self.style.SUCCESS(f"Updated image for {product.name}"))
|
|
else:
|
|
self.stdout.write(self.style.WARNING(f"Could not fetch image for {product.name}"))
|
|
|
|
self.stdout.write(self.style.SUCCESS('Successfully updated all missing images.'))
|