Method to import products tested from shell

This commit is contained in:
pablogg
2021-02-28 19:42:22 +01:00
parent c61ea440d4
commit 2b68413aea

View File

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