2009-12-23 01:58:49 +08:00
|
|
|
import datetime
|
2015-01-28 20:35:27 +08:00
|
|
|
import decimal
|
2013-02-15 23:37:52 +08:00
|
|
|
import unicodedata
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2009-12-23 01:58:49 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils import dateformat, datetime_safe, numberformat, six
|
2012-08-30 04:40:51 +08:00
|
|
|
from django.utils.encoding import force_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
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.translation import (
|
|
|
|
check_for_language, get_language, to_locale,
|
|
|
|
)
|
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 = {}
|
|
|
|
|
2012-10-13 18:21:35 +08:00
|
|
|
ISO_INPUT_FORMATS = {
|
2015-01-22 00:55:57 +08:00
|
|
|
'DATE_INPUT_FORMATS': ['%Y-%m-%d'],
|
|
|
|
'TIME_INPUT_FORMATS': ['%H:%M:%S', '%H:%M:%S.%f', '%H:%M'],
|
|
|
|
'DATETIME_INPUT_FORMATS': [
|
2012-10-13 18:21:35 +08:00
|
|
|
'%Y-%m-%d %H:%M:%S',
|
|
|
|
'%Y-%m-%d %H:%M:%S.%f',
|
|
|
|
'%Y-%m-%d %H:%M',
|
|
|
|
'%Y-%m-%d'
|
2015-01-22 00:55:57 +08:00
|
|
|
],
|
2012-10-13 18:21:35 +08:00
|
|
|
}
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2015-11-12 03:10:55 +08:00
|
|
|
FORMAT_SETTINGS = frozenset([
|
|
|
|
'DECIMAL_SEPARATOR',
|
|
|
|
'THOUSAND_SEPARATOR',
|
|
|
|
'NUMBER_GROUPING',
|
|
|
|
'FIRST_DAY_OF_WEEK',
|
|
|
|
'MONTH_DAY_FORMAT',
|
|
|
|
'TIME_FORMAT',
|
|
|
|
'DATE_FORMAT',
|
|
|
|
'DATETIME_FORMAT',
|
|
|
|
'SHORT_DATE_FORMAT',
|
|
|
|
'SHORT_DATETIME_FORMAT',
|
|
|
|
'YEAR_MONTH_FORMAT',
|
|
|
|
'DATE_INPUT_FORMATS',
|
|
|
|
'TIME_INPUT_FORMATS',
|
|
|
|
'DATETIME_INPUT_FORMATS',
|
|
|
|
])
|
|
|
|
|
|
|
|
|
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 = {}
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-09-07 06:01:23 +08:00
|
|
|
def iter_format_modules(lang, format_module_path=None):
|
2010-09-27 23:25:08 +08:00
|
|
|
"""
|
|
|
|
Does the heavy lifting of finding format modules.
|
|
|
|
"""
|
2014-05-19 13:23:45 +08:00
|
|
|
if not check_for_language(lang):
|
|
|
|
return
|
|
|
|
|
|
|
|
if format_module_path is None:
|
|
|
|
format_module_path = settings.FORMAT_MODULE_PATH
|
|
|
|
|
|
|
|
format_locations = []
|
|
|
|
if format_module_path:
|
|
|
|
if isinstance(format_module_path, six.string_types):
|
|
|
|
format_module_path = [format_module_path]
|
|
|
|
for path in format_module_path:
|
|
|
|
format_locations.append(path + '.%s')
|
|
|
|
format_locations.append('django.conf.locale.%s')
|
|
|
|
locale = to_locale(lang)
|
|
|
|
locales = [locale]
|
|
|
|
if '_' in locale:
|
|
|
|
locales.append(locale.split('_')[0])
|
|
|
|
for location in format_locations:
|
|
|
|
for loc in locales:
|
|
|
|
try:
|
|
|
|
yield import_module('%s.formats' % (location % loc))
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2010-09-27 23:25:08 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-09-22 23:04:27 +08:00
|
|
|
def get_format_modules(lang=None, 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
|
|
|
"""
|
2011-09-22 23:04:27 +08:00
|
|
|
if lang is None:
|
|
|
|
lang = get_language()
|
2015-12-27 04:11:53 +08:00
|
|
|
if lang not in _format_modules_cache:
|
|
|
|
_format_modules_cache[lang] = list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH))
|
|
|
|
modules = _format_modules_cache[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
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
"""
|
2012-08-30 04:40:51 +08:00
|
|
|
format_type = force_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:
|
2011-10-20 20:14:06 +08:00
|
|
|
cached = _format_cache[cache_key]
|
|
|
|
if cached is not None:
|
|
|
|
return cached
|
2010-09-27 23:25:08 +08:00
|
|
|
except KeyError:
|
2011-09-22 23:04:27 +08:00
|
|
|
for module in get_format_modules(lang):
|
2010-09-27 23:25:08 +08:00
|
|
|
try:
|
|
|
|
val = getattr(module, format_type)
|
2012-10-13 18:21:35 +08:00
|
|
|
for iso_input in ISO_INPUT_FORMATS.get(format_type, ()):
|
|
|
|
if iso_input not in val:
|
|
|
|
if isinstance(val, tuple):
|
|
|
|
val = list(val)
|
|
|
|
val.append(iso_input)
|
2010-09-27 23:25:08 +08:00
|
|
|
_format_cache[cache_key] = val
|
|
|
|
return val
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
_format_cache[cache_key] = None
|
2015-11-25 19:17:59 +08:00
|
|
|
if format_type not in FORMAT_SETTINGS:
|
|
|
|
return format_type
|
|
|
|
# Return the general setting by default
|
2009-12-23 01:58:49 +08:00
|
|
|
return getattr(settings, format_type)
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
get_format_lazy = lazy(get_format, six.text_type, list, tuple)
|
2011-05-02 00:14:57 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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
|
|
|
|
2013-11-03 07:53:29 +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
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-09-08 21:25:11 +08:00
|
|
|
def number_format(value, decimal_pos=None, use_l10n=None, force_grouping=False):
|
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),
|
2011-09-08 21:25:11 +08:00
|
|
|
force_grouping=force_grouping
|
2009-12-23 01:58:49 +08:00
|
|
|
)
|
|
|
|
|
2013-11-03 07:53:29 +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
|
|
|
"""
|
2015-11-12 21:59:37 +08:00
|
|
|
if isinstance(value, six.string_types): # Handle strings first for performance reasons.
|
|
|
|
return value
|
|
|
|
elif isinstance(value, bool): # Make sure booleans don't get treated as numbers
|
2012-07-20 20:48:51 +08:00
|
|
|
return mark_safe(six.text_type(value))
|
2012-07-20 18:45:19 +08:00
|
|
|
elif isinstance(value, (decimal.Decimal, float) + six.integer_types):
|
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)
|
2015-11-12 21:59:37 +08:00
|
|
|
return value
|
2009-12-23 01:58:49 +08:00
|
|
|
|
2013-11-03 07:53:29 +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.
|
|
|
|
"""
|
2015-11-12 21:59:37 +08:00
|
|
|
if isinstance(value, six.string_types): # Handle strings first for performance reasons.
|
|
|
|
return value
|
2016-02-03 22:59:55 +08:00
|
|
|
elif isinstance(value, bool): # Don't treat booleans as numbers.
|
|
|
|
return six.text_type(value)
|
2015-11-12 21:59:37 +08:00
|
|
|
elif isinstance(value, (decimal.Decimal, float) + six.integer_types):
|
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)
|
2012-08-30 04:40:51 +08:00
|
|
|
format = force_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
|
2009-12-31 06:11:48 +08:00
|
|
|
return value.strftime(format)
|
|
|
|
elif isinstance(value, datetime.date):
|
|
|
|
value = datetime_safe.new_date(value)
|
2012-08-30 04:40:51 +08:00
|
|
|
format = force_str(default or get_format('DATE_INPUT_FORMATS')[0])
|
2009-12-31 06:11:48 +08:00
|
|
|
return value.strftime(format)
|
|
|
|
elif isinstance(value, datetime.time):
|
2012-08-30 04:40:51 +08:00
|
|
|
format = force_str(default or get_format('TIME_INPUT_FORMATS')[0])
|
2009-12-31 06:11:48 +08:00
|
|
|
return value.strftime(format)
|
|
|
|
return value
|
2010-03-01 18:19:24 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
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.
|
|
|
|
"""
|
2013-02-15 23:37:52 +08:00
|
|
|
if settings.USE_L10N and isinstance(value, six.string_types):
|
|
|
|
parts = []
|
2010-03-01 18:19:24 +08:00
|
|
|
decimal_separator = get_format('DECIMAL_SEPARATOR')
|
2013-02-15 23:37:52 +08:00
|
|
|
if decimal_separator in value:
|
|
|
|
value, decimals = value.split(decimal_separator, 1)
|
|
|
|
parts.append(decimals)
|
|
|
|
if settings.USE_THOUSAND_SEPARATOR:
|
|
|
|
thousand_sep = get_format('THOUSAND_SEPARATOR')
|
2014-08-19 01:57:50 +08:00
|
|
|
if thousand_sep == '.' and value.count('.') == 1 and len(value.split('.')[-1]) != 3:
|
|
|
|
# Special case where we suspect a dot meant decimal separator (see #22171)
|
|
|
|
pass
|
|
|
|
else:
|
2014-09-26 20:31:50 +08:00
|
|
|
for replacement in {
|
|
|
|
thousand_sep, unicodedata.normalize('NFKD', thousand_sep)}:
|
2014-08-19 01:57:50 +08:00
|
|
|
value = value.replace(replacement, '')
|
2013-02-15 23:37:52 +08:00
|
|
|
parts.append(value)
|
|
|
|
value = '.'.join(reversed(parts))
|
2010-03-01 18:19:24 +08:00
|
|
|
return value
|