Example #1
0
    def test_render_none_to_string(self):
        """
        Tests that an ImageNode without image renders an empty string
        """
        context = {"image": None}
        node = ImageNode(Variable("image"), "original")

        rendered = node.render(context)

        self.assertEqual(rendered, "")
Example #2
0
    def test_render_valid_image_to_string(self):
        """
        Tests that an ImageNode with a valid image renders an img tag
        """
        context = {"image": self.image}
        node = ImageNode(Variable("image"), "original")

        rendered = node.render(context)

        self.assertIn('<img alt="Test image"', rendered)
Example #3
0
    def test_render_none_to_string(self):
        """
        Tests that an ImageNode without image renders an empty string
        """
        context = {'image': None}
        node = ImageNode(Variable('image'), 'original')

        rendered = node.render(context)

        self.assertEqual(rendered, '')
Example #4
0
    def test_render_none_as_context_variable(self):
        """
        Tests that an ImageNode without an image and a context variable name
        renders an empty string and puts None in the context variable
        """
        context = {"image": None, "image_node": "fake value"}
        node = ImageNode(Variable("image"), "original", "image_node")

        rendered = node.render(context)

        self.assertEqual(rendered, "")
        self.assertIsNone(context["image_node"])
Example #5
0
    def test_render_valid_image_as_context_variable(self):
        """
        Tests that an ImageNode with a valid image and a context variable name
        renders an empty string and puts a rendition in the context variable
        """
        context = {"image": self.image, "image_node": "fake value"}
        node = ImageNode(Variable("image"), "original", "image_node")

        rendered = node.render(context)

        self.assertEqual(rendered, "")
        self.assertIsInstance(context["image_node"], Rendition)
Example #6
0
    def test_render_none_as_context_variable(self):
        """
        Tests that an ImageNode without an image and a context variable name
        renders an empty string and puts None in the context variable
        """
        context = {'image': None, 'image_node': 'fake value'}
        node = ImageNode(Variable('image'), 'original', 'image_node')

        rendered = node.render(context)

        self.assertEqual(rendered, '')
        self.assertIsNone(context['image_node'])
Example #7
0
    def test_render_valid_image_as_context_variable(self):
        """
        Tests that an ImageNode with a valid image and a context variable name
        renders an empty string and puts a rendition in the context variable
        """
        context = {'image': self.image, 'image_node': 'fake value'}
        node = ImageNode(Variable('image'), 'original', 'image_node')

        rendered = node.render(context)

        self.assertEqual(rendered, '')
        self.assertIsInstance(context['image_node'], Rendition)
Example #8
0
def responsiveimage(parser, token):
    bits = token.split_contents()[1:]
    image_var = bits[0]
    filter_spec = bits[1]
    bits = bits[2:]

    if len(bits) == 2 and bits[0] == 'as':
        # token is of the form {% image self.photo max-320x200 as img %}
        return ImageNode(image_var, filter_spec, output_var_name=bits[1])
    else:
        # token is of the form {% image self.photo max-320x200 %} - all additional tokens
        # should be kwargs, which become attributes
        attrs = {}
        for bit in bits:
            try:
                name, value = bit.split('=')
            except ValueError:
                raise template.TemplateSyntaxError(
                    "'image' tag should be of the form {% image self.photo max-320x200 [ custom-attr=\"value\" ... ] %} or {% image self.photo max-320x200 as img %}"
                )

            attrs[name] = parser.compile_filter(
                value)  # setup to resolve context variables as value

        return ResponsiveImageNode(image_var, filter_spec, attrs=attrs)