diff --git a/products/utils.py b/products/utils.py index 4e17900..96200d9 100644 --- a/products/utils.py +++ b/products/utils.py @@ -11,6 +11,9 @@ import requests from products.models import Product from companies.models import Company from history.models import HistorySync +from PIL import Image +from io import BytesIO +from django.core.files import File def extract_search_filters(result_set): @@ -217,7 +220,7 @@ def find_related_products_v4(keyword): return set(products_qs) -def product_loader(csv_reader, user): +def product_loader(csv_reader, user=None, company=None): """ Parse csv data and extract: @@ -232,33 +235,40 @@ def product_loader(csv_reader, user): for row in csv_reader: # trim strings for key in row: - if row[key]: row[key] = row[key].strip().lower() + if row[key]: + if 'imagen' in key or 'categoria' in key: + row[key] = row[key].strip() + else: + row[key] = row[key].strip().lower() # check required data if '' in (row['nombre-producto'], row['descripcion'], row['precio'], row['categoria']): logging.error(f"Required data missing: {row}") continue - + #import ipdb; ipdb.set_trace() try: # TODO: if tags is empty, auto-generate tags + if not company: + company = user.company # assemble instance data product_data = { - 'company': user.company, + 'company': company, 'name': row['nombre-producto'].strip(), 'description': row['descripcion'].strip(), - #'url': row['url'].strip(), - #'price': row['precio'].strip(), - #'shipping_cost': row['gastos-envio'].strip(), - #'shipping_terms': row['cond-envio'].strip(), - #'discount': row['descuento'].strip(), - #'stock': row['stock'].strip(), - #'tags': row['tags'].strip(), - #'category': row['categoria'].strip(), - #'identifiers': row['identificadores'].strip(), + 'url': row['url'].strip(), + 'price': float(row['precio'].strip().replace(',','.')), + 'shipping_cost': float(row['gastos-envio'].strip().replace(',','.')), + 'shipping_terms': row['cond-envio'].strip(), + 'discount': row['descuento'].strip(), + 'stock': row['stock'].strip(), + 'tags': row['tags'].strip(), + 'category': row['categoria'].strip(), + 'identifiers': row['identificadores'].strip(), #'history': history, 'creator': user, } + product = Product.objects.create(**product_data) # image logo data @@ -282,7 +292,7 @@ def product_loader(csv_reader, user): logging.info(f"Created Product {product.id}") counter += 1 except Exception as e: - import ipdb; ipdb.set_trace() + #import ipdb; ipdb.set_trace() logging.error(f"Could not parse {row}") history.quantity = counter