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 django-ipware==3.0.2
geoip2==4.1.0 geoip2==4.1.0
woocommerce==2.1.1 woocommerce==2.1.1
# manually install `pip install --default-timeout=100 future` to avoid wcapi to timeout
# required for production # required for production
django-anymail[amazon_ses]==8.2 django-anymail[amazon_ses]==8.2
boto3==1.17.11 boto3==1.17.11

View File

@@ -53,15 +53,16 @@ def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
return None return None
# list products # list products
response = wcapi.get('/products/') response = wcapi.get('products/')
if response.status_code == 200: if response.status_code == 200:
products = response.json() products = response.json()
elif response.status_code == 401: elif response.status_code == 401:
logging.error(f"{response.status_code} [{response.url}]: {response.json()}") logging.error(f"{response.status_code} [{response.url}]: {response.json()}")
return None return None
else: else:
logging.error(f"Could not load products from {url}: [{response.status_code}]") import ipdb; ipdb.set_trace()
print(f"Could not load products fom {url}: [{response.status_code}]") 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 return None
# create HistorySync instance and link to every product created # 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 counter = 0
products_created = [] products_created = []
for product in products: 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 # prepare instance data
instance_data = { instance_data = {
'company':company.id, 'company':company.id,
@@ -87,28 +91,22 @@ def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
instance_data[key] = product[key] instance_data[key] = product[key]
# remove unwanted fields # remove unwanted fields
instance_data.pop('id') instance_data.pop('id')
# extract m2m field data
tags = instance_data.pop('tags')
attributes = instance_data.pop('attributes')
# alternative method # alternative method
serializer = ProductSerializer(data=instance_data) serializer = ProductSerializer(data=instance_data)
if serializer.is_valid(): if serializer.is_valid():
try: try:
new = Product.objects.create(**serializer.validated_data) new = Product.objects.create(**serializer.validated_data)
if tags: new.tags = tags
new.tags.set(tags) new.attributes = attributes
if attributes:
new.attributes.set(attributes)
new.save() new.save()
except Exception as e: except Exception as e:
import ipdb; ipdb.set_trace()
logging.error(f"Could not create product instance: {str(e)}") logging.error(f"Could not create product instance: {str(e)}")
continue continue
try: try:
# get image # get image
headers={ headers={"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
"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'] image_url = product['images'][0]['src']
response = requests.get(image_url, stream=True, headers=headers) response = requests.get(image_url, stream=True, headers=headers)
response.raw.decode_content = True 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: except Exception as e:
logging.error(f"Could not add image to product: {str(e)}") logging.error(f"Could not add image to product: {str(e)}")
else: else:
import ipdb; ipdb.set_trace()
logging.error(f"{serializer.errors}") logging.error(f"{serializer.errors}")
continue continue
products_created.append(new) products_created.append(new)
counter += 1 counter += 1
logging.info(f"Product {instance_data.get('name')} created") logging.info(f"Product '{instance_data.get('name')}' created")
print(f"Product {instance_data.get('name')} created") print(f"Product '{instance_data.get('name')}' created")
# update history.quantity # update history.quantity
history.quantity = counter history.quantity = counter