def import_manufacturers(shop, yaml_file, image_dir): print("Loading manufacturers") # noqa with open(yaml_file, "rb") as fp: manufacturers = yaml.safe_load(fp) print("Manufacturers loading, starting import...") # noqa for manufacturer_identifier, data_src in sorted(manufacturers.items()): image_name = data_src.pop("logo", None) identifier = get_manufacturer_identifier_for_shop( shop, manufacturer_identifier) manufacturer = create_from_datum(Manufacturer, identifier, data_src) manufacturer.save() manufacturer.shops.add(shop) if image_name: image_file = os.path.join(image_dir, image_name) if not os.path.isfile(image_file): print("Image file does not exist: %s" % image_file) # noqa return with open(image_file, "rb") as fp: data = fp.read() manufacturer.logo = filer_image_from_data( None, "Manufacturers", image_name.split("/")[-1], data, sha1=True) manufacturer.save()
def _create_random_media_file(shop, file_path): path, name = os.path.split(file_path) pil_image = generate_image(2, 2) sio = BytesIO() pil_image.save(sio, "JPEG", quality=45) filer_file = filer_image_from_data(request=None, path=path, file_name=name, file_data=sio.getvalue()) media_file = MediaFile.objects.create(file=filer_file) media_file.shops.add(shop) return media_file
def _generate_product_image(product): image = generate_image(32, 32) sio = BytesIO() image.save(sio, format="JPEG", quality=75) filer_file = filer_image_from_data( request=None, path="ProductImages/Mock", file_name="%s.jpg" % product.sku, file_data=sio.getvalue(), sha1=True ) media = ProductMedia.objects.create(product=product, kind=ProductMediaKind.IMAGE, file=filer_file) media.shops.set(Shop.objects.all()) media.save() return media
def _filer_image_from_file_path(image_file_path, path): _, full_file_name = os.path.split(image_file_path) file_name, _ = os.path.splitext(full_file_name) image = Image.open(image_file_path) sio = BytesIO() image.save(sio, format="JPEG") return filer_image_from_data(request=None, path=path, file_name="{}.jpeg".format(file_name), file_data=sio.getvalue(), sha1=True)
def _attach_image_from_name(self, product, image_name, shop_product): image_file = os.path.join(self.image_dir, image_name) if not os.path.isfile(image_file): print("Image file does not exist: %s" % image_file) # noqa return with open(image_file, "rb") as fp: data = fp.read() filer_file = filer_image_from_data(None, "Products", image_name, data, sha1=True) assert filer_file image, _ = ProductMedia.objects.get_or_create(product=product, file=filer_file, kind=ProductMediaKind.IMAGE) image.shops.add(self.shop) product.primary_image = image shop_product.shop_primary_image = image
def _filer_image_from_file_path(image_file_path, path): _, full_file_name = os.path.split(image_file_path) file_name, _ = os.path.splitext(full_file_name) image = Image.open(image_file_path) sio = BytesIO() image.save(sio, format="JPEG") return filer_image_from_data( request=None, path=path, file_name="{}.jpeg".format(file_name), file_data=sio.getvalue(), sha1=True )
def _create_random_media_file(shop, file_path): path, name = os.path.split(file_path) pil_image = generate_image(2, 2) sio = BytesIO() pil_image.save(sio, "JPEG", quality=45) filer_file = filer_image_from_data( request=None, path=path, file_name=name, file_data=sio.getvalue() ) media_file = MediaFile.objects.create(file=filer_file) media_file.shops.add(shop) return media_file
def _generate_product_image(product): image = generate_image(512, 512) sio = BytesIO() image.save(sio, format="JPEG", quality=75) filer_file = filer_image_from_data( request=None, path="ProductImages/Mock", file_name="%s.jpg" % product.sku, file_data=sio.getvalue(), sha1=True ) media = ProductMedia.objects.create( product=product, kind=ProductMediaKind.IMAGE, file=filer_file ) media.shops = Shop.objects.all() media.save() return media