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) updated = models.DateTimeField('date last update', auto_now=True)
creator = models.ForeignKey('core.CustomUser', on_delete=models.DO_NOTHING, null=True, related_name='creator') creator = models.ForeignKey('core.CustomUser', on_delete=models.DO_NOTHING, null=True, related_name='creator')
def __str__(self): # def __str__(self):
return self.company_name # return self.company_name
class Meta: class Meta:
verbose_name = "Compañía" 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) discount = models.DecimalField('Descuento', max_digits=5, decimal_places=2, null=True, blank=True)
stock = models.PositiveIntegerField('Stock', null=True) stock = models.PositiveIntegerField('Stock', null=True)
tags = TagField(to=TreeTag) 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') attributes = TagField(to=TreeTag, related_name='product_attributes')
identifiers = models.TextField('Identificador único de producto', null=True, blank=True) 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 return None
product_fields = [f.name for f in Product._meta.get_fields()] product_fields = [f.name for f in Product._meta.get_fields()]
counter = 0
products_created = []
for product in products: for product in products:
instance_data = {} instance_data = {'company':company}
# remove unwanted fields # parse the product info
product.pop('id')
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]
# validate data # remove unwanted fields
import ipdb; ipdb.set_trace() instance_data.pop('id')
serializer = ProductSerializer(**instance_data) if instance_data.get('tags') == []:
if serializer.is_valid(): instance_data.pop('tags')
import ipdb; ipdb.set_trace() if instance_data.get('attributes') == []:
Product.objects.create(**serializer.validated_data) instance_data.pop('attributes')
else: # create instance
import ipdb; ipdb.set_trace() 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