Django

Code

Changeset 3707

Show
Ignore:
Timestamp:
09/02/06 04:26:24 (2 years ago)
Author:
russellm
Message:

Refs #2333 - Re-added the template rendering signal for testing purposes; however, the signal is not available during normal operation. It is only added as part of an instrumentation step that occurs during test framework setup. Previous attempt (r3659) was reverted (r3666) due to performance concerns.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaulttags.py

    r3666 r3707  
    252252        if self.parsed: 
    253253            try: 
    254                 t = Template(output
     254                t = Template(output, name=self.filepath
    255255                return t.render(context) 
    256256            except TemplateSyntaxError, e: 
  • django/trunk/django/template/__init__.py

    r3680 r3707  
    6161from django.utils.functional import curry 
    6262from django.utils.text import smart_split 
     63from django.dispatch import dispatcher 
     64from django.template import signals 
    6365 
    6466__all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 
     
    138140 
    139141class Template(object): 
    140     def __init__(self, template_string, origin=None): 
     142    def __init__(self, template_string, origin=None, name='<Unknown Template>'): 
    141143        "Compilation stage" 
    142144        if settings.TEMPLATE_DEBUG and origin == None: 
     
    145147            # came from... 
    146148        self.nodelist = compile_string(template_string, origin) 
     149        self.name = name 
    147150 
    148151    def __iter__(self): 
     
    153156    def render(self, context): 
    154157        "Display stage -- can be called many times" 
     158        dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context) 
    155159        return self.nodelist.render(context) 
    156160 
  • django/trunk/django/template/loader.py

    r3666 r3707  
    7777    handling template inheritance recursively. 
    7878    """ 
    79     return get_template_from_string(*find_template_source(template_name)) 
     79    source, origin = find_template_source(template_name) 
     80    template = get_template_from_string(source, origin, template_name) 
     81    return template 
    8082 
    81 def get_template_from_string(source, origin=None): 
     83def get_template_from_string(source, origin=None, name=None): 
    8284    """ 
    8385    Returns a compiled Template object for the given template code, 
    8486    handling template inheritance recursively. 
    8587    """ 
    86     return Template(source, origin
     88    return Template(source, origin, name
    8789 
    8890def render_to_string(template_name, dictionary=None, context_instance=None): 
  • django/trunk/django/template/loader_tags.py

    r3666 r3707  
    5858            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent 
    5959        else: 
    60             return get_template_from_string(source, origin
     60            return get_template_from_string(source, origin, parent
    6161 
    6262    def render(self, context): 
  • django/trunk/django/test/client.py

    r3658 r3707  
    11from cStringIO import StringIO 
    2 from django.contrib.admin.views.decorators import LOGIN_FORM_KEY, _encode_post_data 
    32from django.core.handlers.base import BaseHandler 
    43from django.core.handlers.wsgi import WSGIRequest 
    54from django.dispatch import dispatcher 
    65from django.http import urlencode, SimpleCookie 
    7 from django.template import signals 
     6from django.test import signals 
    87from django.utils.functional import curry 
    98 
     
    9796    """ 
    9897    def __init__(self, **defaults): 
    99         self.handler = TestHandler() 
     98        self.handler = ClientHandler() 
    10099        self.defaults = defaults 
    101100        self.cookie = SimpleCookie() 
     
    127126        on_template_render = curry(store_rendered_templates, data) 
    128127        dispatcher.connect(on_template_render, signal=signals.template_rendered) 
    129  
     128         
    130129        response = self.handler(environ) 
    131130         
     
    181180        """ 
    182181        A specialized sequence of GET and POST to log into a view that 
    183         is protected by @login_required or a similar access decorator. 
    184          
    185         path should be the URL of the login page, or of any page that 
    186         is login protected. 
    187          
    188         Returns True if login was successful; False if otherwise.         
    189         """ 
    190         # First, GET the login page.  
    191         # This is required to establish the session
     182        is protected by a @login_required access decorator. 
     183         
     184        path should be the URL of the page that is login protected. 
     185         
     186        Returns the response from GETting the requested URL after  
     187        login is complete. Returns False if login process failed. 
     188        """ 
     189        # First, GET the page that is login protected.  
     190        # This page will redirect to the login page
    192191        response = self.get(path) 
     192        if response.status_code != 302: 
     193            return False 
     194             
     195        login_path, data = response['Location'].split('?') 
     196        next = data.split('=')[1] 
     197 
     198        # Second, GET the login page; required to set up cookies 
     199        response = self.get(login_path, **extra) 
    193200        if response.status_code != 200: 
    194201            return False 
    195  
    196         # Set up the block of form data required by the login page
     202             
     203        # Last, POST the login data
    197204        form_data = { 
    198205            'username': username, 
    199206            'password': password, 
    200             'this_is_the_login_form': 1, 
    201             'post_data': _encode_post_data({LOGIN_FORM_KEY: 1}) 
     207            'next' : next, 
    202208        } 
    203         response = self.post(path, data=form_data, **extra) 
    204          
    205         # login page should give response 200 (if you requested the login 
    206         # page specifically), or 302 (if you requested a login 
    207         # protected page, to which the login can redirect). 
    208         return response.status_code in (200,302) 
     209        response = self.post(login_path, data=form_data, **extra) 
     210 
     211        # Login page should 302 redirect to the originally requested page 
     212        if response.status_code != 302 or response['Location'] != path: 
     213            return False 
     214 
     215        # Since we are logged in, request the actual page again 
     216        return self.get(path) 
  • django/trunk/django/test/simple.py

    r3689 r3707  
    22from django.conf import settings 
    33from django.core import management 
     4from django.test.utils import setup_test_environment, teardown_test_environment 
    45from django.test.utils import create_test_db, destroy_test_db 
    56from django.test.testcases import OutputChecker, DocTestRunner 
     
    5253    will be added to the test suite. 
    5354    """ 
     55    setup_test_environment() 
    5456     
    5557    settings.DEBUG = False     
     
    6769    unittest.TextTestRunner(verbosity=verbosity).run(suite) 
    6870    destroy_test_db(old_name, verbosity) 
     71     
     72    teardown_test_environment() 
  • django/trunk/django/test/utils.py

    r3706 r3707  
    22from django.conf import settings 
    33from django.db import connection, transaction, backend 
     4from django.dispatch import dispatcher 
     5from django.test import signals 
     6from django.template import Template 
    47 
    58# The prefix to put on the default database name when creating 
     
    710TEST_DATABASE_PREFIX = 'test_' 
    811 
     12def instrumented_test_render(self, context): 
     13    """An instrumented Template render method, providing a signal  
     14    that can be intercepted by the test system Client 
     15     
     16    """ 
     17    dispatcher.send(signal=signals.template_rendered, sender=self, template=self, context=context) 
     18    return self.nodelist.render(context) 
     19     
     20def setup_test_environment(): 
     21    """Perform any global pre-test setup. This involves: 
     22         
     23        - Installing the instrumented test renderer 
     24         
     25    """ 
     26    Template.original_render = Template.render 
     27    Template.render = instrumented_test_render 
     28     
     29def teardown_test_environment(): 
     30    """Perform any global post-test teardown. This involves: 
     31 
     32        - Restoring the original test renderer 
     33         
     34    """ 
     35    Template.render = Template.original_render 
     36    del Template.original_render 
     37     
    938def _set_autocommit(connection): 
    1039    "Make sure a connection is in autocommit mode." 
     
    76105        cursor.execute("DROP DATABASE %s" % backend.quote_name(TEST_DATABASE_NAME)) 
    77106        connection.close() 
    78  
  • django/trunk/django/views/debug.py

    r3666 r3707  
    116116            'lineno': '?', 
    117117        }] 
    118     t = Template(TECHNICAL_500_TEMPLATE
     118    t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 Template'
    119119    c = Context({ 
    120120        'exception_type': exc_type.__name__, 
     
    142142            return empty_urlconf(request) 
    143143 
    144     t = Template(TECHNICAL_404_TEMPLATE
     144    t = Template(TECHNICAL_404_TEMPLATE, name='Technical 404 Template'
    145145    c = Context({ 
    146146        'root_urlconf': settings.ROOT_URLCONF, 
     
    155155def empty_urlconf(request): 
    156156    "Create an empty URLconf 404 error response." 
    157     t = Template(EMPTY_URLCONF_TEMPLATE
     157    t = Template(EMPTY_URLCONF_TEMPLATE, name='Empty URLConf Template'
    158158    c = Context({ 
    159159        'project_name': settings.SETTINGS_MODULE.split('.')[0] 
  • django/trunk/django/views/static.py

    r3666 r3707  
    8282        t = loader.get_template('static/directory_index') 
    8383    except TemplateDoesNotExist: 
    84         t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE
     84        t = Template(DEFAULT_DIRECTORY_INDEX_TEMPLATE, name='Default Directory Index Template'
    8585    files = [] 
    8686    for f in os.listdir(fullpath):