fix for image im addtestdata

This commit is contained in:
Sam
2021-02-15 13:27:42 +00:00
parent 7c6db6976c
commit 95dc064ed2

View File

@@ -1,12 +1,16 @@
import logging import logging
import json import json
import shutil
import requests import requests
from django.core.files import File
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.contrib.gis.geos import GEOSGeometry, MultiPolygon from django.contrib.gis.geos import GEOSGeometry, MultiPolygon
from django.conf import settings
from faker import Faker from faker import Faker
from PIL import Image
from companies.factories import CompanyFactory from companies.factories import CompanyFactory
from companies.models import Company from companies.models import Company
@@ -23,7 +27,7 @@ logging.basicConfig(
class Command(BaseCommand): class Command(BaseCommand):
logo_url = "https://picsum.photos/200/300" logo_url = "https://picsum.photos/300/200"
help = 'Creates fake companies and related products in database' help = 'Creates fake companies and related products in database'
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
@@ -55,23 +59,46 @@ class Command(BaseCommand):
for company in new_companies: for company in new_companies:
print("Creating fake products for {company.company_name}") print("Creating fake products for {company.company_name}")
logging.info(f"Creating Products for {company.company_name}") logging.info(f"Creating Products for {company.company_name}")
for i in range(100): # for i in range(100):
for i in range(10):
name = fake.last_name_nonbinary() name = fake.last_name_nonbinary()
description = fake.paragraph(nb_sentences=5) description = fake.paragraph(nb_sentences=5)
# TODO: apply tags from tag list # TODO: apply tags from tag list
image_path = settings.MEDIA_ROOT + company.company_name + '.jpg'
image= None
"""
# TODO: write image to S3 storage # TODO: write image to S3 storage
response = requests.get(self.logo_url) response = requests.get(self.logo_url, stream=True)
# import ipdb; ipdb.set_trace()
# write image to disk
'''
if response.status_code == 200: if response.status_code == 200:
response.raw.decode_content = True with open(image_path, 'wb') as f:
image = response.raw.read() for chunk in response:
import ipdb; ipdb.set_trace()
# f.write(chunk))
else: else:
logging.warning(f"Got {response.status_code} querying {self.logo_url}") logging.warning(f"Got {response.status_code} querying {self.logo_url}")
""" '''
product = ProductFactory(name=name, description=description, image=image) if response.status_code == 200:
with open(image_path, 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw, f)
image = response.raw.read()
else:
logging.warning(f"Got {response.status_code} querying {self.logo_url}")
continue
image = Image.open(image_path)
# import ipdb; ipdb.set_trace()
product = ProductFactory(name=name, description=description)
product.image.save(
image_path,
# image,
File(open(image_path, 'rb')),
save=True) # image=Image.open(image_path))
product.save()
logging.debug(f"New Product {product.name} created") logging.debug(f"New Product {product.name} created")
print("*", end = '.') print("*", end = '.')
print('') print('')