def attach_image_to_product(product, url): if not url: return url = url.replace(' ', '%20') img_temp = NamedTemporaryFile() img_temp.write(urllib2.urlopen(url).read()) img_temp.flush() pi = ProductImage() pi.name = product.title[:25] pi.product = product pi.image.save(product.title[:25], File(img_temp)) pi.save(using='default') product.has_images = True product.save(using='default')
def attach_image_to_product(product, url): if not url: return url = url.replace(' ', '%20') img_temp = NamedTemporaryFile() img_temp.write(urllib2.urlopen(url).read()) img_temp.flush() pi = ProductImage() pi.name = product.title[:25] pi.product = product pi.image.save(product.title[:25], File(img_temp)) pi.save(using='default') product.has_images = True product.save(using='default')
# download the image for url in data['cleaned_data']['image_url']: try: feedutils.attach_image_to_product(product,url) except Exception, e: log.exception( 'Error adding image to product %s from %s: %s' % ( product.id, data['cleaned_data']['image_url'], repr(e))) else: # Dont download images, we will serve them using their original urls for url in data['cleaned_data']['image_url']: pi = ProductImage(product=product, type='external') if 'scene7' in url: pi.type = 'scene7' pi.name = product.title[:25] pi.url = url pi.save(using='default') if sync and not is_update: sync.new_masters += 1 sync.save(using='default') #sync_product = SyncEventProductMapping(sync_event=sync, # product=product, action='added') #sync_product.sku = data['cleaned_data']['sku'] #sync_product.save(using='default') if sync and is_update: sync.edits += 1 sync.save(using='default') #sync_product = SyncEventProductMapping(sync_event=sync,