improvement to WC migration
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'}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Product
|
model = Product
|
||||||
exclude = ['created', 'updated', 'creator']
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
class SearchResultSerializer(TaggitSerializer, serializers.ModelSerializer):
|
class SearchResultSerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -62,11 +64,23 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
|||||||
print(f"Could not load products fom {url}: [{response.status_code}]")
|
print(f"Could not load products fom {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}
|
# 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:
|
||||||
@@ -77,20 +91,6 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
|||||||
tags = instance_data.pop('tags')
|
tags = instance_data.pop('tags')
|
||||||
attributes = instance_data.pop('attributes')
|
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():
|
||||||
@@ -129,6 +129,10 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
|||||||
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