importing woocommerce products is working
This commit is contained in:
@@ -19,6 +19,8 @@ from companies.serializers import CompanySerializer
|
|||||||
from utils.tag_filters import CompanyTagFilter
|
from utils.tag_filters import CompanyTagFilter
|
||||||
from back_latienda.permissions import IsCreator
|
from back_latienda.permissions import IsCreator
|
||||||
|
|
||||||
|
from utils import woocommerce
|
||||||
|
|
||||||
|
|
||||||
class CompanyViewSet(viewsets.ModelViewSet):
|
class CompanyViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Company.objects.all()
|
queryset = Company.objects.all()
|
||||||
@@ -120,23 +122,34 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"errors":{"details": str(e),}}, status=500)
|
return Response({"errors":{"details": str(e),}}, status=500)
|
||||||
|
|
||||||
@action(detail=True, methods=['POST', ])
|
@action(detail=True, methods=['GET', ])
|
||||||
def import_products(self, request, **kwargs):
|
def import_products(self, request, **kwargs):
|
||||||
instance = self.queryset.filter(pk=kwargs['pk']).first()
|
instance = self.queryset.filter(pk=kwargs['pk']).first()
|
||||||
# check if it's a shop
|
# check if it's a shop
|
||||||
if instance.shop is not True:
|
if instance.shop is not True:
|
||||||
return Response('This company is not a shop')
|
return Response({'error': 'This company is not a shop'})
|
||||||
# check what platform
|
|
||||||
platform = instance.platform
|
|
||||||
if platform is None:
|
|
||||||
return Response('This company is not registered with any platforms')
|
|
||||||
# check required credentials
|
# check required credentials
|
||||||
credentials = instance.credentials
|
credentials = instance.credentials
|
||||||
if credentials is None or credentials == {}:
|
if credentials is None or credentials == {}:
|
||||||
return Response('This company has no registered credentials')
|
return Response({'error': 'This company has no registered credentials'})
|
||||||
|
# check what platform
|
||||||
|
platform = instance.platform
|
||||||
|
if platform is None:
|
||||||
|
message = {'error': 'This company is not registered with any platforms'}
|
||||||
|
elif platform == 'WOO_COMMERCE':
|
||||||
|
# recheck credentials
|
||||||
|
if 'key' in credentials.keys() and 'secret' in credentials.keys():
|
||||||
# execute import
|
# execute import
|
||||||
|
products = woocommerce.migrate_shop_products(
|
||||||
return Response()
|
instance.web_link,
|
||||||
|
credentials['key'],
|
||||||
|
credentials['secret'])
|
||||||
|
message = {'details': f'{len(products)} products added for {instance.company_name}'}
|
||||||
|
else:
|
||||||
|
message = {"error": 'Credentials have wrong format'}
|
||||||
|
else:
|
||||||
|
message = {'error': f'Platform {plaform} not registered'}
|
||||||
|
return Response(message)
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET',])
|
@api_view(['GET',])
|
||||||
|
|||||||
@@ -67,24 +67,40 @@ def migrate_shop_products(url, key, secret, 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')
|
||||||
if instance_data.get('tags') == []:
|
# extract m2m field data
|
||||||
instance_data.pop('tags')
|
tags = instance_data.pop('tags')
|
||||||
if instance_data.get('attributes') == []:
|
attributes = instance_data.pop('attributes')
|
||||||
instance_data.pop('attributes')
|
|
||||||
# create instance with serializer
|
# create instance with serializer
|
||||||
|
'''
|
||||||
serializer = ProductSerializer(data=instance_data)
|
serializer = ProductSerializer(data=instance_data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
new = serializer.save()
|
new = serializer.save()
|
||||||
|
if tags:
|
||||||
|
new.tags.set(tags)
|
||||||
|
if attributes:
|
||||||
|
new.attributes.set(attributes)
|
||||||
|
new.save()
|
||||||
else:
|
else:
|
||||||
logging.error(f"{serializer.errors}")
|
logging.error(f"{serializer.errors}")
|
||||||
continue
|
continue
|
||||||
'''
|
'''
|
||||||
# alternative method
|
# alternative method
|
||||||
|
serializer = ProductSerializer(data=instance_data)
|
||||||
|
if serializer.is_valid():
|
||||||
try:
|
try:
|
||||||
new = Product.objects.create(**instance_data)
|
new = Product.objects.create(**serializer.validated_data)
|
||||||
|
if tags:
|
||||||
|
new.tags.set(tags)
|
||||||
|
if attributes:
|
||||||
|
new.attributes.set(attributes)
|
||||||
|
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)}")
|
||||||
'''
|
else:
|
||||||
|
logging.error(f"{serializer.errors}")
|
||||||
|
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")
|
||||||
|
|||||||
Reference in New Issue
Block a user