mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
Added support for time zones. Thanks Luke Plant for the review. Fixed #2626.
For more information on this project, see this thread: http://groups.google.com/group/django-developers/browse_thread/thread/cf0423bbb85b1bbf git-svn-id: http://code.djangoproject.com/svn/django/trunk@17106 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -18,6 +18,7 @@ from django.utils.safestring import (SafeData, EscapeData, mark_safe,
|
||||
from django.utils.formats import localize
|
||||
from django.utils.html import escape
|
||||
from django.utils.module_loading import module_has_submodule
|
||||
from django.utils.timezone import aslocaltime
|
||||
|
||||
|
||||
TOKEN_TEXT = 0
|
||||
@@ -593,6 +594,8 @@ class FilterExpression(object):
|
||||
arg_vals.append(mark_safe(arg))
|
||||
else:
|
||||
arg_vals.append(arg.resolve(context))
|
||||
if getattr(func, 'expects_localtime', False):
|
||||
obj = aslocaltime(obj, context.use_tz)
|
||||
if getattr(func, 'needs_autoescape', False):
|
||||
new_obj = func(obj, autoescape=context.autoescape, *arg_vals)
|
||||
else:
|
||||
@@ -853,6 +856,7 @@ def _render_value_in_context(value, context):
|
||||
means escaping, if required, and conversion to a unicode object. If value
|
||||
is a string, it is expected to have already been translated.
|
||||
"""
|
||||
value = aslocaltime(value, use_tz=context.use_tz)
|
||||
value = localize(value, use_l10n=context.use_l10n)
|
||||
value = force_unicode(value)
|
||||
if ((context.autoescape and not isinstance(value, SafeData)) or
|
||||
@@ -1077,7 +1081,7 @@ class Library(object):
|
||||
elif name is not None and filter_func is not None:
|
||||
# register.filter('somename', somefunc)
|
||||
self.filters[name] = filter_func
|
||||
for attr in ('is_safe', 'needs_autoescape'):
|
||||
for attr in ('expects_localtime', 'is_safe', 'needs_autoescape'):
|
||||
if attr in flags:
|
||||
value = flags[attr]
|
||||
# set the flag on the filter for FilterExpression.resolve
|
||||
@@ -1189,6 +1193,7 @@ class Library(object):
|
||||
'autoescape': context.autoescape,
|
||||
'current_app': context.current_app,
|
||||
'use_l10n': context.use_l10n,
|
||||
'use_tz': context.use_tz,
|
||||
})
|
||||
# Copy across the CSRF token, if present, because
|
||||
# inclusion tags are often used for forms, and we need
|
||||
|
||||
@@ -83,10 +83,12 @@ class BaseContext(object):
|
||||
|
||||
class Context(BaseContext):
|
||||
"A stack container for variable context"
|
||||
def __init__(self, dict_=None, autoescape=True, current_app=None, use_l10n=None):
|
||||
def __init__(self, dict_=None, autoescape=True, current_app=None,
|
||||
use_l10n=None, use_tz=None):
|
||||
self.autoescape = autoescape
|
||||
self.use_l10n = use_l10n
|
||||
self.current_app = current_app
|
||||
self.use_l10n = use_l10n
|
||||
self.use_tz = use_tz
|
||||
self.render_context = RenderContext()
|
||||
super(Context, self).__init__(dict_)
|
||||
|
||||
@@ -162,8 +164,10 @@ class RequestContext(Context):
|
||||
Additional processors can be specified as a list of callables
|
||||
using the "processors" keyword argument.
|
||||
"""
|
||||
def __init__(self, request, dict=None, processors=None, current_app=None, use_l10n=None):
|
||||
Context.__init__(self, dict, current_app=current_app, use_l10n=use_l10n)
|
||||
def __init__(self, request, dict_=None, processors=None, current_app=None,
|
||||
use_l10n=None, use_tz=None):
|
||||
Context.__init__(self, dict_, current_app=current_app,
|
||||
use_l10n=use_l10n, use_tz=use_tz)
|
||||
if processors is None:
|
||||
processors = ()
|
||||
else:
|
||||
|
||||
@@ -3,6 +3,7 @@ from django.utils.encoding import force_unicode
|
||||
from django.utils.html import escape
|
||||
from django.utils.safestring import SafeData, EscapeData
|
||||
from django.utils.formats import localize
|
||||
from django.utils.timezone import aslocaltime
|
||||
|
||||
|
||||
class DebugLexer(Lexer):
|
||||
@@ -81,6 +82,7 @@ class DebugVariableNode(VariableNode):
|
||||
def render(self, context):
|
||||
try:
|
||||
output = self.filter_expression.resolve(context)
|
||||
output = aslocaltime(output, use_tz=context.use_tz)
|
||||
output = localize(output, use_l10n=context.use_l10n)
|
||||
output = force_unicode(output)
|
||||
except UnicodeDecodeError:
|
||||
|
||||
@@ -692,7 +692,7 @@ def get_digit(value, arg):
|
||||
# DATES #
|
||||
###################
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
@register.filter(expects_localtime=True, is_safe=False)
|
||||
def date(value, arg=None):
|
||||
"""Formats a date according to the given format."""
|
||||
if not value:
|
||||
@@ -707,7 +707,7 @@ def date(value, arg=None):
|
||||
except AttributeError:
|
||||
return ''
|
||||
|
||||
@register.filter(is_safe=False)
|
||||
@register.filter(expects_localtime=True, is_safe=False)
|
||||
def time(value, arg=None):
|
||||
"""Formats a time according to the given format."""
|
||||
if value in (None, u''):
|
||||
|
||||
Reference in New Issue
Block a user