more work on load_coop_managers endpoint, unfinished
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import csv
|
||||
import logging
|
||||
import json
|
||||
import io
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.http import HttpResponse
|
||||
@@ -12,6 +14,8 @@ from rest_framework.permissions import IsAdminUser
|
||||
from rest_framework.generics import UpdateAPIView
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
|
||||
from companies.models import Company
|
||||
|
||||
from . import models
|
||||
from . import serializers
|
||||
|
||||
@@ -87,23 +91,40 @@ def load_coop_managers(request):
|
||||
"""Read CSV file being received
|
||||
Parse it to create users and related companies
|
||||
"""
|
||||
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:
|
||||
|
||||
logging.info(f"Reading contents of {csv_file.name}")
|
||||
csv_reader = csv.DictReader(csv_file, delimiter=',')
|
||||
for row in csv_reader:
|
||||
email = row['email']
|
||||
cif = row['cif']
|
||||
company_name = row['nombre-coop']
|
||||
short_name = row['nombre-corto']
|
||||
url = row['url']
|
||||
shop = row['es-tienda']
|
||||
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"}})
|
||||
|
||||
coop = None
|
||||
coop_user = User.objects.create_user(email=email, company=coop, role='COOP_MANAGER')
|
||||
logging.info(f"Reading contents of {csv_file.name}")
|
||||
contents = csv_file.file
|
||||
import ipdb; ipdb.set_trace()
|
||||
csv_reader = csv.reader(contents, 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'],
|
||||
}
|
||||
|
||||
coop = Company.object.create(**coop_data)
|
||||
logging.info(f"Created Coop: {coop_data}")
|
||||
coop_counter += 1
|
||||
|
||||
return Response
|
||||
coop_user = User.objects.create_user(email=row['email'], company=coop, role='COOP_MANAGER')
|
||||
logging.info(f"Created User: {coop_user}")
|
||||
user_counter += 1
|
||||
except Exception as e:
|
||||
logging.error(f"Could not parse {row}")
|
||||
|
||||
return Response()
|
||||
except Exception as e:
|
||||
return Response({"errors": {"details": str(type(e))}})
|
||||
|
||||
Reference in New Issue
Block a user