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]

        adj = Crop(width=50, height=50)

        helper = AdjustmentHelper(iterable, [adj], 'storage_path')
        with self.assertNumQueries(1):
            helper.info_dicts()
Beispiel #2
0
    def render(self, context):
        iterable = self.iterable.resolve(context)

        adj_list = []
        for adj, kwargs in self.adjustments:
            adj_list.append((adj.resolve(context),
                             dict((k, v.resolve(context))
                             for k, v in six.iteritems(kwargs))))

        # First adjustment *might* be a lookup.
        # We consider it a lookup if it is not an adjustment name.
        if adj_list and adj_list[0][0] in registry:
            lookup = None
        else:
            lookup = adj_list[0][0]
            adj_list = adj_list[1:]

        adj_instances = []
        for adj, kwargs in adj_list:
            try:
                adj_cls = registry[adj]
                adj_instances.append(adj_cls(**kwargs))
            except (KeyError, ValueError):
                if settings.TEMPLATE_DEBUG:
                    raise
                context[self.asvar] = []
                return ''

        helper = AdjustmentHelper(iterable, adj_instances, lookup)
        context[self.asvar] = helper.info_dicts()
        return ''
Beispiel #3
0
    def render(self, context):
        image = self.image.resolve(context)

        adj_instances = []
        for adj_to_resolve, kwargs_to_resolve in self.adjustments:
            adj = adj_to_resolve.resolve(context)
            kwargs = dict((k, v.resolve(context))
                          for k, v in six.iteritems(kwargs_to_resolve))
            try:
                adj_cls = registry[adj]
                adj_instances.append(adj_cls(**kwargs))
            except (KeyError, ValueError):
                if settings.TEMPLATE_DEBUG:
                    raise
                if self.asvar is not None:
                    context[self.asvar] = AdjustmentInfoDict()
                return ''

        helper = AdjustmentHelper([image], adj_instances)
        info_dict = helper.info_dicts()[0][1]

        if self.asvar is not None:
            context[self.asvar] = info_dict
            return ''
        return escape(info_dict.get('url', ''))
 def test_path(self):
     # Tag should accept a path as its argument.
     storage_path = self.create_image('100x100.png')
     helper = AdjustmentHelper([storage_path], [Fit(width=50, height=50)])
     t = Template("{% load daguerre %}{% adjust image 'fit' width=50 "
                  "height=50 %}")
     c = Context({'image': storage_path})
     self.assertEqual(t.render(c), escape(helper.info_dicts()[0][1]['url']))
 def test_multiple(self):
     # Tag should allow multiple adjustments to be passed in.
     storage_path = self.create_image('100x100.png')
     helper = AdjustmentHelper([storage_path], [Crop(width=50, height=50),
                                                Fit(width=25)])
     t = Template("{% load daguerre %}{% adjust image 'crop' width=50 "
                  "height=50 'fit' width=25 %}")
     c = Context({'image': storage_path})
     self.assertEqual(t.render(c), escape(helper.info_dicts()[0][1]['url']))
 def test_file(self):
     # Tag should accept an :class:`ImageFieldFile` as its argument.
     storage_path = self.create_image('100x100.png')
     adjusted = AdjustedImage()
     adjusted.adjusted = storage_path
     helper = AdjustmentHelper([storage_path], [Fit(width=50, height=50)])
     t = Template("{% load daguerre %}{% adjust image 'fit' width=50 "
                  "height=50 as adj %}{{ adj }}")
     c = Context({'image': adjusted.adjusted})
     self.assertEqual(t.render(c), escape(helper.info_dicts()[0][1]['url']))
 def test_no_lookups(self):
     # Tag should accept an iterable of paths.
     paths = [
         self.create_image('100x100.png')
     ]
     helper = AdjustmentHelper(paths,
                               [Fit(width=50, height=50)])
     t = Template("{% load daguerre %}{% adjust_bulk paths 'fit' "
                  "width=50 height=50 as bulk %}{{ bulk.0.1 }}")
     c = Context({'paths': paths})
     self.assertEqual(t.render(c),
                      escape(helper.info_dicts()[0][1]['url']))
 def test_paths(self):
     # Tag should accept an iterable of objects with paths.
     objs = [
         BulkTestObject(self.create_image('100x100.png'))
     ]
     helper = AdjustmentHelper(objs, [Fit(width=50, height=50)],
                               'storage_path')
     t = Template("{% load daguerre %}{% adjust_bulk objs 'storage_path' "
                  "'fit' width=50 height=50 as bulk %}"
                  "{{ bulk.0.1 }}")
     c = Context({'objs': objs})
     self.assertEqual(t.render(c),
                      escape(helper.info_dicts()[0][1]['url']))
Beispiel #9
0
 def _bulk_adjusted_items(self, items):
     if self.feed_type is JSONGenerator:
         sizes = THUMBNAIL_SIZES
     else:
         sizes = THUMBNAIL_SIZES[:2]
     for size in sizes:
         helper = AdjustmentHelper(items,
                                   [Fill(width=size[0], height=size[1])],
                                   "thumbnail")
         for item, info_dict in helper.info_dicts():
             # Set a private attribute so we can retrieve this later.
             if not hasattr(item, '_adjusted'):
                 item._adjusted = {}
             item._adjusted[size] = info_dict
     # set the default adjustment as a public attribute so that it
     # can be accessed from the description template.
     for item in items:
         item.description_thumbnail = item._adjusted[THUMBNAIL_SIZES[1]]
     return items
 def _bulk_adjusted_items(self, items):
     if self.feed_type is JSONGenerator:
         sizes = THUMBNAIL_SIZES
     else:
         sizes = THUMBNAIL_SIZES[:2]
     for size in sizes:
         helper = AdjustmentHelper(items,
                                   [Fill(width=size[0], height=size[1])],
                                   "thumbnail")
         for item, info_dict in helper.info_dicts():
             # Set a private attribute so we can retrieve this later.
             if not hasattr(item, '_adjusted'):
                 item._adjusted = {}
             item._adjusted[size] = info_dict
     # set the default adjustment as a public attribute so that it
     # can be accessed from the description template.
     for item in items:
         item.description_thumbnail = item._adjusted[THUMBNAIL_SIZES[1]]
     return items
Beispiel #11
0
    def render(self, context):
        video = self.video.resolve(context)

        # Backwards-compat: livesearch should just use the thumbnail_url.
        if getattr(video, '_livesearch', False):
            info_dict = AdjustmentInfoDict({
                'width': self.width,
                'height': self.height,
                'url': video.thumbnail_url
            })
        else:
            storage_path = None

            if video.has_thumbnail:
                storage_path = video.thumbnail_path
            elif video.feed_id and video.feed.has_thumbnail:
                storage_path = video.feed.thumbnail_path
            elif video.search_id and video.search.has_thumbnail:
                storage_path = video.search.thumbnail_path

            helper = AdjustmentHelper([storage_path],
                                      [Fill(width=self.width,
                                            height=self.height)])
            info_dict = helper.info_dicts()[0][1]

            # localtv_thumbnail has always fallen back in the code.
            if not info_dict:
                info_dict = AdjustmentInfoDict({
                    'width': self.width,
                    'height': self.height,
                    'url': staticfiles_storage.url('localtv/images/default_vid.gif')
                })
        
        if self.asvar is not None:
            context[self.asvar] = info_dict
            return ''
        return info_dict