mirror of
				https://github.com/django/django.git
				synced 2025-10-30 09:06:13 +00:00 
			
		
		
		
	Encapsulated TEMPLATE_STRING_IF_INVALID in Engine.
This commit is contained in:
		| @@ -71,10 +71,6 @@ libraries = {} | |||||||
| # global list of libraries to load by default for a new parser | # global list of libraries to load by default for a new parser | ||||||
| builtins = [] | builtins = [] | ||||||
|  |  | ||||||
| # True if TEMPLATE_STRING_IF_INVALID contains a format string (%s). None means |  | ||||||
| # uninitialized. |  | ||||||
| invalid_var_format_string = None |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class TemplateSyntaxError(Exception): | class TemplateSyntaxError(Exception): | ||||||
|     pass |     pass | ||||||
| @@ -601,15 +597,14 @@ class FilterExpression(object): | |||||||
|                 if ignore_failures: |                 if ignore_failures: | ||||||
|                     obj = None |                     obj = None | ||||||
|                 else: |                 else: | ||||||
|                     if settings.TEMPLATE_STRING_IF_INVALID: |                     string_if_invalid = context.engine.string_if_invalid | ||||||
|                         global invalid_var_format_string |                     if string_if_invalid: | ||||||
|                         if invalid_var_format_string is None: |                         if '%s' in string_if_invalid: | ||||||
|                             invalid_var_format_string = '%s' in settings.TEMPLATE_STRING_IF_INVALID |                             return string_if_invalid % self.var | ||||||
|                         if invalid_var_format_string: |                         else: | ||||||
|                             return settings.TEMPLATE_STRING_IF_INVALID % self.var |                             return string_if_invalid | ||||||
|                         return settings.TEMPLATE_STRING_IF_INVALID |  | ||||||
|                     else: |                     else: | ||||||
|                         obj = settings.TEMPLATE_STRING_IF_INVALID |                         obj = string_if_invalid | ||||||
|         else: |         else: | ||||||
|             obj = self.var |             obj = self.var | ||||||
|         for func, args in self.filters: |         for func, args in self.filters: | ||||||
| @@ -794,7 +789,7 @@ class Variable(object): | |||||||
|                     if getattr(current, 'do_not_call_in_templates', False): |                     if getattr(current, 'do_not_call_in_templates', False): | ||||||
|                         pass |                         pass | ||||||
|                     elif getattr(current, 'alters_data', False): |                     elif getattr(current, 'alters_data', False): | ||||||
|                         current = settings.TEMPLATE_STRING_IF_INVALID |                         current = context.engine.string_if_invalid | ||||||
|                     else: |                     else: | ||||||
|                         try:  # method call (assuming no args required) |                         try:  # method call (assuming no args required) | ||||||
|                             current = current() |                             current = current() | ||||||
| @@ -802,12 +797,12 @@ class Variable(object): | |||||||
|                             try: |                             try: | ||||||
|                                 getcallargs(current) |                                 getcallargs(current) | ||||||
|                             except TypeError:  # arguments *were* required |                             except TypeError:  # arguments *were* required | ||||||
|                                 current = settings.TEMPLATE_STRING_IF_INVALID  # invalid method call |                                 current = context.engine.string_if_invalid  # invalid method call | ||||||
|                             else: |                             else: | ||||||
|                                 raise |                                 raise | ||||||
|         except Exception as e: |         except Exception as e: | ||||||
|             if getattr(e, 'silent_variable_failure', False): |             if getattr(e, 'silent_variable_failure', False): | ||||||
|                 current = settings.TEMPLATE_STRING_IF_INVALID |                 current = context.engine.string_if_invalid | ||||||
|             else: |             else: | ||||||
|                 raise |                 raise | ||||||
|  |  | ||||||
|   | |||||||
| @@ -149,7 +149,7 @@ class BlockTranslateNode(Node): | |||||||
|                 result = translation.pgettext(message_context, singular) |                 result = translation.pgettext(message_context, singular) | ||||||
|             else: |             else: | ||||||
|                 result = translation.ugettext(singular) |                 result = translation.ugettext(singular) | ||||||
|         default_value = settings.TEMPLATE_STRING_IF_INVALID |         default_value = context.engine.string_if_invalid | ||||||
|  |  | ||||||
|         def render_value(key): |         def render_value(key): | ||||||
|             if key in context: |             if key in context: | ||||||
|   | |||||||
| @@ -12,8 +12,7 @@ from django import template | |||||||
| from django.conf import settings | from django.conf import settings | ||||||
| from django.contrib.auth.models import Group | from django.contrib.auth.models import Group | ||||||
| from django.core import urlresolvers | from django.core import urlresolvers | ||||||
| from django.template import (base as template_base, loader, Context, | from django.template import loader, Context, RequestContext, Template, TemplateSyntaxError | ||||||
|     RequestContext, Template, TemplateSyntaxError) |  | ||||||
| from django.template.engine import Engine | from django.template.engine import Engine | ||||||
| from django.template.loaders import app_directories, filesystem | from django.template.loaders import app_directories, filesystem | ||||||
| from django.test import RequestFactory, TestCase | from django.test import RequestFactory, TestCase | ||||||
| @@ -550,14 +549,15 @@ class TemplateTests(TestCase): | |||||||
|             failures = [] |             failures = [] | ||||||
|             tests = sorted(template_tests.items()) |             tests = sorted(template_tests.items()) | ||||||
|  |  | ||||||
|             # Set TEMPLATE_STRING_IF_INVALID to a known string. |  | ||||||
|             expected_invalid_str = 'INVALID' |  | ||||||
|  |  | ||||||
|             # Warm the URL reversing cache. This ensures we don't pay the cost |             # Warm the URL reversing cache. This ensures we don't pay the cost | ||||||
|             # warming the cache during one of the tests. |             # warming the cache during one of the tests. | ||||||
|             urlresolvers.reverse('named.client', args=(0,)) |             urlresolvers.reverse('named.client', args=(0,)) | ||||||
|  |  | ||||||
|             for name, vals in tests: |             for name, vals in tests: | ||||||
|  |  | ||||||
|  |                 # Set TEMPLATE_STRING_IF_INVALID to a known string. | ||||||
|  |                 expected_invalid_str = 'INVALID' | ||||||
|  |  | ||||||
|                 if isinstance(vals[2], tuple): |                 if isinstance(vals[2], tuple): | ||||||
|                     normal_string_result = vals[2][0] |                     normal_string_result = vals[2][0] | ||||||
|                     invalid_string_result = vals[2][1] |                     invalid_string_result = vals[2][1] | ||||||
| @@ -565,7 +565,6 @@ class TemplateTests(TestCase): | |||||||
|                     if isinstance(invalid_string_result, tuple): |                     if isinstance(invalid_string_result, tuple): | ||||||
|                         expected_invalid_str = 'INVALID %s' |                         expected_invalid_str = 'INVALID %s' | ||||||
|                         invalid_string_result = invalid_string_result[0] % invalid_string_result[1] |                         invalid_string_result = invalid_string_result[0] % invalid_string_result[1] | ||||||
|                         template_base.invalid_var_format_string = True |  | ||||||
|  |  | ||||||
|                     try: |                     try: | ||||||
|                         template_debug_result = vals[2][2] |                         template_debug_result = vals[2][2] | ||||||
| @@ -622,10 +621,6 @@ class TemplateTests(TestCase): | |||||||
|  |  | ||||||
|                     Engine.get_default().template_loaders[0].reset() |                     Engine.get_default().template_loaders[0].reset() | ||||||
|  |  | ||||||
|                 if template_base.invalid_var_format_string: |  | ||||||
|                     expected_invalid_str = 'INVALID' |  | ||||||
|                     template_base.invalid_var_format_string = False |  | ||||||
|  |  | ||||||
|         self.assertEqual(failures, [], "Tests failed:\n%s\n%s" % |         self.assertEqual(failures, [], "Tests failed:\n%s\n%s" % | ||||||
|             ('-' * 70, ("\n%s\n" % ('-' * 70)).join(failures))) |             ('-' * 70, ("\n%s\n" % ('-' * 70)).join(failures))) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user