improvements to coop loader
This commit is contained in:
@@ -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
|
||||
@@ -12,6 +13,9 @@ from django.core.validators import validate_email, EmailValidator, URLValidator,
|
||||
|
||||
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
|
||||
@@ -116,7 +120,7 @@ def coop_loader(csv_reader, request=None):
|
||||
for row in csv_reader:
|
||||
# trim strings
|
||||
for key in row:
|
||||
if row[key]: row[key] = row[key].strip()
|
||||
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}")
|
||||
@@ -136,14 +140,19 @@ def coop_loader(csv_reader, request=None):
|
||||
try:
|
||||
validator(row['url'])
|
||||
except ValidationError:
|
||||
logging.warning(f"Invalid url value '{row['url']}', skipped")
|
||||
logging.warning(f"Invalid url value '{row['url']}'")
|
||||
row['url'] = None
|
||||
try:
|
||||
validator(row['logo-url'])
|
||||
except ValidationError:
|
||||
logging.warning(f"Invalid url value '{row['logo-url']}', skipped")
|
||||
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:
|
||||
@@ -151,10 +160,31 @@ def coop_loader(csv_reader, request=None):
|
||||
'cif': row['cif'],
|
||||
'company_name': row['nombre-coop'],
|
||||
'short_name': row['nombre-corto'],
|
||||
'shop': bool(row['es-tienda']),
|
||||
'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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user