diff --git a/products/tests.py b/products/tests.py index a617611..53ca3a0 100644 --- a/products/tests.py +++ b/products/tests.py @@ -421,6 +421,9 @@ class LoadCoopProductsTestCase(APITestCase): # create company company = CompanyFactory(creator=self.user) + # link with user + self.user.company = company + self.user.save() # read csv file files = {'csv_file': open(self.csv_path,'rt')} @@ -431,8 +434,9 @@ class LoadCoopProductsTestCase(APITestCase): # send in request response = self.client.post(self.endpoint, files) + import ipdb; ipdb.set_trace() - # check re sponse + # check response self.assertEqual(response.status_code, 200) # check for object creation self.assertEquals(5, self.model.objects.count()) diff --git a/products/utils.py b/products/utils.py index 0078784..50c7bc4 100644 --- a/products/utils.py +++ b/products/utils.py @@ -184,7 +184,6 @@ def product_loader(csv_reader, user, company=None): # create historysync instance history = HistorySync.objects.create(company=company, sync_date=datetime.datetime.now()) - for row in csv_reader: # trim strings for key in row: @@ -246,6 +245,7 @@ def product_loader(csv_reader, user, company=None): counter += 1 except Exception as e: logging.error(f"Could not parse {counter}: {str(e)}") + import ipdb; ipdb.set_trace() history.quantity = counter history.save() diff --git a/products/views.py b/products/views.py index 3199d80..c49b8c1 100644 --- a/products/views.py +++ b/products/views.py @@ -62,30 +62,31 @@ def my_products(request): @api_view(['POST',]) @permission_classes([IsAuthenticated,]) def load_coop_products(request): - """Read CSV file being received - Parse it to create products for related Company + """Read CSV file being received + Parse it to create products for related Company - Authenticated user must have a related company - """ - try: - # check company linked to user - if request.user.company is None: - return Response({"errors":{"details": "Your user has no company to add products to"}}) + Authenticated user must have a related company + """ + # check company linked to user + if request.user.company is None: + return Response({"errors":{"details": "Your user has no company to add products to"}}, status=status.HTTP_406_NOT_ACCEPTABLE) - csv_file = request.FILES['csv_file'] - if csv_file.name.endswith('.csv') is not True: - logging.error(f"File {csv_file.name} is not a CSV file") - return Response({"errors":{"details": "File is not CSV type"}}) + try: + csv_file = request.FILES['csv_file'] + if csv_file.name.endswith('.csv') is not True: + logging.error(f"File {csv_file.name} is not a CSV file") + return Response({"errors":{"details": "File is not CSV type"}}) - 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) + 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) @api_view(['GET',]) # include allowed methods