예제 #1
0
    def _get_helpers(self):
        if not hasattr(settings, 'DAGUERRE_PREADJUSTMENTS'):
            raise CommandError(NO_ADJUSTMENTS)
        dp = settings.DAGUERRE_PREADJUSTMENTS
        helpers = []
        try:
            for (model_or_iterable, adjustments, lookup) in dp:
                if isinstance(model_or_iterable, six.string_types):
                    app_label, model_name = model_or_iterable.split('.')
                    model_or_iterable = get_model(app_label, model_name)
                if (isinstance(model_or_iterable, six.class_types) and
                        issubclass(model_or_iterable, Model)):
                    iterable = model_or_iterable.objects.all()
                elif isinstance(model_or_iterable, QuerySet):
                    iterable = model_or_iterable._clone()
                else:
                    iterable = model_or_iterable

                helper = AdjustmentHelper(iterable, lookup=lookup, generate=True)
                for adjustment in adjustments:
                    helper.adjust(adjustment)
                helper._finalize()
                helpers.append(helper)
        except (ValueError, TypeError, LookupError):
            raise CommandError(BAD_STRUCTURE)

        return helpers
예제 #2
0
 def test_lookup__invalid(self):
     storage_path = 'path/to/somewhe.re'
     iterable = [BulkTestObject({'_bar': storage_path})]
     helper = AdjustmentHelper(iterable, lookup="storage_path._bar")
     helper.adjust('fit', width=50, height=50)
     helper._finalize()
     self.assertEqual(helper.adjusted, {iterable[0]: {}})
     self.assertEqual(helper.remaining, {})
