Author: jezdez
Date: 2011-05-07 09:58:45 -0700 (Sat, 07 May 2011)
New Revision: 16172

Modified:
   django/trunk/django/template/defaulttags.py
   django/trunk/docs/ref/templates/builtins.txt
   django/trunk/tests/regressiontests/i18n/tests.py
   django/trunk/tests/regressiontests/templates/tests.py
Log:
Fixed #15263 -- Added support for format localization to the now template tag. 
Thanks to danielr and dmclain.

Modified: django/trunk/django/template/defaulttags.py
===================================================================
--- django/trunk/django/template/defaulttags.py 2011-05-07 16:58:35 UTC (rev 
16171)
+++ django/trunk/django/template/defaulttags.py 2011-05-07 16:58:45 UTC (rev 
16172)
@@ -2,12 +2,14 @@
 
 import sys
 import re
+from datetime import datetime
 from itertools import groupby, cycle as itertools_cycle
 
 from django.template.base import Node, NodeList, Template, Context, Variable
 from django.template.base import TemplateSyntaxError, VariableDoesNotExist, 
BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, 
SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
 from django.template.base import get_library, Library, InvalidTemplateLibrary
 from django.template.smartif import IfParser, Literal
+from django.template.defaultfilters import date
 from django.conf import settings
 from django.utils.encoding import smart_str, smart_unicode
 from django.utils.safestring import mark_safe
@@ -380,10 +382,7 @@
         self.format_string = format_string
 
     def render(self, context):
-        from datetime import datetime
-        from django.utils.dateformat import DateFormat
-        df = DateFormat(datetime.now())
-        return df.format(self.format_string)
+        return date(datetime.now(), self.format_string)
 
 class SpacelessNode(Node):
     def __init__(self, nodelist):

Modified: django/trunk/docs/ref/templates/builtins.txt
===================================================================
--- django/trunk/docs/ref/templates/builtins.txt        2011-05-07 16:58:35 UTC 
(rev 16171)
+++ django/trunk/docs/ref/templates/builtins.txt        2011-05-07 16:58:45 UTC 
(rev 16172)
@@ -727,6 +727,18 @@
 
 This would display as "It is the 4th of September".
 
+.. versionchanged:: 1.4
+
+.. note::
+
+    The format passed can also be one of the predefined ones
+    :setting:`DATE_FORMAT`, :setting:`DATETIME_FORMAT`,
+    :setting:`SHORT_DATE_FORMAT` or :setting:`SHORT_DATETIME_FORMAT`.
+    The predefined formats may vary depending on the current locale and
+    if :ref:`format-localization` is enabled, e.g.::
+
+        It is {% now "SHORT_DATETIME_FORMAT" %}
+
 .. templatetag:: regroup
 
 regroup

Modified: django/trunk/tests/regressiontests/i18n/tests.py
===================================================================
--- django/trunk/tests/regressiontests/i18n/tests.py    2011-05-07 16:58:35 UTC 
(rev 16171)
+++ django/trunk/tests/regressiontests/i18n/tests.py    2011-05-07 16:58:45 UTC 
(rev 16172)
@@ -305,6 +305,7 @@
             self.assertEqual(u'10:15:48', Template('{{ t|time:"TIME_FORMAT" 
}}').render(self.ctxt))
             self.assertEqual(u'31/12/2009', Template('{{ 
d|date:"SHORT_DATE_FORMAT" }}').render(self.ctxt))
             self.assertEqual(u'31/12/2009 20:50', Template('{{ 
dt|date:"SHORT_DATETIME_FORMAT" }}').render(self.ctxt))
+            self.assertEqual(date_format(datetime.datetime.now(), 
"DATE_FORMAT"), Template('{% now "DATE_FORMAT" %}').render(self.ctxt))
 
             form4 = I18nForm({
                 'decimal_field': u'66666,666',

Modified: django/trunk/tests/regressiontests/templates/tests.py
===================================================================
--- django/trunk/tests/regressiontests/templates/tests.py       2011-05-07 
16:58:35 UTC (rev 16171)
+++ django/trunk/tests/regressiontests/templates/tests.py       2011-05-07 
16:58:45 UTC (rev 16172)
@@ -21,6 +21,7 @@
 from django.test.utils import get_warnings_state, restore_warnings_state,\
     setup_test_template_loader, restore_template_loaders
 from django.utils import unittest
+from django.utils.formats import date_format
 from django.utils.translation import activate, deactivate, ugettext as _
 from django.utils.safestring import mark_safe
 from django.utils.tzinfo import LocalTimezone
@@ -1422,6 +1423,8 @@
             'now02': ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError),
         #    'now03': ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + 
'"' + str(datetime.now().month) + '"' + str(datetime.now().year)),
         #    'now04': ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + 
'\n' + str(datetime.now().month) + '\n' + str(datetime.now().year))
+            # Check parsing of locale strings
+            'now05': ('{% now "DATE_FORMAT" %}', {},  
date_format(datetime.now())),
 
             ### URL TAG 
########################################################
             # Successes

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to