Ticket #13841: 13841_tests_docs.diff

File 13841_tests_docs.diff, 4.7 KB (added by Colin Copeland, 13 years ago)

Combined diff with tests and docs

  • django/template/context.py

    diff --git a/django/template/context.py b/django/template/context.py
    index 8f70d70..383e184 100644
    a b  
    11from copy import copy
     2from inspect import getargspec
    23from django.core.exceptions import ImproperlyConfigured
    34from django.utils.importlib import import_module
    45
    class RequestContext(Context):  
    169170        else:
    170171            processors = tuple(processors)
    171172        for processor in get_standard_processors() + processors:
    172             self.update(processor(request))
     173            if not hasattr(processor, 'pass_context'):
     174                arginfo = getargspec(processor)
     175                processor.pass_context = len(arginfo[0]) > 1
     176            if processor.pass_context:
     177                self.update(processor(request, context=self))
     178            else:
     179                self.update(processor(request))
  • docs/ref/templates/api.txt

    diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt
    index c8100fc..db19756 100644
    a b Writing your own context processors  
    526526~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    527527
    528528A context processor has a very simple interface: It's just a Python function
    529 that takes one argument, an :class:`~django.http.HttpRequest` object, and
    530 returns a dictionary that gets added to the template context. Each context
    531 processor *must* return a dictionary.
     529that takes at least one argument, an :class:`~django.http.HttpRequest` object.
     530
     531.. versionadded:: 1.2
     532   The optional keyword argument ``context`` is now supported, which allows
     533   access to the current :class:`~django.template.Context` object.
     534
     535The function returns a dictionary that gets added to the template context.
     536Each context processor *must* return a dictionary.
    532537
    533538Custom context processors can live anywhere in your code base. All Django cares
    534539about is that your custom context processors are pointed-to by your
  • tests/regressiontests/context_processors/tests.py

    diff --git a/tests/regressiontests/context_processors/tests.py b/tests/regressiontests/context_processors/tests.py
    index 1f2209e..53eefca 100644
    a b  
    22Tests for Django's bundled context processors.
    33"""
    44from django.test import TestCase
     5from django.template import RequestContext, Template
     6from django.test.client import RequestFactory
    57
    68
    79class RequestContextProcessorTests(TestCase):
    class RequestContextProcessorTests(TestCase):  
    1113
    1214    urls = 'regressiontests.context_processors.urls'
    1315
     16    def _render(self, template, context, processors=None):
     17        """
     18        Helper function to ease testing of context processors without
     19        needing templates on the file system
     20        """
     21        factory = RequestFactory()
     22        request = factory.get('/request_attrs/')
     23        context = RequestContext(request, context, processors=processors)
     24        return Template(template).render(context)
     25
    1426    def test_request_attributes(self):
    1527        """
    1628        Test that the request object is available in the template and that its
    class RequestContextProcessorTests(TestCase):  
    3547        response = self.client.post(url, {'path': '/blah/'})
    3648        self.assertContains(response, url)
    3749
     50    def test_no_passed_context(self):
     51        """
     52        Context processors not expecting the "context" argument should
     53        continue to work as usual (refs #13841)
     54        """
     55        def add_vegetable(request):
     56            return {'vegetable': 'broccoli'}
     57        # vegetable shuldn't exist yet in the context
     58        content =  self._render('{{ fruit }} {{ vegetable }}',
     59                                {'fruit': 'apple'})
     60        self.assertEqual(content, 'apple ')
     61        # add_vegetable context processor should add "broccoli" to context
     62        content =  self._render('{{ fruit }} {{ vegetable }}',
     63                                {'fruit': 'apple'}, [add_vegetable])
     64        self.assertEqual(content, 'apple broccoli')
     65
     66    def test_can_access_context(self):
     67        """
     68        Test context processor expecting the "context" argument can access
     69        and modify the context (refs #13841)
     70        """
     71        def pluralize_fruit(request, context):
     72            return {'fruit': context['fruit'] + 's'}
     73        # should just print apple (default behavior)
     74        content =  self._render('{{ fruit }}', {'fruit': 'apple'})
     75        self.assertEqual(content, 'apple')
     76        # the pluralize_fruit context processor should add an 's' to apple
     77        content =  self._render('{{ fruit }}', {'fruit': 'apple'},
     78                                [pluralize_fruit])
     79        self.assertEqual(content, 'apples')
Back to Top