added image-grabbing code to woocommerve migration,images still saved locally
This commit is contained in:
@@ -52,6 +52,8 @@ INSTALLED_APPS = [
|
|||||||
'taggit_serializer',
|
'taggit_serializer',
|
||||||
'tagulous',
|
'tagulous',
|
||||||
'anymail',
|
'anymail',
|
||||||
|
'storages',
|
||||||
|
|
||||||
|
|
||||||
# local apps
|
# local apps
|
||||||
'core',
|
'core',
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from django.conf import settings
|
|||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
|
from products.models import Product
|
||||||
|
|
||||||
from utils.woocommerce import migrate_shop_products
|
from utils.woocommerce import migrate_shop_products
|
||||||
|
|
||||||
@@ -29,6 +30,9 @@ class Command(BaseCommand):
|
|||||||
help = 'Load data from example site https://woo.enreda.coop/ '
|
help = 'Load data from example site https://woo.enreda.coop/ '
|
||||||
|
|
||||||
def handle(self, *args, **kwargs):
|
def handle(self, *args, **kwargs):
|
||||||
|
print("Deleting existing Product instances")
|
||||||
|
Product.objects.all().delete()
|
||||||
|
|
||||||
print("Migrate data from Enreda WooCommerce...\n")
|
print("Migrate data from Enreda WooCommerce...\n")
|
||||||
# find or create company instance
|
# find or create company instance
|
||||||
enreda, created = Company.objects.get_or_create(company_name='enreda', web_link=self.url)
|
enreda, created = Company.objects.get_or_create(company_name='enreda', web_link=self.url)
|
||||||
|
|||||||
@@ -7,8 +7,12 @@ This file holds the functions necesary to:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
from woocommerce import API
|
from woocommerce import API
|
||||||
|
import requests
|
||||||
|
from PIL import Image
|
||||||
|
from django.core.files import File
|
||||||
|
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
@@ -42,12 +46,9 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
|||||||
company = Company.objects.filter(web_link=url).first()
|
company = Company.objects.filter(web_link=url).first()
|
||||||
|
|
||||||
if not company:
|
if not company:
|
||||||
# logging.error(f"Could not find Company with URL: {url}")
|
logging.error(f"Could not find Company with URL: {url}")
|
||||||
# print(f"Could not find Company with URL: {url}")
|
print(f"Could not find Company with URL: {url}")
|
||||||
# return None
|
return None
|
||||||
# TODO: ELIMINATE THIS AFTER DEBUGGING
|
|
||||||
company = Company.objects.create(web_link=url)
|
|
||||||
logging.error(f"Created Company for testing: {url}")
|
|
||||||
|
|
||||||
# list products
|
# list products
|
||||||
response = wcapi.get('/products/')
|
response = wcapi.get('/products/')
|
||||||
@@ -102,6 +103,24 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
|||||||
new.save()
|
new.save()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Could not create product instance: {str(e)}")
|
logging.error(f"Could not create product instance: {str(e)}")
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
# import ipdb; ipdb.set_trace()
|
||||||
|
# get image
|
||||||
|
headers={
|
||||||
|
"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
|
||||||
|
}
|
||||||
|
image_url = product['images'][0]['src']
|
||||||
|
response = requests.get(image_url, stream=True, headers=headers)
|
||||||
|
response.raw.decode_content = True
|
||||||
|
image = Image.open(response.raw)
|
||||||
|
# save using File object
|
||||||
|
img_io = BytesIO()
|
||||||
|
image.save(img_io, format='JPEG')
|
||||||
|
new.image.save(f"{new.name}-{new.sku}.jpg", File(img_io), save=False)
|
||||||
|
new.save()
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Could not add image to product: {str(e)}")
|
||||||
else:
|
else:
|
||||||
logging.error(f"{serializer.errors}")
|
logging.error(f"{serializer.errors}")
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user