Commit bb0a9a07 authored by Martin Brochhaus's avatar Martin Brochhaus Committed by Tim Graham
Browse files

Fixed #20477: Allowed list of modules for FORMAT_MODULE_PATH

Previously the FORMAT_MODULE_PATH setting only accepted one string (dotted
module path). A feature has been added to also allow a list of strings.

This is useful when using several reusable third party apps that define new
formats. We can now use them all and we can even override some of the formats
by providing a project-wide format module.
parent dfeef8e1
Loading
Loading
Loading
Loading
+23 −15
Original line number Diff line number Diff line
@@ -46,11 +46,19 @@ def iter_format_modules(lang, format_module_path=None):
    """
    Does the heavy lifting of finding format modules.
    """
    if check_for_language(lang):
        format_locations = ['django.conf.locale.%s']
    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:
            format_locations.append(format_module_path + '.%s')
            format_locations.reverse()
        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:
+14 −0
Original line number Diff line number Diff line
@@ -1378,6 +1378,20 @@ like::
                __init__.py
                formats.py

.. versionchanged:: 1.8

    You can also set this setting to a list of Python paths, for example::

        FORMAT_MODULE_PATH = [
            'mysite.formats',
            'some_app.formats',
        ]

    When Django searches for a certain format, it will go through all given
    Python paths until it finds a module that actually defines the given
    format. This means that formats defined in packages farther up in the list
    will take precendence over the same formats in packages farther down.

Available formats are :setting:`DATE_FORMAT`, :setting:`TIME_FORMAT`,
:setting:`DATETIME_FORMAT`, :setting:`YEAR_MONTH_FORMAT`,
:setting:`MONTH_DAY_FORMAT`, :setting:`SHORT_DATE_FORMAT`,
+4 −1
Original line number Diff line number Diff line
@@ -139,7 +139,10 @@ Forms
Internationalization
^^^^^^^^^^^^^^^^^^^^

* ...
* :setting:`FORMAT_MODULE_PATH` can now be a list of strings representing
  module paths. This allows importing several format modules from different
  reusable apps. It also allows overriding those custom formats in your main
  Django project.

Management Commands
^^^^^^^^^^^^^^^^^^^
+13 −4
Original line number Diff line number Diff line
@@ -154,11 +154,20 @@ Django provides format definitions for many locales, but sometimes you might
want to create your own, because a format files doesn't exist for your locale,
or because you want to overwrite some of the values.

To use custom formats, specify the path where you'll place format files first.
To do that, just set your :setting:`FORMAT_MODULE_PATH` setting to the package
where format files will exist, for instance::

    FORMAT_MODULE_PATH = 'mysite.formats'
.. versionchanged:: 1.8

    The ability to specify :setting:`FORMAT_MODULE_PATH` as a list was added.
    Previously, only a single string value was supported.

To use custom formats, specify the path where you'll place format files
first. To do that, just set your :setting:`FORMAT_MODULE_PATH` setting to
the package where format files will exist, for instance::

    FORMAT_MODULE_PATH = [
        'mysite.formats',
        'some_app.formats',
    ]

Files are not placed directly in this directory, but in a directory named as
the locale, and must be named ``formats.py``.
+0 −0

Empty file added.

Loading