From c52ae07453a5556ba255aa115f2a4e8f56c7fd73 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Mon, 13 Jul 2009 15:19:05 +0000 Subject: [soc2009/i18n] Date filter allowed to use predefined formats, improved fallbacks on getting formats, and documentation added. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/i18n-improvements@11231 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaultfilters.py | 16 +++++++++++----- django/utils/formats.py | 25 +++++++++++++++---------- django/views/i18n.py | 18 +++++++++++++----- 3 files changed, 39 insertions(+), 20 deletions(-) (limited to 'django') diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 94108f2d23..d31783949a 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -18,7 +18,7 @@ from django.conf import settings from django.utils.translation import ugettext, ungettext from django.utils.encoding import force_unicode, iri_to_uri from django.utils.safestring import mark_safe, SafeData -from django.utils.formats import number_format +from django.utils.formats import date_format, number_format register = Library() @@ -685,9 +685,12 @@ def date(value, arg=None): if arg is None: arg = settings.DATE_FORMAT try: - return format(value, arg) + return date_format(value, arg) except AttributeError: - return '' + try: + return format(value, arg) + except AttributeError: + return '' date.is_safe = False def time(value, arg=None): @@ -698,9 +701,12 @@ def time(value, arg=None): if arg is None: arg = settings.TIME_FORMAT try: - return time_format(value, arg) + return date_format(value, arg) except AttributeError: - return '' + try: + return time_format(value, arg) + except AttributeError: + return '' time.is_safe = False def timesince(value, arg=None): diff --git a/django/utils/formats.py b/django/utils/formats.py index 8aece8a239..cf6f2a1fc2 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -15,35 +15,40 @@ def getformat_null(format_type): """ return getattr(settings, format_type) -def formats_module(): +def project_formats_module(): """ - Returns the formats module for the current locale. It can be the - custom one from the user, or Django's default. + Returns the formats module for the current locale, defined + on the project """ if settings.FORMAT_MODULE_PATH: try: return import_module('.formats', '%s.%s' % (settings.FORMAT_MODULE_PATH, get_language())) except ImportError: pass + return None +def django_formats_module(): + """ + Returns the formats module for the current locale, defined + on Django + """ try: return import_module('.formats', 'django.conf.locale.%s' % get_language()) except ImportError: return None - def getformat_real(format_type): """ For a specific format type, returns the format for the current language (locale) defaulting to the format on settings. format_type is the name of the format, for example 'DATE_FORMAT' """ - module = formats_module() - if module: - try: - return getattr(module, format_type) - except AttributeError: - pass + for module in (project_formats_module(), django_formats_module()): + if module: + try: + return getattr(module, format_type) + except AttributeError: + pass return getformat_null(format_type) # getformat will just return the value on setings if diff --git a/django/views/i18n.py b/django/views/i18n.py index eaa4723f94..e70cd3eca3 100644 --- a/django/views/i18n.py +++ b/django/views/i18n.py @@ -3,7 +3,7 @@ from django.conf import settings from django.utils import importlib from django.utils.translation import check_for_language, activate, to_locale, get_language from django.utils.text import javascript_quote -from django.utils.formats import formats_module +from django.utils.formats import project_formats_module, django_formats_module import os import gettext as gettext_module @@ -37,11 +37,19 @@ def get_formats(): """ Returns an iterator over all formats in formats file """ - module = formats_module() + FORMAT_SETTINGS = ('DATE_FORMAT', 'DATETIME_FORMAT', 'TIME_FORMAT', + 'YEAR_MONTH_FORMAT', 'MONTH_DAY_FORMAT', 'SHORT_DATE_FORMAT', + 'SHORT_DATETIME_FORMAT', 'FIRST_DAY_OF_WEEK', 'DECIMAL_SEPARATOR', + 'THOUSAND_SEPARATOR', 'NUMBER_GROUPING') + result = {} - for attr in dir(module): - if attr[:2] != '__': - result[attr] = getattr(module, attr) + for module in (settings, django_formats_module(), project_formats_module()): + if module: + for attr in FORMAT_SETTINGS: + try: + result[attr] = getattr(module, attr) + except AttributeError: + pass return result NullSource = """ -- cgit v1.3