Esempio n. 1
0
 def test_get_format_modules_stability(self):
     with self.settings(USE_L10N=True,
             FORMAT_MODULE_PATH='regressiontests.i18n.other.locale'):
         with translation.override('de', deactivate=True):
             old = "%r" % get_format_modules(reverse=True)
             new = "%r" % get_format_modules(reverse=True) # second try
             self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.')
Esempio n. 2
0
File: tests.py Progetto: nasi/django
 def test_get_format_modules_stability(self):
     activate('de')
     old_format_module_path = settings.FORMAT_MODULE_PATH
     settings.FORMAT_MODULE_PATH = 'regressiontests.i18n.other.locale'
     try:
         settings.USE_L10N = True
         old = "%r" % get_format_modules(reverse=True)
         new = "%r" % get_format_modules(reverse=True) # second try
         self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.')
     finally:
         settings.FORMAT_MODULE_PATH = old_format_module_path
         deactivate()
Esempio n. 3
0
 def test_get_format_modules_stability(self):
     activate('de')
     old_format_module_path = settings.FORMAT_MODULE_PATH
     settings.FORMAT_MODULE_PATH = 'regressiontests.i18n.other.locale'
     try:
         settings.USE_L10N = True
         old = "%r" % get_format_modules(reverse=True)
         new = "%r" % get_format_modules(reverse=True) # second try
         self.assertEqual(new, old, 'Value returned by get_formats_modules() must be preserved between calls.')
     finally:
         settings.FORMAT_MODULE_PATH = old_format_module_path
         deactivate()
def _add12hrFormats():
    # Time12hrInput will not work unless django.forms.fields.TimeField
    # can process 12hr times, so sneak them into the default and all the
    # selectable locales that define TIME_INPUT_FORMATS.

    # strptime does not accept %P, %p is for both cases here.
    _12hrFormats = ['%I:%M%p', # 2:30pm
                    '%I%p']    # 7am

    # TIME_INPUT_FORMATS is defined in django.conf.global_settings if not
    # by the user's local settings.
    if (_12hrFormats[0] not in settings.TIME_INPUT_FORMATS or
        _12hrFormats[1] not in settings.TIME_INPUT_FORMATS):
        settings.TIME_INPUT_FORMATS += _12hrFormats

    # As at 2019-06-13 none of the built-in locales define TIME_INPUT_FORMATS
    # but a user-defined locale could or it could be added to a built-in one.
    langCodes = [language[0] for language in get_available_admin_languages()]
    langCodes.append(settings.LANGUAGE_CODE)
    for lang in langCodes:
        for module in get_format_modules(lang):
            inputFormats = getattr(module, 'TIME_INPUT_FORMATS', None)
            if (inputFormats is not None and
                (_12hrFormats[0] not in inputFormats or
                 _12hrFormats[1] not in inputFormats)):
                inputFormats += _12hrFormats
Esempio n. 5
0
def get_formats():
    """
    Returns all formats strings required for i18n to work
    """
    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",
        "DATE_INPUT_FORMATS",
        "TIME_INPUT_FORMATS",
        "DATETIME_INPUT_FORMATS",
    )
    result = {}
    for module in [settings] + get_format_modules(reverse=True):
        for attr in FORMAT_SETTINGS:
            result[attr] = get_format(attr)
    src = []
    for k, v in result.items():
        if isinstance(v, (six.string_types, int)):
            src.append("formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_text(v))))
        elif isinstance(v, (tuple, list)):
            v = [javascript_quote(smart_text(value)) for value in v]
            src.append("formats['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v)))
    return "".join(src)
Esempio n. 6
0
def get_formats():
    """
    Returns all formats strings required for i18n to work
    """
    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",
        "DATE_INPUT_FORMATS",
        "TIME_INPUT_FORMATS",
        "DATETIME_INPUT_FORMATS",
    )
    result = {}
    for module in [settings] + get_format_modules(reverse=True):
        for attr in FORMAT_SETTINGS:
            result[attr] = get_format(attr)
    formats = {}
    for k, v in result.items():
        if isinstance(v, (six.string_types, int)):
            formats[k] = smart_text(v)
        elif isinstance(v, (tuple, list)):
            formats[k] = [smart_text(value) for value in v]
    return formats
Esempio n. 7
0
def get_formats():
    """
    Returns all formats strings required for i18n to work
    """
    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",
        "DATE_INPUT_FORMATS",
        "TIME_INPUT_FORMATS",
        "DATETIME_INPUT_FORMATS",
    )
    result = {}
    for module in [settings] + get_format_modules(reverse=True):
        for attr in FORMAT_SETTINGS:
            result[attr] = get_format(attr)
    formats = {}
    for k, v in list(result.items()):
        if isinstance(v, (six.string_types, int)):
            formats[k] = smart_text(v)
        elif isinstance(v, (tuple, list)):
            formats[k] = [smart_text(value) for value in v]
    return formats