예제 #3
0
 def test_adjust_crop__100x50(self):
     expected = Image.open(self._data_path('100x50_crop.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('crop', width=100, height=50)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
예제 #4
0
 def test_adjust_crop__100x50(self):
     expected = Image.open(self._data_path('100x50_crop.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('crop', width=100, height=50)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
예제 #5
0
 def test_adjust_crop__50x100(self):
     expected = Image.open(self._data_path('50x100_crop.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('crop', width=50, height=100)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
     # Make sure that the path is properly formatted.
     self.assertTrue(adjusted.adjusted.path.endswith('.png'))
예제 #6
0
 def test_adjust_crop__50x50_area(self):
     self.create_area(storage_path=self.base_image, x1=21, x2=70, y1=46,
                      y2=95)
     expected = Image.open(self._data_path('50x50_crop_area.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('crop', width=50, height=50)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
예제 #7
0
 def test_adjust_crop__50x100(self):
     expected = Image.open(self._data_path('50x100_crop.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('crop', width=50, height=100)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
     # Make sure that the path is properly formatted.
     self.assertTrue(adjusted.adjusted.path.endswith('.png'))
예제 #8
0
 def test_lookup__invalid(self):
     storage_path = 'path/to/somewhe.re'
     iterable = [
         BulkTestObject({'_bar': storage_path})
     ]
     helper = AdjustmentHelper(iterable, lookup="storage_path._bar")
     helper.adjust('fit', width=50, height=50)
     helper._finalize()
     self.assertEqual(helper.adjusted, {iterable[0]: {}})
     self.assertEqual(helper.remaining, {})
예제 #9
0
 def test_named_crop(self):
     self.create_area(storage_path=self.base_image, x1=21, x2=70, y1=46,
                      y2=95, name='area')
     expected = Image.open(self._data_path('25x25_fit_named_crop.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('namedcrop', name='area')
         helper.adjust('fit', width=25, height=25)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
예제 #10
0
    def test_info_dicts__semiprepped(self):
        images = [
            self.create_image('100x100.png'),
            self.create_image('100x100.png'),
            self.create_image('100x50_crop.png'),
            self.create_image('50x100_crop.png'),
        ]
        iterable = [BulkTestObject(image) for image in images]

        helper = AdjustmentHelper(iterable, lookup='storage_path', generate=False)
        helper.adjust('crop', width=50, height=50)
        with self.assertNumQueries(1):
            helper._finalize()
예제 #11
0
 def test_adjust_crop__50x50_area(self):
     self.create_area(storage_path=self.base_image,
                      x1=21,
                      x2=70,
                      y1=46,
                      y2=95)
     expected = Image.open(self._data_path('50x50_crop_area.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('crop', width=50, height=50)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
예제 #12
0
    def test_info_dicts__non_bulk(self):
        images = [
            self.create_image('100x100.png'),
            self.create_image('100x100.png'),
            self.create_image('100x50_crop.png'),
            self.create_image('50x100_crop.png'),
        ]

        adj = Crop(width=50, height=50)
        with self.assertNumQueries(4):
            for image in images:
                helper = AdjustmentHelper([image], generate=False)
                helper.adjust(adj)
                helper._finalize()
    def test_adjust__broken(self):
        broken_file = self._data_file('broken.png', 'rb')
        storage_path = default_storage.save('daguerre/test/broken.png',
                                            ContentFile(broken_file.read()))
        broken_file = default_storage.open(storage_path, 'rb')
        image = Image.open(broken_file)
        self.assertRaises(IndexError, image.verify)

        helper = AdjustmentHelper([storage_path], generate=True)
        helper.adjust('fill', width=50, height=50)
        with self.assertNumQueries(1):
            helper._finalize()
        self.assertEqual(helper.adjusted, {helper.iterable[0]: {}})
        self.assertEqual(helper.remaining, {})
예제 #14
0
    def test_info_dicts__non_bulk(self):
        images = [
            self.create_image('100x100.png'),
            self.create_image('100x100.png'),
            self.create_image('100x50_crop.png'),
            self.create_image('50x100_crop.png'),
        ]

        adj = Crop(width=50, height=50)
        with self.assertNumQueries(4):
            for image in images:
                helper = AdjustmentHelper([image], generate=False)
                helper.adjust(adj)
                helper._finalize()
예제 #15
0
    def test_adjust__broken(self):
        broken_file = self._data_file('broken.png', 'rb')
        storage_path = default_storage.save('daguerre/test/broken.png',
                                            ContentFile(broken_file.read()))
        broken_file = default_storage.open(storage_path, 'rb')
        image = Image.open(broken_file)
        self.assertRaises(IndexError, image.verify)

        helper = AdjustmentHelper([storage_path], generate=True)
        helper.adjust('fill', width=50, height=50)
        with self.assertNumQueries(1):
            helper._finalize()
        self.assertEqual(helper.adjusted, {helper.iterable[0]: {}})
        self.assertEqual(helper.remaining, {})
예제 #16
0
 def test_named_crop(self):
     self.create_area(storage_path=self.base_image,
                      x1=21,
                      x2=70,
                      y1=46,
                      y2=95,
                      name='area')
     expected = Image.open(self._data_path('25x25_fit_named_crop.png'))
     with self.assertNumQueries(4):
         helper = AdjustmentHelper([self.base_image], generate=True)
         helper.adjust('namedcrop', name='area')
         helper.adjust('fit', width=25, height=25)
         helper._finalize()
     adjusted = AdjustedImage.objects.get()
     self.assertImageEqual(Image.open(adjusted.adjusted.path), expected)
예제 #17
0
    def test_info_dicts__semiprepped(self):
        images = [
            self.create_image('100x100.png'),
            self.create_image('100x100.png'),
            self.create_image('100x50_crop.png'),
            self.create_image('50x100_crop.png'),
        ]
        iterable = [BulkTestObject(image) for image in images]

        helper = AdjustmentHelper(iterable,
                                  lookup='storage_path',
                                  generate=False)
        helper.adjust('crop', width=50, height=50)
        with self.assertNumQueries(1):
            helper._finalize()
예제 #18
0
    def test_adjust__nonexistant(self):
        """
        Adjusting a path that doesn't exist should raise an IOError.

        """
        storage_path = 'nonexistant.png'
        self.assertFalse(default_storage.exists(storage_path))
        helper = AdjustmentHelper([storage_path], generate=True)
        helper.adjust('fit', width=50, height=50)
        # We still do get one query because the first try is always for
        # an AdjustedImage, whether or not the original file exists.
        # This is for historic reasons and doesn't necessarily need to
        # continue to be the case.
        with self.assertNumQueries(1):
            helper._finalize()
        self.assertEqual(helper.adjusted, {helper.iterable[0]: {}})
        self.assertEqual(helper.remaining, {})
예제 #19
0
    def test_adjust__nonexistant(self):
        """
        Adjusting a path that doesn't exist should raise an IOError.

        """
        storage_path = 'nonexistant.png'
        self.assertFalse(default_storage.exists(storage_path))
        helper = AdjustmentHelper([storage_path], generate=True)
        helper.adjust('fit', width=50, height=50)
        # We still do get one query because the first try is always for
        # an AdjustedImage, whether or not the original file exists.
        # This is for historic reasons and doesn't necessarily need to
        # continue to be the case.
        with self.assertNumQueries(1):
            helper._finalize()
        self.assertEqual(helper.adjusted, {helper.iterable[0]: {}})
        self.assertEqual(helper.remaining, {})
예제 #20
0
    def test_readjust(self):
        """
        Adjusting a previously-adjusted image should return the previous
        adjustment.

        """
        new_im = Image.open(self._data_path('50x100_crop.png'))
        with self.assertNumQueries(4):
            helper = AdjustmentHelper([self.base_image], generate=True)
            helper.adjust('crop', width=50, height=100)
            helper._finalize()
        adjusted = AdjustedImage.objects.get()
        self.assertImageEqual(Image.open(adjusted.adjusted.path), new_im)

        with self.assertNumQueries(1):
            helper = AdjustmentHelper([self.base_image], generate=True)
            helper.adjust('crop', width=50, height=100)
            helper._finalize()
        self.assertEqual(AdjustedImage.objects.count(), 1)
예제 #21
0
    def test_readjust(self):
        """
        Adjusting a previously-adjusted image should return the previous
        adjustment.

        """
        new_im = Image.open(self._data_path('50x100_crop.png'))
        with self.assertNumQueries(4):
            helper = AdjustmentHelper([self.base_image], generate=True)
            helper.adjust('crop', width=50, height=100)
            helper._finalize()
        adjusted = AdjustedImage.objects.get()
        self.assertImageEqual(Image.open(adjusted.adjusted.path), new_im)

        with self.assertNumQueries(1):
            helper = AdjustmentHelper([self.base_image], generate=True)
            helper.adjust('crop', width=50, height=100)
            helper._finalize()
        self.assertEqual(AdjustedImage.objects.count(), 1)
예제 #22
0
    def test_readjust_multiple(self):
        """
        If there are multiple adjusted versions of the image with the same
        parameters, one of them should be returned rather than erroring out.

        """
        with self.assertNumQueries(4):
            helper = AdjustmentHelper([self.base_image], generate=True)
            helper.adjust('crop', width=50, height=100)
            helper._finalize()
        adjusted1 = AdjustedImage.objects.get()
        adjusted2 = AdjustedImage.objects.get()
        adjusted2.pk = None
        adjusted2.save()
        self.assertNotEqual(adjusted1.pk, adjusted2.pk)

        helper = AdjustmentHelper([self.base_image])
        helper.adjust('crop', width=50, height=100)
        with self.assertNumQueries(1):
            helper._finalize()
        url = list(helper.adjusted.values())[0]['url']
        self.assertEqual(url, adjusted1.adjusted.url)
        self.assertEqual(url, adjusted2.adjusted.url)
예제 #23
0
    def test_readjust_multiple(self):
        """
        If there are multiple adjusted versions of the image with the same
        parameters, one of them should be returned rather than erroring out.

        """
        with self.assertNumQueries(4):
            helper = AdjustmentHelper([self.base_image], generate=True)
            helper.adjust('crop', width=50, height=100)
            helper._finalize()
        adjusted1 = AdjustedImage.objects.get()
        adjusted2 = AdjustedImage.objects.get()
        adjusted2.pk = None
        adjusted2.save()
        self.assertNotEqual(adjusted1.pk, adjusted2.pk)

        helper = AdjustmentHelper([self.base_image])
        helper.adjust('crop', width=50, height=100)
        with self.assertNumQueries(1):
            helper._finalize()
        url = list(helper.adjusted.values())[0]['url']
        self.assertEqual(url, adjusted1.adjusted.url)
        self.assertEqual(url, adjusted2.adjusted.url)
예제 #24
0
class BrokenImageAdjustmentHelperTestCase(BaseTestCase):

    def setUp(self):
        super(BrokenImageAdjustmentHelperTestCase, self).setUp()

        self.broken_file = self._data_file('broken.png', 'rb')
        self.storage_path = default_storage.save(
            'daguerre/test/broken.png', ContentFile(self.broken_file.read()))
        self.helper = AdjustmentHelper([self.storage_path], generate=True)

    def test_adjust__broken(self):
        self.helper.adjust('fill', width=50, height=50)
        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_struct_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = struct.error

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_indexerror_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = IndexError('index out of range')

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_io_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = IOError('truncated png file')

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_syntax_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = SyntaxError('broken png file')

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})
예제 #25
0
class BrokenImageAdjustmentHelperTestCase(BaseTestCase):
    def setUp(self):
        super(BrokenImageAdjustmentHelperTestCase, self).setUp()

        self.broken_file = self._data_file('broken.png', 'rb')
        self.storage_path = default_storage.save(
            'daguerre/test/broken.png', ContentFile(self.broken_file.read()))
        self.helper = AdjustmentHelper([self.storage_path], generate=True)

    def test_adjust__broken(self):
        self.helper.adjust('fill', width=50, height=50)
        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_struct_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = struct.error

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_indexerror_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = IndexError('index out of range')

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_io_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = IOError('truncated png file')

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})

    @mock.patch('daguerre.helpers.Image')
    def test_adjust__broken_with_syntax_error(self, image_mock):
        bad_image = image_mock.open.return_value
        bad_image.verify.side_effect = SyntaxError('broken png file')

        self.helper.adjust('fill', width=50, height=50)

        with self.assertNumQueries(1):
            self.helper._finalize()
        self.assertEqual(self.helper.adjusted, {self.helper.iterable[0]: {}})
        self.assertEqual(self.helper.remaining, {})