Ejemplo n.º 1
0
    def test_keeper(self):
        """
        If the format is in KEEP_FORMATS, it should be preserved.

        """
        image = Image.open(self._data_path('100x100.png'))
        self.assertIn(image.format, KEEP_FORMATS)

        storage_path = save_image(image, 'daguerre/test/keeper.png', format=image.format)
        with default_storage.open(storage_path, 'r') as f:
            new_image = Image.open(f)

        self.assertEqual(new_image.format, image.format)
Ejemplo n.º 2
0
    def _generate(self, storage_path):
        # May raise IOError if the file doesn't exist or isn't a valid image.

        # If we're here, we can assume that the adjustment doesn't already
        # exist. Try to create one from the storage path. Raises IOError if
        # something goes wrong.
        kwargs = {
            'requested': self.requested,
            'storage_path': storage_path
        }

        with default_storage.open(storage_path, 'rb') as im_file:
            im = Image.open(im_file)
            try:
                im.verify()
            except Exception:
                # Raise an IOError if the image isn't valid.
                raise IOError
            im_file.seek(0)
            im = Image.open(im_file)
            im.load()
        format = im.format if im.format in KEEP_FORMATS else DEFAULT_FORMAT

        if self.adjust_uses_areas:
            areas = self.get_areas(storage_path)
        else:
            areas = None

        for adjustment in self.adjustments:
            im = adjustment.adjust(im, areas=areas)

        adjusted = AdjustedImage(**kwargs)
        f = adjusted._meta.get_field('adjusted')

        args = (six.text_type(kwargs), datetime.datetime.now().isoformat())
        filename = '.'.join((make_hash(*args, step=2), format.lower()))
        storage_path = f.generate_filename(adjusted, filename)

        final_path = save_image(im, storage_path, format=format,
                                storage=default_storage)
        # Try to handle race conditions gracefully.
        try:
            adjusted = AdjustedImage.objects.filter(**kwargs
                                                    ).only('adjusted')[:1][0]
        except IndexError:
            adjusted.adjusted = final_path
            adjusted.save()
        else:
            default_storage.delete(final_path)
        return adjusted
Ejemplo n.º 3
0
    def test_non_keeper(self):
        """
        If the format is a weird one, such as a .psd, then the image should
        be saved as the default format rather than the original.

        """
        image = Image.open(self._data_path('100x50.psd'))
        self.assertNotIn(image.format, KEEP_FORMATS)

        storage_path = save_image(image, 'daguerre/test/nonkeeper.png', format=image.format)
        with default_storage.open(storage_path, 'r') as f:
            new_image = Image.open(f)

        self.assertEqual(new_image.format, DEFAULT_FORMAT)
Ejemplo n.º 4
0
    def _generate(self, storage_path):
        # May raise IOError if the file doesn't exist or isn't a valid image.

        # If we're here, we can assume that the adjustment doesn't already
        # exist. Try to create one from the storage path. Raises IOError if
        # something goes wrong.
        kwargs = {
            'requested': self.requested,
            'storage_path': storage_path
        }

        with default_storage.open(storage_path, 'rb') as im_file:
            im = Image.open(im_file)
            try:
                im.verify()
            except (IndexError, struct.error, SyntaxError):
                # Raise an IOError if the image isn't valid.
                raise IOError
            im_file.seek(0)
            im = Image.open(im_file)
            im.load()
        format = im.format if im.format in KEEP_FORMATS else DEFAULT_FORMAT

        if self.adjust_uses_areas:
            areas = self.get_areas(storage_path)
        else:
            areas = None

        for adjustment in self.adjustments:
            im = adjustment.adjust(im, areas=areas)

        adjusted = AdjustedImage(**kwargs)
        f = adjusted._meta.get_field('adjusted')

        args = (six.text_type(kwargs), datetime.datetime.now().isoformat())
        filename = '.'.join((make_hash(*args, step=2), format.lower()))
        storage_path = f.generate_filename(adjusted, filename)

        final_path = save_image(im, storage_path, format=format,
                                storage=default_storage)
        # Try to handle race conditions gracefully.
        try:
            adjusted = AdjustedImage.objects.filter(**kwargs
                                                    ).only('adjusted')[:1][0]
        except IndexError:
            adjusted.adjusted = final_path
            adjusted.save()
        else:
            default_storage.delete(final_path)
        return adjusted
Ejemplo n.º 5
0
    def test_keeper(self):
        """
        If the format is in KEEP_FORMATS, it should be preserved.

        """
        image = Image.open(self._data_path('100x100.png'))
        self.assertIn(image.format, KEEP_FORMATS)

        storage_path = save_image(image,
                                  'daguerre/test/keeper.png',
                                  format=image.format)
        with default_storage.open(storage_path, 'rb') as f:
            new_image = Image.open(f)

        self.assertEqual(new_image.format, image.format)
Ejemplo n.º 6
0
    def test_non_keeper(self):
        """
        If the format is a weird one, such as a .psd, then the image should
        be saved as the default format rather than the original.

        """
        image = Image.open(self._data_path('100x50.psd'))
        self.assertNotIn(image.format, KEEP_FORMATS)

        storage_path = save_image(image,
                                  'daguerre/test/nonkeeper.png',
                                  format=image.format)
        with default_storage.open(storage_path, 'rb') as f:
            new_image = Image.open(f)

        self.assertEqual(new_image.format, DEFAULT_FORMAT)
Ejemplo n.º 7
0
    def adjust(self):
        # May raise IOError.

        # First try to fetch a version that already exists.
        kwargs = self.get_query_kwargs()
        try:
            return AdjustedImage.objects.filter(**kwargs)[:1][0]
        except IndexError:
            pass

        # If that fails, try to create one from the storage path.
        # Raises IOError if something goes wrong.
        adjustment = self.adjustment_for_path(self.storage_path)
        im = adjustment.adjust()

        creation_kwargs = {}
        for k, v in kwargs.iteritems():
            if k.endswith('__isnull'):
                creation_kwargs[k[:-len('__isnull')]] = None
            else:
                creation_kwargs[k] = v

        adjusted = AdjustedImage(**creation_kwargs)
        f = adjusted._meta.get_field('adjusted')

        format = (adjustment.format
                  if adjustment.format in KEEP_FORMATS else DEFAULT_FORMAT)
        args = (unicode(creation_kwargs), datetime.datetime.now().isoformat())
        filename = '.'.join((make_hash(*args, step=2), format.lower()))
        storage_path = f.generate_filename(adjusted, filename)

        final_path = save_image(im, storage_path, format=format,
                                storage=default_storage)
        # Try to handle race conditions gracefully.
        try:
            adjusted = AdjustedImage.objects.filter(**kwargs)[:1][0]
        except IndexError:
            adjusted.adjusted = final_path
            adjusted.save()
        else:
            default_storage.delete(final_path)
        return adjusted
Ejemplo n.º 8
0
	def create_image(self, test_path):
		image = Image.open(self._data_path(test_path))
		return save_image(image, 'daguerre/test/{0}'.format(test_path))
Ejemplo n.º 9
0
 def create_image(self, test_path):
     image = Image.open(self._data_path(test_path))
     return save_image(image, 'daguerre/test/{0}'.format(test_path))