~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

Viewing changes to django/utils/formats.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from django.conf import settings
5
5
from django.utils import dateformat, numberformat, datetime_safe
6
6
from django.utils.importlib import import_module
7
 
from django.utils.encoding import smart_str
 
7
from django.utils.encoding import force_str
8
8
from django.utils.functional import lazy
9
9
from django.utils.safestring import mark_safe
 
10
from django.utils import six
10
11
from django.utils.translation import get_language, to_locale, check_for_language
11
12
 
12
13
# format_cache is a mapping from (format_type, lang) to the format string.
15
16
_format_cache = {}
16
17
_format_modules_cache = {}
17
18
 
 
19
ISO_INPUT_FORMATS = {
 
20
    'DATE_INPUT_FORMATS': ('%Y-%m-%d',),
 
21
    'TIME_INPUT_FORMATS': ('%H:%M:%S', '%H:%M'),
 
22
    'DATETIME_INPUT_FORMATS': (
 
23
        '%Y-%m-%d %H:%M:%S',
 
24
        '%Y-%m-%d %H:%M:%S.%f',
 
25
        '%Y-%m-%d %H:%M',
 
26
        '%Y-%m-%d'
 
27
    ),
 
28
}
 
29
 
18
30
def reset_format_cache():
19
31
    """Clear any cached formats.
20
32
 
65
77
    If use_l10n is provided and is not None, that will force the value to
66
78
    be localized (or not), overriding the value of settings.USE_L10N.
67
79
    """
68
 
    format_type = smart_str(format_type)
 
80
    format_type = force_str(format_type)
69
81
    if use_l10n or (use_l10n is None and settings.USE_L10N):
70
82
        if lang is None:
71
83
            lang = get_language()
81
93
            for module in get_format_modules(lang):
82
94
                try:
83
95
                    val = getattr(module, format_type)
 
96
                    for iso_input in ISO_INPUT_FORMATS.get(format_type, ()):
 
97
                        if iso_input not in val:
 
98
                            if isinstance(val, tuple):
 
99
                                val = list(val)
 
100
                            val.append(iso_input)
84
101
                    _format_cache[cache_key] = val
85
102
                    return val
86
103
                except AttributeError:
88
105
            _format_cache[cache_key] = None
89
106
    return getattr(settings, format_type)
90
107
 
91
 
get_format_lazy = lazy(get_format, unicode, list, tuple)
 
108
get_format_lazy = lazy(get_format, six.text_type, list, tuple)
92
109
 
93
110
def date_format(value, format=None, use_l10n=None):
94
111
    """
138
155
    be localized (or not), overriding the value of settings.USE_L10N.
139
156
    """
140
157
    if isinstance(value, bool):
141
 
        return mark_safe(unicode(value))
142
 
    elif isinstance(value, (decimal.Decimal, float, int, long)):
 
158
        return mark_safe(six.text_type(value))
 
159
    elif isinstance(value, (decimal.Decimal, float) + six.integer_types):
143
160
        return number_format(value, use_l10n=use_l10n)
144
161
    elif isinstance(value, datetime.datetime):
145
162
        return date_format(value, 'DATETIME_FORMAT', use_l10n=use_l10n)
155
172
    Checks if an input value is a localizable type and returns it
156
173
    formatted with the appropriate formatting string of the current locale.
157
174
    """
158
 
    if isinstance(value, (decimal.Decimal, float, int, long)):
 
175
    if isinstance(value, (decimal.Decimal, float) + six.integer_types):
159
176
        return number_format(value)
160
177
    elif isinstance(value, datetime.datetime):
161
178
        value = datetime_safe.new_datetime(value)
162
 
        format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
 
179
        format = force_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
163
180
        return value.strftime(format)
164
181
    elif isinstance(value, datetime.date):
165
182
        value = datetime_safe.new_date(value)
166
 
        format = smart_str(default or get_format('DATE_INPUT_FORMATS')[0])
 
183
        format = force_str(default or get_format('DATE_INPUT_FORMATS')[0])
167
184
        return value.strftime(format)
168
185
    elif isinstance(value, datetime.time):
169
 
        format = smart_str(default or get_format('TIME_INPUT_FORMATS')[0])
 
186
        format = force_str(default or get_format('TIME_INPUT_FORMATS')[0])
170
187
        return value.strftime(format)
171
188
    return value
172
189
 
177
194
    """
178
195
    if settings.USE_L10N:
179
196
        decimal_separator = get_format('DECIMAL_SEPARATOR')
180
 
        if isinstance(value, basestring):
 
197
        if isinstance(value, six.string_types):
181
198
            parts = []
182
199
            if decimal_separator in value:
183
200
                value, decimals = value.split(decimal_separator, 1)