integration starts to work

This commit is contained in:
Sam
2021-02-16 13:23:40 +00:00
parent 6e54d513c9
commit abd0c6312c
3 changed files with 25 additions and 15 deletions

View File

@@ -48,8 +48,8 @@ class Company(models.Model):
updated = models.DateTimeField('date last update', auto_now=True)
creator = models.ForeignKey('core.CustomUser', on_delete=models.DO_NOTHING, null=True, related_name='creator')
def __str__(self):
return self.company_name
# def __str__(self):
# return self.company_name
class Meta:
verbose_name = "Compañía"

View File

@@ -35,7 +35,7 @@ class Product(models.Model):
discount = models.DecimalField('Descuento', max_digits=5, decimal_places=2, null=True, blank=True)
stock = models.PositiveIntegerField('Stock', null=True)
tags = TagField(to=TreeTag)
category = SingleTagField(null=True) # main tag category
category = SingleTagField(blank=True, null=True) # main tag category
attributes = TagField(to=TreeTag, related_name='product_attributes')
identifiers = models.TextField('Identificador único de producto', null=True, blank=True)

View File

@@ -57,22 +57,32 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
return None
product_fields = [f.name for f in Product._meta.get_fields()]
counter = 0
products_created = []
for product in products:
instance_data = {}
# remove unwanted fields
product.pop('id')
instance_data = {'company':company}
# parse the product info
for key in product:
if key in product_fields:
instance_data[key] = product[key]
# validate data
import ipdb; ipdb.set_trace()
serializer = ProductSerializer(**instance_data)
if serializer.is_valid():
import ipdb; ipdb.set_trace()
Product.objects.create(**serializer.validated_data)
else:
import ipdb; ipdb.set_trace()
# remove unwanted fields
instance_data.pop('id')
if instance_data.get('tags') == []:
instance_data.pop('tags')
if instance_data.get('attributes') == []:
instance_data.pop('attributes')
# create instance
try:
new = Product.objects.create(**instance_data)
products_created.append(new)
counter += 1
logging.info(f"Product {instance_data.get('name')} created")
print(f"Product {instance_data.get('name')} created")
except Exception as e:
logging.error(f"Could not create product instance: {str(e)}")
print(f"Products created: {counter}")
return products_created