improvements to csv loading code
This commit is contained in:
@@ -8,6 +8,7 @@ from django.template.loader import render_to_string
|
||||
from django.core.mail import EmailMessage
|
||||
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
||||
from django.conf import settings
|
||||
from django.core.validators import validate_email, EmailValidator, URLValidator, ValidationError
|
||||
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
|
||||
@@ -101,7 +102,7 @@ def reformat_google_taxonomy(file_name):
|
||||
destination_file.write(line)
|
||||
|
||||
|
||||
def coop_loader(csv_reader, request):
|
||||
def coop_loader(csv_reader, request=None):
|
||||
"""
|
||||
Parse csv data and extract:
|
||||
|
||||
@@ -113,16 +114,45 @@ def coop_loader(csv_reader, request):
|
||||
coop_counter = 0
|
||||
user_counter = 0
|
||||
for row in csv_reader:
|
||||
# trim strings
|
||||
for key in row:
|
||||
if row[key]: row[key] = row[key].strip()
|
||||
# import ipdb; ipdb.set_trace()
|
||||
if '' in (row['cif'], row['nombre-coop'], row['email']):
|
||||
logging.error(f"Required data missing: {row}")
|
||||
continue
|
||||
# validate email
|
||||
try:
|
||||
validate_email(row['email'])
|
||||
except ValidationError:
|
||||
logging.warning(f"Invalid email value '{row['email']}', skipped")
|
||||
continue
|
||||
# validate URLs
|
||||
if row['url'].startswith('http') is not True:
|
||||
row['url'] = 'http://' + row['url']
|
||||
if row['logo-url'].startswith('http') is not True:
|
||||
row['logo-url'] = 'http://' + row['logo-url']
|
||||
validator = URLValidator()
|
||||
try:
|
||||
validator(row['url'])
|
||||
except ValidationError:
|
||||
logging.warning(f"Invalid url value '{row['url']}', skipped")
|
||||
row['url'] = None
|
||||
try:
|
||||
validator(row['logo-url'])
|
||||
except ValidationError:
|
||||
logging.warning(f"Invalid url value '{row['logo-url']}', skipped")
|
||||
row['logo-url'] = None
|
||||
# validate boolean
|
||||
|
||||
# create instances
|
||||
try:
|
||||
coop_data = {
|
||||
'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(),
|
||||
'cif': row['cif'],
|
||||
'company_name': row['nombre-coop'],
|
||||
'short_name': row['nombre-corto'],
|
||||
'shop': bool(row['es-tienda']),
|
||||
'shop_link': row['url'],
|
||||
}
|
||||
coop = Company.objects.create(**coop_data)
|
||||
logging.info(f"Created Coop: {coop_data}")
|
||||
@@ -130,10 +160,12 @@ def coop_loader(csv_reader, request):
|
||||
|
||||
coop_user = User.objects.create_user(email=row['email'], company=coop, role='COOP_MANAGER', is_active=False)
|
||||
# send confirmation email
|
||||
send_verification_email(request, coop_user)
|
||||
if request is not None:
|
||||
send_verification_email(request, coop_user)
|
||||
logging.info(f"Created User: {coop_user}")
|
||||
user_counter += 1
|
||||
except Exception as e:
|
||||
import ipdb; ipdb.set_trace()
|
||||
logging.error(f"Could not parse {row}")
|
||||
return coop_counter, user_counter
|
||||
|
||||
|
||||
Reference in New Issue
Block a user