Ticket #14365: 14365_r13962.diff

File 14365_r13962.diff, 6.1 KB (added by Carl Meyer, 14 years ago)
  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    index c316786..60cbc03 100644
    a b from inspect import getargspec  
    5454
    5555from django.conf import settings
    5656from django.template.context import Context, RequestContext, ContextPopException
     57from django.template.signals import template_rendered
    5758from django.utils.importlib import import_module
    5859from django.utils.itercompat import is_iterable
    5960from django.utils.functional import curry, Promise
    class StringOrigin(Origin):  
    148149        return self.source
    149150
    150151class Template(object):
     152    send_rendered_signal = settings.TEMPLATE_DEBUG
     153
    151154    def __init__(self, template_string, origin=None, name='<Unknown Template>'):
    152155        try:
    153156            template_string = smart_unicode(template_string)
    class Template(object):  
    164167                yield subnode
    165168
    166169    def _render(self, context):
     170        if self.send_rendered_signal:
     171            template_rendered.send(sender=self, template=self, context=context)
    167172        return self.nodelist.render(context)
    168173
    169174    def render(self, context):
  • new file django/template/signals.py

    diff --git a/django/template/signals.py b/django/template/signals.py
    new file mode 100644
    index 0000000..a328a77
    - +  
     1from django.dispatch import Signal
     2
     3template_rendered = Signal(providing_args=["template", "context"])
  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index 08e3ff6..3251275 100644
    a b from django.core.handlers.wsgi import WSGIRequest  
    1616from django.core.signals import got_request_exception
    1717from django.http import SimpleCookie, HttpRequest, QueryDict
    1818from django.template import TemplateDoesNotExist
    19 from django.test import signals
     19from django.template import signals
    2020from django.utils.functional import curry
    2121from django.utils.encoding import smart_str
    2222from django.utils.http import urlencode
  • deleted file django/test/signals.py

    diff --git a/django/test/signals.py b/django/test/signals.py
    deleted file mode 100644
    index a328a77..0000000
    + -  
    1 from django.dispatch import Signal
    2 
    3 template_rendered = Signal(providing_args=["template", "context"])
  • django/test/utils.py

    diff --git a/django/test/utils.py b/django/test/utils.py
    index 8ecb5a0..7dda41f 100644
    a b import os  
    44from django.conf import settings
    55from django.core import mail
    66from django.core.mail.backends import locmem
    7 from django.test import signals
    87from django.template import Template
    98from django.utils.translation import deactivate
    109
    class ContextList(list):  
    4342            return False
    4443        return True
    4544
    46 def instrumented_test_render(self, context):
    47     """
    48     An instrumented Template render method, providing a signal
    49     that can be intercepted by the test system Client
    50     """
    51     signals.template_rendered.send(sender=self, template=self, context=context)
    52     return self.nodelist.render(context)
    53 
    5445
    5546def setup_test_environment():
    5647    """Perform any global pre-test setup. This involves:
    def setup_test_environment():  
    5950        - Set the email backend to the locmem email backend.
    6051        - Setting the active locale to match the LANGUAGE_CODE setting.
    6152    """
    62     Template.original_render = Template._render
    63     Template._render = instrumented_test_render
     53    Template._original_send_rendered_signal = Template.send_rendered_signal
     54    Template.send_rendered_signal = True
    6455
    6556    mail.original_SMTPConnection = mail.SMTPConnection
    6657    mail.SMTPConnection = locmem.EmailBackend
    def teardown_test_environment():  
    7970        - Restoring the email sending functions
    8071
    8172    """
    82     Template._render = Template.original_render
    83     del Template.original_render
     73    Template.send_rendered_signal = Template._original_send_rendered_signal
     74    del Template._original_send_rendered_signal
    8475
    8576    mail.SMTPConnection = mail.original_SMTPConnection
    8677    del mail.original_SMTPConnection
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index bbbcae3..ff049f1 100644
    a b import unittest  
    1616from django import template
    1717from django.core import urlresolvers
    1818from django.template import loader
     19from django.template.signals import template_rendered
    1920from django.template.loaders import app_directories, filesystem, cached
    2021from django.utils.translation import activate, deactivate, ugettext as _
    2122from django.utils.safestring import mark_safe
    class TemplateTagLoading(unittest.TestCase):  
    13891390        settings.INSTALLED_APPS = ('tagsegg',)
    13901391        t = template.Template(ttext)
    13911392
     1393
     1394class TemplateSignalTests(unittest.TestCase):
     1395    def setUp(self):
     1396        self._old_send = template.Template.send_rendered_signal
     1397
     1398        self.received = []
     1399        def _handle_signal(signal, sender, template, context, **kwargs):
     1400            self.received.append((template, context))
     1401        template_rendered.connect(_handle_signal, weak=False, dispatch_uid='test-template-rendered')
     1402
     1403    def tearDown(self):
     1404        template_rendered.disconnect(dispatch_uid='test-template-rendered')
     1405
     1406        template.Template.send_rendered_signal = self._old_send
     1407
     1408    def test_template_rendered_signal_on(self):
     1409        "If Template.send_rendered_signal is True, template-rendering sends a template_rendered signal."
     1410        template.Template.send_rendered_signal = True
     1411
     1412        t = template.Template("{{ stuff }}")
     1413        c = template.Context({'stuff': 'something'})
     1414        t.render(c)
     1415
     1416        self.assertEqual(self.received, [(t, c)])
     1417
     1418    def test_template_rendered_signal_off(self):
     1419        "If Template.send_rendered_signal is False, template-rendering does not send template_rendered signal."
     1420        template.Template.send_rendered_signal = False
     1421
     1422        t = template.Template("{{ stuff }}")
     1423        c = template.Context({'stuff': 'something'})
     1424        t.render(c)
     1425
     1426        self.assertEqual(self.received, [])
     1427
     1428
    13921429if __name__ == "__main__":
    13931430    unittest.main()
Back to Top