merge conflict
This commit is contained in:
@@ -144,7 +144,9 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
|||||||
products = woocommerce.migrate_shop_products(
|
products = woocommerce.migrate_shop_products(
|
||||||
instance.web_link,
|
instance.web_link,
|
||||||
credentials['key'],
|
credentials['key'],
|
||||||
credentials['secret'])
|
credentials['secret'],
|
||||||
|
request.user,
|
||||||
|
)
|
||||||
message = {'details': f'{len(products)} products added for {instance.company_name}'}
|
message = {'details': f'{len(products)} products added for {instance.company_name}'}
|
||||||
else:
|
else:
|
||||||
message = {"error": 'Credentials have wrong format'}
|
message = {"error": 'Credentials have wrong format'}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
exclude = ['updated', 'creator']
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
class SearchResultSerializer(TaggitSerializer, serializers.ModelSerializer):
|
class SearchResultSerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ This file holds the functions necesary to:
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
import datetime
|
||||||
|
|
||||||
from woocommerce import API
|
from woocommerce import API
|
||||||
import requests
|
import requests
|
||||||
@@ -17,6 +18,7 @@ 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
|
||||||
from products.serializers import ProductSerializer
|
from products.serializers import ProductSerializer
|
||||||
|
from history.models import HistorySync
|
||||||
|
|
||||||
|
|
||||||
def get_wcapi_instance(url, key, secret, version="wc/v3"):
|
def get_wcapi_instance(url, key, secret, version="wc/v3"):
|
||||||
@@ -30,7 +32,7 @@ def get_wcapi_instance(url, key, secret, version="wc/v3"):
|
|||||||
return wcapi
|
return wcapi
|
||||||
|
|
||||||
|
|
||||||
def migrate_shop_products(url, key, secret, version="wc/v3"):
|
def migrate_shop_products(url, key, secret, user=None, version="wc/v3"):
|
||||||
"""Tries to connect to WooCommerce site @ url with given credentials
|
"""Tries to connect to WooCommerce site @ url with given credentials
|
||||||
|
|
||||||
If succesful, returns list of Product instances created
|
If succesful, returns list of Product instances created
|
||||||
@@ -51,64 +53,60 @@ def migrate_shop_products(url, key, secret, 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
|
||||||
|
history = HistorySync.objects.create(
|
||||||
|
company=company,
|
||||||
|
sync_date=datetime.datetime.now(),
|
||||||
|
)
|
||||||
|
|
||||||
product_fields = [f.name for f in Product._meta.get_fields()]
|
product_fields = [f.name for f in Product._meta.get_fields()]
|
||||||
counter = 0
|
counter = 0
|
||||||
products_created = []
|
products_created = []
|
||||||
for product in products:
|
for product in products:
|
||||||
instance_data = {'company':company.id}
|
# 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,
|
||||||
|
'creator': user.id if user is not None else None,
|
||||||
|
'history': history.id,
|
||||||
|
'url': product['permalink'],
|
||||||
|
}
|
||||||
# parse the product info
|
# parse the product info
|
||||||
for key in product:
|
for key in product:
|
||||||
if key in product_fields:
|
if key in product_fields:
|
||||||
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')
|
|
||||||
|
|
||||||
# create instance with serializer
|
|
||||||
'''
|
|
||||||
serializer = ProductSerializer(data=instance_data)
|
|
||||||
if serializer.is_valid():
|
|
||||||
new = serializer.save()
|
|
||||||
if tags:
|
|
||||||
new.tags.set(tags)
|
|
||||||
if attributes:
|
|
||||||
new.attributes.set(attributes)
|
|
||||||
new.save()
|
|
||||||
else:
|
|
||||||
logging.error(f"{serializer.errors}")
|
|
||||||
continue
|
|
||||||
'''
|
|
||||||
# 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,18 @@ def migrate_shop_products(url, key, secret, 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
|
||||||
|
history.quantity = counter
|
||||||
|
history.save()
|
||||||
|
|
||||||
print(f"Products created: {counter}")
|
print(f"Products created: {counter}")
|
||||||
return products_created
|
return products_created
|
||||||
|
|||||||
Reference in New Issue
Block a user