Esempio n. 8
0
def get_formats():
    """
    Returns all formats strings required for i18n to work
    """
    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',
        'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'
    )
    result = {}
    for module in [settings] + get_format_modules(reverse=True):
        for attr in FORMAT_SETTINGS:
            try:
                result[attr] = getattr(module, attr)
            except AttributeError:
                pass
    src = []
    for k, v in result.items():
        if isinstance(v, (basestring, int)):
            src.append("formats['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(smart_unicode(v))))
        elif isinstance(v, (tuple, list)):
            v = [javascript_quote(smart_unicode(value)) for value in v]
            src.append("formats['%s'] = ['%s'];\n" % (javascript_quote(k), "', '".join(v)))
    return ''.join(src)
Esempio n. 9
0
def get_date_format_override():
    if hasattr(settings, 'USE_I18N') and settings.USE_I18N==True:
        
        for m in formats.get_format_modules():
            if hasattr(m, 'DATE_INPUT_FORMATS'):
                return ''
            else: # fall back to the ISO to be sure date will be parsed
                return 'yy-mm-dd'
    else: # Fall back to ISO if I18N is *not* used
        return 'yy-mm-dd'
Esempio n. 10
0
def get_date_format_override():
    if hasattr(settings, 'USE_I18N') and settings.USE_I18N == True:

        for m in formats.get_format_modules():
            if hasattr(m, 'DATE_INPUT_FORMATS'):
                return ''
            else:  # fall back to the ISO to be sure date will be parsed
                return 'yy-mm-dd'
    else:  # Fall back to ISO if I18N is *not* used
        return 'yy-mm-dd'
Esempio n. 11
0
def badge_datetime_format(timestamp):
    lang = translation.get_language()
    for module in formats.get_format_modules(lang):
        fmt = getattr(module, "BADGE_DATETIME_FORMAT", None)
        if fmt is not None:
            break
    else:
        logger.error("No BADGE_DATETIME_FORMAT found for language: %s", lang)
        fmt = "d/m H:i"   # 18/02 16:33, as fallback in case nothing is defined

    localized_time = timezone.localtime(timestamp)
    return formats.date_format(localized_time, format=fmt)
Esempio n. 12
0
def badge_datetime_format(timestamp):
    lang = translation.get_language()
    for module in formats.get_format_modules(lang):
        fmt = getattr(module, "BADGE_DATETIME_FORMAT", None)
        if fmt is not None:
            break
    else:
        logger.error("No BADGE_DATETIME_FORMAT found for language: %s", lang)
        fmt = "d/m H:i"  # 18/02 16:33, as fallback in case nothing is defined

    localized_time = timezone.localtime(timestamp)
    return formats.date_format(localized_time, format=fmt)
Esempio n. 13
0
def get_formats():
    """
    Returns an iterator over all formats in formats file
    """
    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 module in [settings] + get_format_modules():
        for attr in FORMAT_SETTINGS:
            try:
                result[attr] = getattr(module, attr)
            except AttributeError:
                pass
    return result
Esempio n. 14
0
File: i18n.py Progetto: zain/django
def get_formats():
    """
    Returns all formats strings required for i18n to work
    """
    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',
        'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'
    )
    result = {}
    for module in [settings] + get_format_modules(reverse=True):
        for attr in FORMAT_SETTINGS:
            try:
                result[attr] = getattr(module, attr)
            except AttributeError:
                pass
    return result
Esempio n. 15
0
def get_formats():
    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',
        'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'
    )
    result = {}
    return_result = {}
    for module in [settings] + get_format_modules(reverse=True):
        for attr in FORMAT_SETTINGS:
            result[attr] = get_format(attr)
    for k, v in result.items():
        if isinstance(v, (basestring, int)):
            return_result[k] = smart_unicode(v)
        elif isinstance(v, (tuple, list)):
            v = [javascript_quote(smart_unicode(value)) for value in v]
            return_result[k] = "', '".join(v)
    return return_result
Esempio n. 16
0
def _add12hrFormats():
    # Time12hrInput will not work unless django.forms.fields.TimeField
    # can process 12hr times, so sneak them into the default and all locales
    # TIME_INPUT_FORMATS.

    # Note: strptime does not accept %P %p is for both cases here
    _12hrFormats = [
        '%I:%M%p',  # 2:30pm
        '%I%p'
    ]  # 7am
    if (_12hrFormats[0] not in settings.TIME_INPUT_FORMATS
            or _12hrFormats[1] not in settings.TIME_INPUT_FORMATS):
        settings.TIME_INPUT_FORMATS += _12hrFormats

    for lang, _ in getattr(settings, 'WAGTAILADMIN_PERMITTED_LANGUAGES', []):
        for module in get_format_modules(lang):
            inputFormats = getattr(module, 'TIME_INPUT_FORMATS', [])
            if (_12hrFormats[0] not in inputFormats
                    or _12hrFormats[1] not in inputFormats):
                inputFormats += _12hrFormats
                setattr(module, 'TIME_INPUT_FORMATS', inputFormats)
Esempio n. 17
0
def get_formats():
    """
    Returns all formats strings required for i18n to work
    """
    from django.conf import settings
    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',
        'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS'
    )
    result = {}
    for module in [settings] + get_format_modules(reverse=True):
        for attr in FORMAT_SETTINGS:
            result[attr] = get_format(attr)
    formats = {}
    for k, v in result.items():
        if isinstance(v, (six.string_types, int)):
            formats[k] = smart_text(v)
        elif isinstance(v, (tuple, list)):
            formats[k] = [smart_text(value) for value in v]
    return formats
Esempio n. 18
0
import gettext as gettext_module