Ejemplo n.º 1
0
    def test_render_context(self):
        test_context = RenderContext({'fruit': 'papaya'})

        # Test that push() limits access to the topmost dict
        test_context.push()

        test_context['vegetable'] = 'artichoke'
        self.assertEqual(list(test_context), ['vegetable'])

        self.assertNotIn('fruit', test_context)
        with self.assertRaises(KeyError):
            test_context['fruit']
        self.assertIsNone(test_context.get('fruit'))
Ejemplo n.º 2
0
    def test_render_context(self):
        test_context = RenderContext({'fruit': 'papaya'})

        # Test that push() limits access to the topmost dict
        test_context.push()

        test_context['vegetable'] = 'artichoke'
        self.assertEqual(list(test_context), ['vegetable'])

        self.assertNotIn('fruit', test_context)
        with self.assertRaises(KeyError):
            test_context['fruit']
        self.assertIsNone(test_context.get('fruit'))
Ejemplo n.º 3
0
    def test_render_context(self):
        test_context = RenderContext({"fruit": "papaya"})

        # Test that push() limits access to the topmost dict
        test_context.push()

        test_context["vegetable"] = "artichoke"
        self.assertEqual(list(test_context), ["vegetable"])

        self.assertNotIn("fruit", test_context)
        with self.assertRaises(KeyError):
            test_context["fruit"]
        self.assertIsNone(test_context.get("fruit"))
Ejemplo n.º 4
0
    def test_render_context(self):
        test_context = RenderContext({"fruit": "papaya"})

        # push() limits access to the topmost dict
        test_context.push()

        test_context["vegetable"] = "artichoke"
        self.assertEqual(list(test_context), ["vegetable"])

        self.assertNotIn("fruit", test_context)
        with self.assertRaises(KeyError):
            test_context["fruit"]
        self.assertIsNone(test_context.get("fruit"))
Ejemplo n.º 5
0
    def render(self, name, value, attrs=None):
        model_options = (self.rel_to._meta.app_label,
                         self.rel_to._meta.model_name)
        original_value = copy.copy(value)
        if attrs is None:
            attrs = {}
        attrs['name'] = name
        if self.multi:
            value = value or []
            attrs['multi'] = 'true'
            value = self.multivalue_separator.join(
                force_text(v) for v in value)
        else:
            value = value or ""

        search_fields = self.get_search_fields()
        search_fields_info = self.render_search_fields_info(
            dict(search_fields))

        if self.rel_to in self.admin_site._registry:
            related_url = reverse(
                'admin:%s_%s_changelist' % (
                    self.rel_to._meta.app_label,
                    self.rel_to._meta.model_name,
                ),
                current_app=self.admin_site.name,
            ) + self.get_url_parameters()

        context = RenderContext({
            'model':
            str(self.rel_to._meta.verbose_name),
            'data_suggest_url':
            reverse('autocomplete-list',
                    kwargs={
                        'app': self.field.rel.related_model._meta.app_label,
                        'model': self.field.rel.related_model._meta.model_name,
                        'field': self.field.name
                    }),
            'data_details_url':
            reverse('admin:{}_{}_autocomplete_details'.format(*model_options)),
            'name':
            name or "",
            'value':
            value,
            'attrs':
            flatatt(attrs),
            'related_url':
            related_url,
            'search_fields_info':
            search_fields_info,
            'prefetch_data':
            self.get_prefetch_data(original_value)
        })

        info = (self.rel_to._meta.app_label, self.rel_to._meta.model_name)
        is_polymorphic = getattr(self.rel_to, 'is_polymorphic', False)
        if not is_polymorphic and self.can_add:
            context['add_related_url'] = self.get_related_url(info, 'add')
        template = loader.get_template('admin/widgets/autocomplete.html')
        return template.render(context)
Ejemplo n.º 6
0
def contribute_to_context(context, current_app=''):
    template = mock.MagicMock()
    template.engine.string_if_invalid = ''

    context.template = template

    if VERSION >= (1, 11):
        context.render_context = RenderContext()

    if VERSION >= (1, 10):
        match = mock.MagicMock()
        match.app_name = current_app
        context.resolver_match = match

    else:
        context._current_app = current_app
Ejemplo n.º 7
0
def django_render_block(template, block_name, context, request=None):
    # Create a Django Context if needed
    if isinstance(context, Context):
        # Make a copy of the context and reset the rendering state.
        # Trying to re-use a RenderContext in multiple renders can
        # lead to TemplateNotFound errors, as Django will skip past
        # any template files it thinks it has already rendered in a
        # template's inheritance stack.
        context_instance = copy(context)
        context_instance.render_context = RenderContext()
    elif request:
        context_instance = RequestContext(request, context)
    else:
        context_instance = Context(context)

    # Get the underlying django.template.base.Template object.
    template = template.template

    # Bind the template to the context.
    with context_instance.bind_template(template):
        # Before trying to render the template, we need to traverse the tree of
        # parent templates and find all blocks in them.
        parent_template = _build_block_context(template, context_instance)

        try:
            return _render_template_block(template, block_name,
                                          context_instance)
        except BlockNotFound:
            # The block wasn't found in the current template.

            # If there's no parent template (i.e. no ExtendsNode), re-raise.
            if not parent_template:
                raise

            # Check the parent template for this block.
            return _render_template_block(parent_template, block_name,
                                          context_instance)