csv loading working
This commit is contained in:
@@ -10,8 +10,6 @@ from django.test import TestCase
|
|||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
import requests
|
|
||||||
|
|
||||||
from core.utils import get_tokens_for_user
|
from core.utils import get_tokens_for_user
|
||||||
|
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
@@ -382,12 +380,13 @@ class UpdateUserViewTest(APITestCase):
|
|||||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
|
||||||
class LoadCoopManagerTestCase(TestCase):
|
class LoadCoopManagerTestCase(APITestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Tests setup
|
"""Tests setup
|
||||||
"""
|
"""
|
||||||
self.endpoint = 'http://127.0.0.1:8000/api/v1/load_coops/'
|
self.url = 'http://127.0.0.1:8000/api/v1/load_coops/'
|
||||||
|
self.endpoint = '/api/v1/load_coops/'
|
||||||
self.user_factory = factories.CustomUserFactory
|
self.user_factory = factories.CustomUserFactory
|
||||||
self.user_model = models.CustomUser
|
self.user_model = models.CustomUser
|
||||||
self.company_model = Company
|
self.company_model = Company
|
||||||
@@ -400,24 +399,26 @@ class LoadCoopManagerTestCase(TestCase):
|
|||||||
self.user = self.user_factory(email=self.reg_email, is_active=True)
|
self.user = self.user_factory(email=self.reg_email, is_active=True)
|
||||||
self.user.set_password(self.password)
|
self.user.set_password(self.password)
|
||||||
self.user.save()
|
self.user.save()
|
||||||
|
# test CSV file path
|
||||||
|
self.csv_path = 'datasets/test_coop.csv'
|
||||||
|
|
||||||
def test_admin_can_load_csv(self):
|
def test_admin_can_load_csv(self):
|
||||||
company_count = self.company_model.objects.count()
|
company_count = self.company_model.objects.count()
|
||||||
user_count = self.user_model.objects.count()
|
user_count = self.user_model.objects.count()
|
||||||
|
|
||||||
# read csv file
|
# read csv file
|
||||||
csv_file = '../coop.csv'
|
csv_file = 'datasets/test_coop.csv'
|
||||||
files = {'csv_file': open(csv_file,'rt')}
|
files = {'csv_file': open(self.csv_path,'rt')}
|
||||||
|
|
||||||
# Authenticate
|
# Authenticate
|
||||||
token = get_tokens_for_user(self.admin_user)
|
token = get_tokens_for_user(self.admin_user)
|
||||||
headers = {'Authorization': f"Bearer {token['access']}"}
|
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||||
|
|
||||||
# send in request
|
# send in request
|
||||||
response = requests.post(self.endpoint, files=files, headers=headers)
|
response = self.client.post(self.url, files)
|
||||||
|
|
||||||
# check response
|
|
||||||
|
|
||||||
|
# check re sponse
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
# check for object creation
|
# check for object creation
|
||||||
self.assertNotEqual(company_count, self.company_model.objects.count())
|
self.assertNotEqual(company_count, self.company_model.objects.count())
|
||||||
self.assertNotEqual(user_count, self.user_model.objects.count())
|
self.assertNotEqual(user_count, self.user_model.objects.count())
|
||||||
@@ -427,18 +428,17 @@ class LoadCoopManagerTestCase(TestCase):
|
|||||||
user_count = self.user_model.objects.count()
|
user_count = self.user_model.objects.count()
|
||||||
|
|
||||||
# read csv file
|
# read csv file
|
||||||
csv_file = '../coop.csv'
|
files = {'csv_file': open(self.csv_path,'r')}
|
||||||
files = {'csv_file': open(csv_file,'r')}
|
|
||||||
|
|
||||||
# Authenticate
|
# Authenticate
|
||||||
token = get_tokens_for_user(self.user)
|
token = get_tokens_for_user(self.user)
|
||||||
headers = {'Authorization': f"Bearer {token['access']}"}
|
self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {token['access']}")
|
||||||
|
|
||||||
# send in request
|
# send in request
|
||||||
response = requests.post(self.endpoint, files=files, headers=headers)
|
response = self.client.post(self.url, files)
|
||||||
|
|
||||||
# check response
|
# check response
|
||||||
|
self.assertEqual(response.status_code, 403)
|
||||||
# check for object creation
|
# check for object creation
|
||||||
self.assertEqual(company_count, self.company_model.objects.count())
|
self.assertEqual(company_count, self.company_model.objects.count())
|
||||||
self.assertEqual(user_count, self.user_model.objects.count())
|
self.assertEqual(user_count, self.user_model.objects.count())
|
||||||
@@ -448,14 +448,13 @@ class LoadCoopManagerTestCase(TestCase):
|
|||||||
user_count = self.user_model.objects.count()
|
user_count = self.user_model.objects.count()
|
||||||
|
|
||||||
# read csv file
|
# read csv file
|
||||||
csv_file = '../coop.csv'
|
files = {'csv_file': open(self.csv_path,'r')}
|
||||||
files = {'csv_file': open(csv_file,'r')}
|
|
||||||
|
|
||||||
# Query endpoint
|
# send in request
|
||||||
response = self.client.post(self.endpoint, files=files)
|
response = self.client.post(self.url, files)
|
||||||
|
|
||||||
# check response
|
# check response
|
||||||
|
self.assertEqual(response.status_code, 401)
|
||||||
# check for object creation
|
# check for object creation
|
||||||
self.assertEqual(company_count, self.company_model.objects.count())
|
self.assertEqual(company_count, self.company_model.objects.count())
|
||||||
self.assertEqual(user_count, self.user_model.objects.count())
|
self.assertEqual(user_count, self.user_model.objects.count())
|
||||||
|
|||||||
@@ -92,35 +92,32 @@ def load_coop_managers(request):
|
|||||||
Parse it to create users and related companies
|
Parse it to create users and related companies
|
||||||
"""
|
"""
|
||||||
try:
|
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")
|
||||||
return Response({"errors":{"details": "File is not CSV type"}})
|
return Response({"errors":{"details": "File is not CSV type"}})
|
||||||
|
|
||||||
logging.info(f"Reading contents of {csv_file.name}")
|
logging.info(f"Reading contents of {csv_file.name}")
|
||||||
contents = csv_file.file
|
decoded_file = csv_file.read().decode('utf-8').splitlines()
|
||||||
import ipdb; ipdb.set_trace()
|
csv_reader = csv.DictReader(decoded_file, delimiter=',')
|
||||||
csv_reader = csv.reader(contents, delimiter=',')
|
|
||||||
coop_counter = 0
|
coop_counter = 0
|
||||||
user_counter = 0
|
user_counter = 0
|
||||||
for row in csv_reader:
|
for row in csv_reader:
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
try:
|
try:
|
||||||
coop_data = {
|
coop_data = {
|
||||||
'cif': row['cif'],
|
'cif': row['cif'].strip(),
|
||||||
'company_name': row['nombre-coop'],
|
'company_name': row['nombre-coop'].strip(),
|
||||||
'short_name': row['nombre-corto'],
|
'short_name': row['nombre-corto'].strip(),
|
||||||
'shop': row['es-tienda'],
|
'shop': bool(row['es-tienda'].strip()),
|
||||||
'url': row['url'],
|
'shop_link': row['url'].strip(),
|
||||||
}
|
}
|
||||||
|
coop = Company.objects.create(**coop_data)
|
||||||
coop = Company.object.create(**coop_data)
|
|
||||||
logging.info(f"Created Coop: {coop_data}")
|
logging.info(f"Created Coop: {coop_data}")
|
||||||
coop_counter += 1
|
coop_counter += 1
|
||||||
|
|
||||||
coop_user = User.objects.create_user(email=row['email'], company=coop, role='COOP_MANAGER')
|
coop_user = User.objects.create_user(email=row['email'], company=coop, role='COOP_MANAGER')
|
||||||
logging.info(f"Created User: {coop_user}")
|
logging.info(f"Created User: {coop_user}")
|
||||||
|
# TODO: send confirmation email
|
||||||
user_counter += 1
|
user_counter += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Could not parse {row}")
|
logging.error(f"Could not parse {row}")
|
||||||
|
|||||||
9
datasets/test_coop.csv
Normal file
9
datasets/test_coop.csv
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
email,cif,nombre-coop,nombre-corto,url,es-tienda
|
||||||
|
qwer@mail.com, 1223432214L, FEWQ4FEWQ COOP, fc, tienda1.com, True
|
||||||
|
dsfds@mail.com, 65876514L, FEW2QFEWQ COOP, fc, tienda2.com, True
|
||||||
|
ghjhg@mail.com, 122343214L, FEWQF2EWQ COOP, fc, tienda3.com, True
|
||||||
|
xcv@mail.com, 12343214L, FEWQ2FEWQ COOP, fc, tienda4.com, True
|
||||||
|
cvc@mail.com, 1879783214L, 2FEWQFEWQ COOP, fc, tienda5.com, True
|
||||||
|
bvbc@mail.com, 5653214L, FEW2QFEWQ COOP, fc, tienda6.com, True
|
||||||
|
kjk@mail.com, 54326543H, FE2WQF2EWQ COOP, fc, tienda7.com, True
|
||||||
|
yuyu@mail.com, 12343214L, F2EWQFEWQ COOP, fc, tienda8.com, True
|
||||||
|
@@ -10,5 +10,3 @@ django-cors-headers==3.5.0
|
|||||||
django-taggit-serializer==0.1.7
|
django-taggit-serializer==0.1.7
|
||||||
django-tagulous==1.1.0
|
django-tagulous==1.1.0
|
||||||
Pillow==8.1.0
|
Pillow==8.1.0
|
||||||
# for testing
|
|
||||||
requests==2.25.1
|
|
||||||
Reference in New Issue
Block a user