def add(request): """Submit image using form""" if request.method == 'POST': form = ImageForm(BackgroundImage, request.POST, request.FILES) if form.is_valid(): form.save() return redirect('/') else: form = ImageForm(BackgroundImage.specs()) return render(request, 'django_images.html', dict(form=form))
def test_resize_big_image_in_background_format(self): """Test resizing of big enough image to background format""" filepath = os.path.join(settings.BASE_DIR, 'big.jpeg') with open(filepath) as f: # prepare form data image = InMemoryUploadedFile( f, 'image', 'big.jpeg', 'image/jpeg', 42, # not significant for the test 'utf-8') files = MultiValueDict() files['image'] = image post = MultiValueDict() post['name'] = 'test image' # create form form = ImageForm(TestImage, post, files) # validate resize operation self.assertTrue(form.is_valid()) # execute resize operation image = form.save() for size in ('og', 'lg', 'md', 'sm', 'xs'): filepath = getattr(image, size)['filepath'] filepath = os.path.join(settings.MEDIA_ROOT, filepath) self.assertTrue(os.path.exists(filepath))
def create_image(): filepath = os.path.join(settings.BASE_DIR, 'big.jpeg') with open(filepath) as f: # prepare form data image = InMemoryUploadedFile( f, 'image', 'big.jpeg', 'image/jpeg', 42, # not significant for the test 'utf-8') files = MultiValueDict() files['image'] = image post = MultiValueDict() post['ptype'] = 1 post['name'] = 'test with big.jpeg' # create form form = ImageForm(TestImage, post, files) # validate resize operation form.is_valid() # execute resize operation image = form.save() return image
def test_validation(self): """Validate an image against a complex format""" filepath = os.path.join(settings.BASE_DIR, 'middle.jpeg') with open(filepath) as f: # prepare form data image = InMemoryUploadedFile( f, 'image', 'middle.jpeg', 'image/jpeg', 42, # not significant for the test 'utf-8') files = MultiValueDict() files['image'] = image post = MultiValueDict() post['name'] = 'test image' # create form form = ImageForm(TestImage, post, files) # validate resize operation v = form.is_valid() self.assertTrue(v)
def test_fail_to_resize_small_image_in_background_format(self): """Test resizing of image fails validation""" filepath = os.path.join(settings.BASE_DIR, 'small.jpeg') with open(filepath) as f: # prepare form data image = InMemoryUploadedFile( f, 'image', 'small.jpeg', 'image/jpeg', 42, # not significant for the test 'utf-8') files = MultiValueDict() files['image'] = image post = MultiValueDict() post['ptype'] = 1 post['name'] = 'test with small.jpeg' # create form form = ImageForm(TestImage, post, files) # validate resize operation self.assertFalse(form.is_valid())