def test_product_image_deletion_does_not_delete_referenced_variation(self): try: from io import BytesIO except ImportError: from cStringIO import StringIO as BytesIO stream = BytesIO() from PIL import Image im = Image.new("RGB", (50, 50), "white") im.save(stream, "png") del im stream.seek(0) from django.core.files import File product = Product(title="Doodah", unit_price="2.99") product.save() image = ProductImage(product_id=product.id, file=File(stream)) image.save() ProductVariation( unit_price="1.99", product_id=product.id, default=True, image=image ).save() self.assertEqual(product.variations.count(), 1) image.delete() self.assertEqual(product.variations.count(), 1)
def main(): breeds_list, links = parser_breeds() breeds_list = breeds_list[61:160] links = links[61:160] description = breeds_links_read(links) for i, breed in enumerate(breeds_list): print('Start downloading images for ' + breed) links = request_to_google_cse(API_KEY, breed, CUSTOM_SEARCH_ENGINE_ID, NUMBER_IMG_REQUIR) folder_single_breed_img = os.path.join(MEDIA_ROOT, 'uploads/product', breed) # create folder for each breed if it isn't exist if not os.path.exists(folder_single_breed_img): os.makedirs(folder_single_breed_img) new_dog_product = Product(available=True, title=breed, content=description[i]) new_dog_product.save() variation = ProductVariation(product=new_dog_product, default=True, unit_price=choice(PRICE_CHOICES), sale_from=timezone.now()) variation.save() for link in links['items']: try: img_resized = download_images_by_link( breed, link['link'], IMG_WIDTH_REQUIR, IMG_HEIGHT_REQUIR, os.path.join(MEDIA_ROOT, WATERMARK), WATERMARK_OPACITY, MEDIA_ROOT) # create a new instance of ProductImage class and link it with Product class image = ProductImage(product=new_dog_product) # assign path to image to ProductImage instance image.file = img_resized image.save() print(img_resized + ' was downloaded') except HTTPError: print('HTTP Error 403: Forbidden. Link is not valid') except OSError: print('OSError.') # Set the first instance of ProductImage as default image for default variation new_dog_product.variations.set_default_images([]) # Copy duplicate fields (``Priced`` fields) from the default variation to the product. new_dog_product.copy_default_variation()