more improvements to WC migration

This commit is contained in:
Sam
2021-02-24 11:54:11 +00:00
parent 873ec25a92
commit 62eb739978
2 changed files with 15 additions and 15 deletions

View File

@@ -14,6 +14,7 @@ drf-extra-fields==3.0.4
django-ipware==3.0.2
geoip2==4.1.0
woocommerce==2.1.1
# manually install `pip install --default-timeout=100 future` to avoid wcapi to timeout
# required for production
django-anymail[amazon_ses]==8.2
boto3==1.17.11

View File

@@ -53,15 +53,16 @@ def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
return None
# list products
response = wcapi.get('/products/')
response = wcapi.get('products/')
if response.status_code == 200:
products = response.json()
elif response.status_code == 401:
logging.error(f"{response.status_code} [{response.url}]: {response.json()}")
return None
else:
logging.error(f"Could not load products from {url}: [{response.status_code}]")
print(f"Could not load products fom {url}: [{response.status_code}]")
import ipdb; ipdb.set_trace()
logging.error(f"Could not load products from {response.url}: [{response.status_code}]")
print(f"Could not load products fom {response.url}: [{response.status_code}]")
return None
# create HistorySync instance and link to every product created
@@ -74,6 +75,9 @@ def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
counter = 0
products_created = []
for product in products:
# extract m2m field data
tags = [t.get('name') for t in product.pop('tags')]
attributes = [t.get('name') for t in product.pop('attributes')]
# prepare instance data
instance_data = {
'company':company.id,
@@ -87,28 +91,22 @@ def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
instance_data[key] = product[key]
# remove unwanted fields
instance_data.pop('id')
# extract m2m field data
tags = instance_data.pop('tags')
attributes = instance_data.pop('attributes')
# alternative method
serializer = ProductSerializer(data=instance_data)
if serializer.is_valid():
try:
new = Product.objects.create(**serializer.validated_data)
if tags:
new.tags.set(tags)
if attributes:
new.attributes.set(attributes)
new.tags = tags
new.attributes = attributes
new.save()
except Exception as e:
import ipdb; ipdb.set_trace()
logging.error(f"Could not create product instance: {str(e)}")
continue
try:
# 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"
}
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
@@ -121,13 +119,14 @@ def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
except Exception as e:
logging.error(f"Could not add image to product: {str(e)}")
else:
import ipdb; ipdb.set_trace()
logging.error(f"{serializer.errors}")
continue
products_created.append(new)
counter += 1
logging.info(f"Product {instance_data.get('name')} created")
print(f"Product {instance_data.get('name')} created")
logging.info(f"Product '{instance_data.get('name')}' created")
print(f"Product '{instance_data.get('name')}' created")
# update history.quantity
history.quantity = counter