work on product csv loading
This commit is contained in:
@@ -421,6 +421,9 @@ class LoadCoopProductsTestCase(APITestCase):
|
|||||||
|
|
||||||
# create company
|
# create company
|
||||||
company = CompanyFactory(creator=self.user)
|
company = CompanyFactory(creator=self.user)
|
||||||
|
# link with user
|
||||||
|
self.user.company = company
|
||||||
|
self.user.save()
|
||||||
|
|
||||||
# read csv file
|
# read csv file
|
||||||
files = {'csv_file': open(self.csv_path,'rt')}
|
files = {'csv_file': open(self.csv_path,'rt')}
|
||||||
@@ -431,6 +434,7 @@ class LoadCoopProductsTestCase(APITestCase):
|
|||||||
|
|
||||||
# send in request
|
# send in request
|
||||||
response = self.client.post(self.endpoint, files)
|
response = self.client.post(self.endpoint, files)
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
|
|
||||||
# check response
|
# check response
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|||||||
@@ -184,7 +184,6 @@ def product_loader(csv_reader, user, company=None):
|
|||||||
|
|
||||||
# create historysync instance
|
# create historysync instance
|
||||||
history = HistorySync.objects.create(company=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
|
||||||
for key in row:
|
for key in row:
|
||||||
@@ -246,6 +245,7 @@ def product_loader(csv_reader, user, company=None):
|
|||||||
counter += 1
|
counter += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Could not parse {counter}: {str(e)}")
|
logging.error(f"Could not parse {counter}: {str(e)}")
|
||||||
|
import ipdb; ipdb.set_trace()
|
||||||
|
|
||||||
history.quantity = counter
|
history.quantity = counter
|
||||||
history.save()
|
history.save()
|
||||||
|
|||||||
@@ -67,11 +67,11 @@ def load_coop_products(request):
|
|||||||
|
|
||||||
Authenticated user must have a related company
|
Authenticated user must have a related company
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
# check company linked to user
|
# check company linked to user
|
||||||
if request.user.company is None:
|
if request.user.company is None:
|
||||||
return Response({"errors":{"details": "Your user has no company to add products to"}})
|
return Response({"errors":{"details": "Your user has no company to add products to"}}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
|
||||||
|
try:
|
||||||
csv_file = request.FILES['csv_file']
|
csv_file = request.FILES['csv_file']
|
||||||
if csv_file.name.endswith('.csv') is not True:
|
if csv_file.name.endswith('.csv') is not True:
|
||||||
logging.error(f"File {csv_file.name} is not a CSV file")
|
logging.error(f"File {csv_file.name} is not a CSV file")
|
||||||
@@ -80,6 +80,7 @@ def load_coop_products(request):
|
|||||||
logging.info(f"Reading contents of {csv_file.name}")
|
logging.info(f"Reading contents of {csv_file.name}")
|
||||||
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:
|
if count is None:
|
||||||
return Response({"errors": {"details": "Authenticated user is not related to any company"}}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
return Response({"errors": {"details": "Authenticated user is not related to any company"}}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
|
|||||||
Reference in New Issue
Block a user