improvements to csv product loading
This commit is contained in:
@@ -434,11 +434,10 @@ class LoadCoopProductsTestCase(APITestCase):
|
||||
|
||||
# send in request
|
||||
response = self.client.post(self.endpoint, files)
|
||||
|
||||
# check response
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# check for object creation
|
||||
self.assertEquals(5, self.model.objects.count())
|
||||
self.assertEquals(9, self.model.objects.count())
|
||||
|
||||
def test_auth_user_with_no_company_cannot_load_csv(self):
|
||||
# delete existing instances
|
||||
@@ -455,7 +454,7 @@ class LoadCoopProductsTestCase(APITestCase):
|
||||
response = self.client.post(self.endpoint, files)
|
||||
|
||||
# check response
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.status_code, 406)
|
||||
# check for object creation
|
||||
self.assertEqual(0, self.model.objects.count())
|
||||
|
||||
|
||||
@@ -187,16 +187,19 @@ def product_loader(csv_reader, user, company=None):
|
||||
for row in csv_reader:
|
||||
# trim strings
|
||||
for key in row:
|
||||
if row[key]:
|
||||
if 'imagen' in key or 'categoria' in key:
|
||||
row[key] = row[key].strip()
|
||||
elif key in ['precio', 'gastos-envio']:
|
||||
row[key] = Decimal(row[key][:-1].strip().replace(',','.'))
|
||||
try:
|
||||
if row[key]:
|
||||
if 'imagen' in key or 'categoria' in key:
|
||||
row[key] = row[key].strip()
|
||||
elif key in ['precio', 'gastos-envio']:
|
||||
row[key] = Decimal(row[key][:-1].strip().replace(',','.'))
|
||||
else:
|
||||
row[key] = row[key].strip()
|
||||
else:
|
||||
row[key] = row[key].strip()
|
||||
if row[key] == '':
|
||||
row[key] = None
|
||||
|
||||
row[key] = None
|
||||
except Exception as e:
|
||||
logging.error(f"Could not access key {key}: {str(e)}")
|
||||
continue
|
||||
# check required data
|
||||
if '' in (row['nombre-producto'], row['descripcion'], row['precio'],):
|
||||
logging.error(f"Required data missing: {row}")
|
||||
|
||||
@@ -80,13 +80,12 @@ def load_coop_products(request):
|
||||
logging.info(f"Reading contents of {csv_file.name}")
|
||||
decoded_file = csv_file.read().decode('utf-8').splitlines()
|
||||
csv_reader = csv.DictReader(decoded_file, delimiter=',')
|
||||
|
||||
count = product_loader(csv_reader, request.user)
|
||||
if count is None:
|
||||
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:
|
||||
return Response({"errors": {"details": str(type(e))}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
return Response({"errors": {"details": str(e)}}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
|
||||
|
||||
@api_view(['GET',]) # include allowed methods
|
||||
|
||||
Reference in New Issue
Block a user