improvements to coop product loader
This commit is contained in:
@@ -41,7 +41,7 @@ class Company(models.Model):
|
|||||||
shop_rss_feed = models.URLField('RSS tienda online', null=True, blank=True)
|
shop_rss_feed = models.URLField('RSS tienda online', null=True, blank=True)
|
||||||
sale_terms = models.TextField('Condiciones de venta', null=True, blank=True)
|
sale_terms = models.TextField('Condiciones de venta', null=True, blank=True)
|
||||||
shipping_cost = models.DecimalField('Gastos de envío', max_digits=10, decimal_places=2, null=True, blank=True)
|
shipping_cost = models.DecimalField('Gastos de envío', max_digits=10, decimal_places=2, null=True, blank=True)
|
||||||
tags = TagField(force_lowercase=True,max_count=5, tree=True, null=True, blank=True)
|
tags = TagField(force_lowercase=True,max_count=5, tree=True, blank=True)
|
||||||
sync = models.BooleanField('Sincronizar tienda', default=False, null=True, blank=True)
|
sync = models.BooleanField('Sincronizar tienda', default=False, null=True, blank=True)
|
||||||
is_validated = models.BooleanField('Validado', default=False, null=True, blank=True)
|
is_validated = models.BooleanField('Validado', default=False, null=True, blank=True)
|
||||||
is_active = models.BooleanField('Activado', default=False, null=True, blank=True)
|
is_active = models.BooleanField('Activado', default=False, null=True, blank=True)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import datetime
|
import datetime
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector, TrigramSimilarity
|
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector, TrigramSimilarity
|
||||||
@@ -16,6 +17,14 @@ from io import BytesIO
|
|||||||
from django.core.files import File
|
from django.core.files import File
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
filename='logs/csv-load.log',
|
||||||
|
filemode='w',
|
||||||
|
format='%(levelname)s:%(message)s',
|
||||||
|
level=logging.INFO,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def extract_search_filters(result_set):
|
def extract_search_filters(result_set):
|
||||||
"""
|
"""
|
||||||
Returned object should look something like:
|
Returned object should look something like:
|
||||||
@@ -220,7 +229,7 @@ def find_related_products_v4(keyword):
|
|||||||
return set(products_qs)
|
return set(products_qs)
|
||||||
|
|
||||||
|
|
||||||
def product_loader(csv_reader, user=None, company=None):
|
def product_loader(csv_reader, user, company=None):
|
||||||
"""
|
"""
|
||||||
Parse csv data and extract:
|
Parse csv data and extract:
|
||||||
|
|
||||||
@@ -229,8 +238,16 @@ def product_loader(csv_reader, user=None, company=None):
|
|||||||
Return counts
|
Return counts
|
||||||
"""
|
"""
|
||||||
counter = 0
|
counter = 0
|
||||||
|
# get company
|
||||||
|
if company is None and user is not None:
|
||||||
|
if user.company is not None:
|
||||||
|
company = user.company
|
||||||
|
else:
|
||||||
|
# cannot add products without a company
|
||||||
|
return None
|
||||||
|
|
||||||
# create historysync instance
|
# create historysync instance
|
||||||
history = HistorySync.objects.create(company=user.company, sync_date=datetime.datetime.now())
|
history = HistorySync.objects.create(company=company, sync_date=datetime.datetime.now())
|
||||||
|
|
||||||
for row in csv_reader:
|
for row in csv_reader:
|
||||||
# trim strings
|
# trim strings
|
||||||
@@ -238,37 +255,35 @@ def product_loader(csv_reader, user=None, company=None):
|
|||||||
if row[key]:
|
if row[key]:
|
||||||
if 'imagen' in key or 'categoria' in key:
|
if 'imagen' in key or 'categoria' in key:
|
||||||
row[key] = row[key].strip()
|
row[key] = row[key].strip()
|
||||||
|
elif key in ['precio', 'gastos-envio']:
|
||||||
|
# dec = row[key][:-1].strip()
|
||||||
|
row[key] = row[key][:-1].strip()
|
||||||
else:
|
else:
|
||||||
row[key] = row[key].strip().lower()
|
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'],):
|
||||||
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': 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': float(row['precio'].strip().replace(',','.')),
|
'price': float(row['precio'].strip().replace(',','.')),
|
||||||
'shipping_cost': float(row['gastos-envio'].strip().replace(',','.')),
|
'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
|
||||||
@@ -292,8 +307,7 @@ def product_loader(csv_reader, user=None, company=None):
|
|||||||
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()
|
logging.error(f"Could not parse {counter}: {str(e)}")
|
||||||
logging.error(f"Could not parse {row}")
|
|
||||||
|
|
||||||
history.quantity = counter
|
history.quantity = counter
|
||||||
history.save()
|
history.save()
|
||||||
|
|||||||
@@ -81,8 +81,9 @@ def load_coop_products(request):
|
|||||||
decoded_file = csv_file.read().decode('utf-8').splitlines()
|
decoded_file = csv_file.read().decode('utf-8').splitlines()
|
||||||
csv_reader = csv.DictReader(decoded_file, delimiter=',')
|
csv_reader = csv.DictReader(decoded_file, delimiter=',')
|
||||||
count = product_loader(csv_reader, request.user)
|
count = product_loader(csv_reader, request.user)
|
||||||
|
if count is None:
|
||||||
return Response(f"{count} products registered for {request.user.company_name}")
|
return Response({"errors": {"details": "Authenticated user is not related to any company"}}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
return Response(f"{count} products registered for {request.user.company.company_name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"errors": {"details": str(type(e))}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response({"errors": {"details": str(type(e))}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user