Merge branch 'master' of bitbucket.org:enreda/back-latienda
This commit is contained in:
@@ -452,8 +452,7 @@ class LoadCoopManagerTestCase(APITestCase):
|
||||
|
||||
# send in request
|
||||
response = self.client.post(self.endpoint, files)
|
||||
|
||||
# check re sponse
|
||||
# check response
|
||||
self.assertEqual(response.status_code, 200)
|
||||
# check for object creation
|
||||
self.assertEquals(company_count + 5, self.company_model.objects.count())
|
||||
|
||||
101
core/utils.py
101
core/utils.py
@@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from io import BytesIO
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.sites.shortcuts import get_current_site
|
||||
@@ -8,11 +9,17 @@ 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
|
||||
|
||||
import requests
|
||||
from PIL import Image
|
||||
from django.core.files import File
|
||||
from tagulous.models import TagModel
|
||||
|
||||
from companies.models import Company
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
@@ -98,3 +105,97 @@ def reformat_google_taxonomy(file_name):
|
||||
line = line.replace(' > ', '/')
|
||||
destination_file.write(line)
|
||||
|
||||
|
||||
def coop_loader(csv_reader, request=None):
|
||||
"""
|
||||
Parse csv data and extract:
|
||||
|
||||
- coop data
|
||||
- manager user data
|
||||
|
||||
Return counts
|
||||
"""
|
||||
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().lower()
|
||||
# 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']}'")
|
||||
row['url'] = None
|
||||
try:
|
||||
validator(row['logo-url'])
|
||||
except ValidationError:
|
||||
logging.warning(f"Invalid logo URL value '{row['logo-url']}'")
|
||||
row['logo-url'] = None
|
||||
# validate boolean
|
||||
try:
|
||||
shop = bool(row['es-tienda'])
|
||||
except:
|
||||
logging.warning(f"Invalid valur for es-tiends: {row['es-tienda']}")
|
||||
shop = None
|
||||
|
||||
# create instances
|
||||
try:
|
||||
coop_data = {
|
||||
'cif': row['cif'],
|
||||
'company_name': row['nombre-coop'],
|
||||
'short_name': row['nombre-corto'],
|
||||
'shop': shop,
|
||||
'shop_link': row['url'],
|
||||
'phone': row['telefono'],
|
||||
'address': row['direccion'],
|
||||
}
|
||||
coop = Company.objects.create(**coop_data)
|
||||
# image logo data
|
||||
if row['logo-url'] is not None:
|
||||
try:
|
||||
# get image
|
||||
headers={"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
|
||||
response = requests.get(row['logo-url'], stream=True, headers=headers)
|
||||
assert(response.status_code==200)
|
||||
response.raw.decode_content = True
|
||||
image = Image.open(response.raw)
|
||||
# save using File object
|
||||
img_io = BytesIO()
|
||||
image.save(img_io, format=image.format)
|
||||
coop.logo.save(f"{coop.company_name}.{image.format.lower()}", File(img_io), save=False)
|
||||
coop.save()
|
||||
except AssertionError as e:
|
||||
logging.error(f"Source image [{row['logo-url']}] not reachable: {response.status_code}")
|
||||
except Exception as e:
|
||||
logging.error(f"Could not add image to COOP {coop.company_name} from [{row['logo-url']}]: {str(e)}")
|
||||
#
|
||||
logging.info(f"Created Coop: {coop_data}")
|
||||
coop_counter += 1
|
||||
|
||||
coop_user = User.objects.create_user(email=row['email'], company=coop, role='COOP_MANAGER', is_active=False)
|
||||
# send confirmation email
|
||||
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
|
||||
|
||||
|
||||
@@ -183,35 +183,11 @@ def load_coop_managers(request):
|
||||
logging.info(f"Reading contents of {csv_file.name}")
|
||||
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:
|
||||
if '' in (row['cif'], row['nombre-coop'], row['email']):
|
||||
logging.error(f"Required data missing: {row}")
|
||||
continue
|
||||
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(),
|
||||
}
|
||||
coop = Company.objects.create(**coop_data)
|
||||
logging.info(f"Created Coop: {coop_data}")
|
||||
coop_counter += 1
|
||||
coop_count, user_count = utils.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
|
||||
utils.send_verification_email(request, coop_user)
|
||||
logging.info(f"Created User: {coop_user}")
|
||||
user_counter += 1
|
||||
except Exception as e:
|
||||
logging.error(f"Could not parse {row}")
|
||||
|
||||
return Response()
|
||||
return Response({'details': f"Created {coop_count} Companies, {user_count} Managing Users"})
|
||||
except Exception as e:
|
||||
return Response({"errors": {"details": str(type(e))}})
|
||||
return Response({"errors": {"details": f'{type(e)}: {e}'}})
|
||||
|
||||
|
||||
@api_view(['GET',])
|
||||
|
||||
Reference in New Issue
Block a user