manager: simplify (and fix) localisation handling of datetime widget
This commit is contained in:
parent
cf7ebcca48
commit
40a83b3f4c
|
@ -13,61 +13,10 @@ import uuid
|
|||
|
||||
from django.forms import widgets
|
||||
from django.forms.widgets import DateTimeInput, DateInput, TimeInput
|
||||
from django.utils.formats import get_format, get_language
|
||||
from django.utils.formats import get_language
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.six import string_types
|
||||
|
||||
# This should be updated as more .po files are added to the datetime picker javascript code
|
||||
supported_languages = set([
|
||||
'ar',
|
||||
'bg',
|
||||
'ca', 'cs',
|
||||
'da', 'de',
|
||||
'ee', 'el', 'es',
|
||||
'fi', 'fr',
|
||||
'he', 'hr', 'hu',
|
||||
'id', 'is', 'it',
|
||||
'ja',
|
||||
'ko', 'kr',
|
||||
'lt', 'lv',
|
||||
'ms',
|
||||
'nb', 'nl', 'no',
|
||||
'pl', 'pt-BR', 'pt',
|
||||
'ro', 'rs', 'rs-latin', 'ru',
|
||||
'sk', 'sl', 'sv', 'sw',
|
||||
'th', 'tr',
|
||||
'ua', 'uk',
|
||||
'zh-CN', 'zh-TW',
|
||||
])
|
||||
|
||||
|
||||
def get_supported_language(language_country_code):
|
||||
"""Helps us get from django's 'language-countryCode' to the datepicker's 'language' if we
|
||||
possibly can.
|
||||
|
||||
If we pass the django 'language_countryCode' through untouched then it might not
|
||||
match an exact language string supported by the datepicker and would default to English which
|
||||
would be worse than trying to match the language part.
|
||||
"""
|
||||
|
||||
# Catch empty strings in case one sneeks in
|
||||
if not language_country_code:
|
||||
return 'en'
|
||||
|
||||
# Check full language & country code against the supported languages as there are dual entries
|
||||
# in the list eg. zh-CN (assuming that is a language country code)
|
||||
if language_country_code in supported_languages:
|
||||
return language_country_code
|
||||
|
||||
# Grab just the language part and try that
|
||||
language = language_country_code.split('-')[0]
|
||||
if language in supported_languages:
|
||||
return language
|
||||
|
||||
# Otherwise return English as the default
|
||||
return 'en'
|
||||
|
||||
|
||||
DATE_FORMAT_JS_PY_MAPPING = {
|
||||
'P': '%p',
|
||||
'ss': '%S',
|
||||
|
@ -164,39 +113,15 @@ class PickerWidgetMixin(object):
|
|||
attrs = {'readonly': ''}
|
||||
|
||||
self.options = options
|
||||
self.options['language'] = get_language().split('-')[0]
|
||||
|
||||
self.is_localized = False
|
||||
self.format = None
|
||||
|
||||
# We want to have a Javascript style date format specifier in the options dictionary and we
|
||||
# want a Python style date format specifier as a member variable for parsing the date string
|
||||
# from the form data
|
||||
if usel10n is True:
|
||||
# If we're doing localisation, get the local Python date format and convert it to
|
||||
# Javascript data format for the options dictionary
|
||||
self.is_localized = True
|
||||
|
||||
# Get format from django format system
|
||||
self.format = get_format(self.format_name)[0]
|
||||
|
||||
# Convert Python format specifier to Javascript format specifier
|
||||
self.options['format'] = DATE_FORMAT_TO_JS_REGEX.sub(
|
||||
lambda x: DATE_FORMAT_PY_JS_MAPPING[x.group()],
|
||||
self.format
|
||||
)
|
||||
|
||||
# Set the local language
|
||||
self.options['language'] = get_supported_language(get_language())
|
||||
|
||||
else:
|
||||
|
||||
# If we're not doing localisation, get the Javascript date format provided by the user,
|
||||
# with a default, and convert it to a Python data format for later string parsing
|
||||
date_format = self.options['format']
|
||||
self.format = DATE_FORMAT_TO_PYTHON_REGEX.sub(
|
||||
lambda x: DATE_FORMAT_JS_PY_MAPPING[x.group()],
|
||||
date_format
|
||||
)
|
||||
# We're not doing localisation, get the Javascript date format provided by the user,
|
||||
# with a default, and convert it to a Python data format for later string parsing
|
||||
date_format = self.options['format']
|
||||
self.format = DATE_FORMAT_TO_PYTHON_REGEX.sub(
|
||||
lambda x: DATE_FORMAT_JS_PY_MAPPING[x.group()],
|
||||
date_format
|
||||
)
|
||||
|
||||
super(PickerWidgetMixin, self).__init__(attrs, format=self.format)
|
||||
|
||||
|
@ -228,23 +153,6 @@ class PickerWidgetMixin(object):
|
|||
)
|
||||
)
|
||||
|
||||
def _media(self):
|
||||
|
||||
js = ["js/bootstrap-datetimepicker.js"]
|
||||
|
||||
language = self.options.get('language', 'en')
|
||||
if language != 'en':
|
||||
js.append("js/locales/bootstrap-datetimepicker.%s.js" % language)
|
||||
|
||||
return widgets.Media(
|
||||
css={
|
||||
'all': ('css/datetimepicker.css',)
|
||||
},
|
||||
js=js
|
||||
)
|
||||
|
||||
media = property(_media)
|
||||
|
||||
|
||||
class DateTimeWidget(PickerWidgetMixin, DateTimeInput):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue