2009-12-23 01:58:49 +08:00
|
|
|
import decimal
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from django.conf import settings
|
2011-05-02 00:14:57 +08:00
|
|
|
from django.utils import dateformat, numberformat, datetime_safe
|
2009-12-23 01:58:49 +08:00
|
|
|
from django.utils.importlib import import_module
|
2009-12-31 06:11:48 +08:00
|
|
|
from django.utils.encoding import smart_str
|
2011-05-02 00:14:57 +08:00
|
|
|
from django.utils.functional import lazy
|
2010-12-04 15:44:00 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2011-05-02 00:14:57 +08:00
|
|
|
from django.utils.translation import get_language, to_locale, check_for_language
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2010-09-27 23:25:08 +08:00
|
|
|
# format_cache is a mapping from (format_type, lang) to the format string.
|
|
|
|
# By using the cache, it is possible to avoid running get_format_modules
|
|
|
|
# repeatedly.
|
|
|
|
_format_cache = {}
|
|
|
|
_format_modules_cache = {}
|
|
|
|
|
2011-02-04 21:52:36 +08:00
|
|
|
def reset_format_cache():
|
|
|
|
"""Clear any cached formats.
|
|
|
|
|
|
|
|
This method is provided primarily for testing purposes,
|
|
|
|
so that the effects of cached formats can be removed.
|
|
|
|
"""
|
|
|
|
global _format_cache, _format_modules_cache
|
|
|
|
_format_cache = {}
|
|
|
|
_format_modules_cache = {}
|
|
|
|
|
2010-09-27 23:25:08 +08:00
|
|
|
def iter_format_modules(lang):
|
|
|
|
"""
|
|
|
|
Does the heavy lifting of finding format modules.
|
|
|
|
"""
|
2011-02-04 21:52:36 +08:00
|
|
|
if check_for_language(lang):
|
2010-09-27 23:25:08 +08:00
|
|
|
format_locations = ['django.conf.locale.%s']
|
|
|
|
if settings.FORMAT_MODULE_PATH:
|
|
|
|
format_locations.append(settings.FORMAT_MODULE_PATH + '.%s')
|
|
|
|
format_locations.reverse()
|
|
|
|
locale = to_locale(lang)
|
2011-01-13 11:02:32 +08:00
|
|
|
locales = [locale]
|
|
|
|
if '_' in locale:
|
|
|
|
locales.append(locale.split('_')[0])
|
2010-09-27 23:25:08 +08:00
|
|
|
for location in format_locations:
|
|
|
|
for loc in locales:
|
|
|
|
try:
|
|
|
|
yield import_module('.formats', location % loc)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2009-12-31 06:12:16 +08:00
|
|
|
def get_format_modules(reverse=False):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2011-02-03 23:43:50 +08:00
|
|
|
Returns a list of the format modules found
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-09-27 23:25:08 +08:00
|
|
|
lang = get_language()
|
|
|
|
modules = _format_modules_cache.setdefault(lang, list(iter_format_modules(lang)))
|
2009-12-31 06:12:16 +08:00
|
|
|
if reverse:
|
2011-02-03 23:43:50 +08:00
|
|
|
return list(reversed(modules))
|
2009-12-23 01:58:49 +08:00
|
|
|
return modules
|
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def get_format(format_type, lang=None, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
|
|
|
For a specific format type, returns the format for the current
|
|
|
|
language (locale), defaults to the format in the settings.
|
|
|
|
format_type is the name of the format, e.g. 'DATE_FORMAT'
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
If use_l10n is provided and is not None, that will force the value to
|
|
|
|
be localized (or not), overriding the value of settings.USE_L10N.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-01-02 05:38:58 +08:00
|
|
|
format_type = smart_str(format_type)
|
2010-10-30 00:48:58 +08:00
|
|
|
if use_l10n or (use_l10n is None and settings.USE_L10N):
|
2010-09-27 23:25:08 +08:00
|
|
|
if lang is None:
|
|
|
|
lang = get_language()
|
|
|
|
cache_key = (format_type, lang)
|
|
|
|
try:
|
|
|
|
return _format_cache[cache_key] or getattr(settings, format_type)
|
|
|
|
except KeyError:
|
|
|
|
for module in get_format_modules():
|
|
|
|
try:
|
|
|
|
val = getattr(module, format_type)
|
|
|
|
_format_cache[cache_key] = val
|
|
|
|
return val
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
_format_cache[cache_key] = None
|
2009-12-23 01:58:49 +08:00
|
|
|
return getattr(settings, format_type)
|
|
|
|
|
2011-05-02 00:14:57 +08:00
|
|
|
get_format_lazy = lazy(get_format, unicode, list, tuple)
|
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def date_format(value, format=None, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
|
|
|
Formats a datetime.date or datetime.datetime object using a
|
|
|
|
localizable format
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
If use_l10n is provided and is not None, that will force the value to
|
|
|
|
be localized (or not), overriding the value of settings.USE_L10N.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-10-30 00:48:58 +08:00
|
|
|
return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def time_format(value, format=None, use_l10n=None):
|
2010-01-02 05:36:36 +08:00
|
|
|
"""
|
|
|
|
Formats a datetime.time object using a localizable format
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
If use_l10n is provided and is not None, that will force the value to
|
|
|
|
be localized (or not), overriding the value of settings.USE_L10N.
|
2010-01-02 05:36:36 +08:00
|
|
|
"""
|
2010-10-30 00:48:58 +08:00
|
|
|
return dateformat.time_format(value, get_format(format or 'TIME_FORMAT', use_l10n=use_l10n))
|
2010-01-02 05:36:36 +08:00
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def number_format(value, decimal_pos=None, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
|
|
|
Formats a numeric value using localization settings
|
2010-10-30 00:48:58 +08:00
|
|
|
|
|
|
|
If use_l10n is provided and is not None, that will force the value to
|
|
|
|
be localized (or not), overriding the value of settings.USE_L10N.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-10-30 00:48:58 +08:00
|
|
|
if use_l10n or (use_l10n is None and settings.USE_L10N):
|
2010-09-27 23:25:08 +08:00
|
|
|
lang = get_language()
|
|
|
|
else:
|
|
|
|
lang = None
|
2009-12-23 01:58:49 +08:00
|
|
|
return numberformat.format(
|
|
|
|
value,
|
2010-10-30 00:48:58 +08:00
|
|
|
get_format('DECIMAL_SEPARATOR', lang, use_l10n=use_l10n),
|
2009-12-23 01:58:49 +08:00
|
|
|
decimal_pos,
|
2010-10-30 00:48:58 +08:00
|
|
|
get_format('NUMBER_GROUPING', lang, use_l10n=use_l10n),
|
|
|
|
get_format('THOUSAND_SEPARATOR', lang, use_l10n=use_l10n),
|
2009-12-23 01:58:49 +08:00
|
|
|
)
|
|
|
|
|
2010-10-30 00:48:58 +08:00
|
|
|
def localize(value, use_l10n=None):
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2009-12-31 06:11:48 +08:00
|
|
|
Checks if value is a localizable type (date, number...) and returns it
|
2010-10-30 00:48:58 +08:00
|
|
|
formatted as a string using current locale format.
|
|
|
|
|
|
|
|
If use_l10n is provided and is not None, that will force the value to
|
|
|
|
be localized (or not), overriding the value of settings.USE_L10N.
|
2009-12-23 01:58:49 +08:00
|
|
|
"""
|
2010-12-04 15:44:00 +08:00
|
|
|
if isinstance(value, bool):
|
|
|
|
return mark_safe(unicode(value))
|
|
|
|
elif isinstance(value, (decimal.Decimal, float, int, long)):
|
2010-10-30 00:48:58 +08:00
|
|
|
return number_format(value, use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
elif isinstance(value, datetime.datetime):
|
2010-10-30 00:48:58 +08:00
|
|
|
return date_format(value, 'DATETIME_FORMAT', use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
elif isinstance(value, datetime.date):
|
2010-10-30 00:48:58 +08:00
|
|
|
return date_format(value, use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
elif isinstance(value, datetime.time):
|
2010-10-30 00:48:58 +08:00
|
|
|
return time_format(value, 'TIME_FORMAT', use_l10n=use_l10n)
|
2010-09-13 03:40:44 +08:00
|
|
|
else:
|
|
|
|
return value
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2009-12-31 06:11:48 +08:00
|
|
|
def localize_input(value, default=None):
|
|
|
|
"""
|
|
|
|
Checks if an input value is a localizable type and returns it
|
|
|
|
formatted with the appropriate formatting string of the current locale.
|
|
|
|
"""
|
2010-09-28 00:21:16 +08:00
|
|
|
if isinstance(value, (decimal.Decimal, float, int, long)):
|
2010-02-05 08:44:35 +08:00
|
|
|
return number_format(value)
|
2010-09-27 23:25:08 +08:00
|
|
|
elif isinstance(value, datetime.datetime):
|
2009-12-31 06:11:48 +08:00
|
|
|
value = datetime_safe.new_datetime(value)
|
|
|
|
format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
|
|
|
|
return value.strftime(format)
|
|
|
|
elif isinstance(value, datetime.date):
|
|
|
|
value = datetime_safe.new_date(value)
|
|
|
|
format = smart_str(default or get_format('DATE_INPUT_FORMATS')[0])
|
|
|
|
return value.strftime(format)
|
|
|
|
elif isinstance(value, datetime.time):
|
|
|
|
format = smart_str(default or get_format('TIME_INPUT_FORMATS')[0])
|
|
|
|
return value.strftime(format)
|
|
|
|
return value
|
2010-03-01 18:19:24 +08:00
|
|
|
|
|
|
|
def sanitize_separators(value):
|
|
|
|
"""
|
|
|
|
Sanitizes a value according to the current decimal and
|
|
|
|
thousand separator setting. Used with form field input.
|
|
|
|
"""
|
|
|
|
if settings.USE_L10N:
|
|
|
|
decimal_separator = get_format('DECIMAL_SEPARATOR')
|
|
|
|
if isinstance(value, basestring):
|
|
|
|
parts = []
|
|
|
|
if decimal_separator in value:
|
|
|
|
value, decimals = value.split(decimal_separator, 1)
|
|
|
|
parts.append(decimals)
|
|
|
|
if settings.USE_THOUSAND_SEPARATOR:
|
|
|
|
parts.append(value.replace(get_format('THOUSAND_SEPARATOR'), ''))
|
|
|
|
else:
|
|
|
|
parts.append(value)
|
|
|
|
value = '.'.join(reversed(parts))
|
|
|
|
return value
|