Exemplo n.º 1
0
 def test_get_flatpages_with_prefix_for_user(self):
     "The flatpage template tag retrieve prefixed flatpages for an authenticated user"
     me = User.objects.create_user('testuser', '*****@*****.**', 's3krit')
     out = Template(
         "{% load flatpages %}"
         "{% get_flatpages '/location/' for me as location_flatpages %}"
         "{% for page in location_flatpages %}"
         "{{ page.title }},"
         "{% endfor %}").render(Context({'me': me}))
     self.assertEqual(out, "A Nested Flatpage,Sekrit Nested Flatpage,")
Exemplo n.º 2
0
    def test_translates_multiple_percent_signs(self):
        expected = (
            '1 % signe pour cent, signes %% 2 pour cent, trois signes de pourcentage %%%'
        )

        trans_tpl = Template(
            '{% load i18n %}{% trans "1 percent sign %, 2 percent signs %%, '
            '3 percent signs %%%" %}')
        self.assertEqual(trans_tpl.render(Context({})), expected)
        block_tpl = Template(
            '{% load i18n %}{% blocktrans %}1 percent sign %, 2 percent signs '
            '%%, 3 percent signs %%%{% endblocktrans %}')
        self.assertEqual(block_tpl.render(Context({})), expected)

        block_tpl = Template(
            '{% load i18n %}{% blocktrans %}{{name}} says: 1 percent sign %, '
            '2 percent signs %%{% endblocktrans %}')
        self.assertEqual(
            block_tpl.render(Context({"name": "Django"})),
            'Django dit: 1 pour cent signe %, deux signes de pourcentage %%')
Exemplo n.º 3
0
    def test_no_extra_query_when_accessing_attrs(self):
        """
        ModelChoiceField with RadioSelect widget doesn't produce unnecessary
        db queries when accessing its BoundField's attrs.
        """
        class ModelChoiceForm(forms.Form):
            category = forms.ModelChoiceField(Category.objects.all(), widget=forms.RadioSelect)

        form = ModelChoiceForm()
        field = form['category']  # BoundField
        template = Template('{{ field.name }}{{ field }}{{ field.help_text }}')
        with self.assertNumQueries(1):
            template.render(Context({'field': field}))
Exemplo n.º 4
0
 def humanize_tester(self,
                     test_list,
                     result_list,
                     method,
                     normalize_result_func=escape):
     for test_content, result in zip(test_list, result_list):
         t = Template('{%% load humanize %%}{{ test_content|%s }}' % method)
         rendered = t.render(Context(locals())).strip()
         self.assertEqual(
             rendered,
             normalize_result_func(result),
             msg="%s test failed, produced '%s', should've produced '%s'" %
             (method, rendered, result))
Exemplo n.º 5
0
    def test_num_queries(self):
        """
        Widgets that render multiple subwidgets shouldn't make more than one
        database query.
        """
        categories = Category.objects.all()

        class CategoriesForm(forms.Form):
            radio = forms.ModelChoiceField(queryset=categories, widget=forms.RadioSelect)
            checkbox = forms.ModelMultipleChoiceField(queryset=categories, widget=forms.CheckboxSelectMultiple)

        template = Template(
            '{% for widget in form.checkbox %}{{ widget }}{% endfor %}'
            '{% for widget in form.radio %}{{ widget }}{% endfor %}'
        )
        with self.assertNumQueries(2):
            template.render(Context({'form': CategoriesForm()}))
Exemplo n.º 6
0
    def test_regress_3871(self):
        related = RelatedModel.objects.create()

        relation = RelationModel()
        relation.fk = related
        relation.gfk = related
        relation.save()
        relation.m2m.add(related)

        t = Template(
            '{{ related.test_fk.all.0 }}{{ related.test_gfk.all.0 }}{{ related.test_m2m.all.0 }}'
        )

        self.assertEqual(
            t.render(Context({'related': related})),
            ''.join([str(relation.pk)] * 3),
        )
Exemplo n.º 7
0
 def assertRenderEqual(self, tpl, expected, **context):
     context = Context(context)
     tpl = Template(tpl)
     self.assertEqual(tpl.render(context), expected)
Exemplo n.º 8
0
 def render(t):
     return Template(t).render(Context())