csv loading working

This commit is contained in:
Sam
2021-02-03 09:59:01 +00:00
parent a80d4565db
commit cc0bb2ebbf
4 changed files with 37 additions and 34 deletions

View File

@@ -92,35 +92,32 @@ def load_coop_managers(request):
Parse it to create users and related companies
"""
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}")
contents = csv_file.file
import ipdb; ipdb.set_trace()
csv_reader = csv.reader(contents, delimiter=',')
decoded_file = csv_file.read().decode('utf-8').splitlines()
csv_reader = csv.DictReader(decoded_file, delimiter=',')
coop_counter = 0
user_counter = 0
for row in csv_reader:
import ipdb; ipdb.set_trace()
try:
coop_data = {
'cif': row['cif'],
'company_name': row['nombre-coop'],
'short_name': row['nombre-corto'],
'shop': row['es-tienda'],
'url': row['url'],
'cif': row['cif'].strip(),
'company_name': row['nombre-coop'].strip(),
'short_name': row['nombre-corto'].strip(),
'shop': bool(row['es-tienda'].strip()),
'shop_link': row['url'].strip(),
}
coop = Company.object.create(**coop_data)
coop = Company.objects.create(**coop_data)
logging.info(f"Created Coop: {coop_data}")
coop_counter += 1
coop_user = User.objects.create_user(email=row['email'], company=coop, role='COOP_MANAGER')
logging.info(f"Created User: {coop_user}")
# TODO: send confirmation email
user_counter += 1
except Exception as e:
logging.error(f"Could not parse {row}")