Completed
Push — master ( 2ea47d...8bcc2a )
by Diederik van der
01:05
created

get_fallback_language()   A

Complexity

Conditions 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 15
rs 9.4285
1
"""
2
The configuration wrappers that are used for :ref:`PARLER_LANGUAGES`.
3
"""
4
5
import copy
6
import sys
7
8
from django.conf import settings
9
from django.core.exceptions import ImproperlyConfigured
10
from django.utils import six
11
from django.utils.translation import get_language
12
from parler.utils.i18n import is_supported_django_language
13
14
15
def add_default_language_settings(languages_list, var_name='PARLER_LANGUAGES', **extra_defaults):
16
    """
17
    Apply extra defaults to the language settings.
18
    This function can also be used by other packages to
19
    create their own variation of ``PARLER_LANGUAGES`` with extra fields.
20
    For example::
21
22
        from django.conf import settings
23
        from parler import appsettings as parler_appsettings
24
25
        # Create local names, which are based on the global parler settings
26
        MYAPP_DEFAULT_LANGUAGE_CODE = getattr(settings, 'MYAPP_DEFAULT_LANGUAGE_CODE', parler_appsettings.PARLER_DEFAULT_LANGUAGE_CODE)
27
        MYAPP_LANGUAGES = getattr(settings, 'MYAPP_LANGUAGES', parler_appsettings.PARLER_LANGUAGES)
28
29
        # Apply the defaults to the languages
30
        MYAPP_LANGUAGES = parler_appsettings.add_default_language_settings(MYAPP_LANGUAGES, 'MYAPP_LANGUAGES',
31
            code=MYAPP_DEFAULT_LANGUAGE_CODE,
32
            fallback=MYAPP_DEFAULT_LANGUAGE_CODE,
33
            hide_untranslated=False
34
        )
35
36
    The returned object will be an :class:`~parler.utils.conf.LanguagesSetting` object,
37
    which adds additional methods to the :class:`dict` object.
38
39
    :param languages_list: The settings, in :ref:`PARLER_LANGUAGES` format.
40
    :param var_name: The name of your variable, for debugging output.
41
    :param extra_defaults: Any defaults to override in the ``languages_list['default']`` section, e.g. ``code``, ``fallback``, ``hide_untranslated``.
42
    :return: The updated ``languages_list`` with all defaults applied to all sections.
43
    :rtype: LanguagesSetting
44
    """
45
    languages_list = LanguagesSetting(languages_list)
46
47
    languages_list.setdefault('default', {})
48
    defaults = languages_list['default']
49
    defaults.setdefault('hide_untranslated', False)   # Whether queries with .active_translations() may or may not return the fallback language.
50
51
    if 'fallback' in defaults:
52
        #warnings.warn("Please use 'fallbacks' instead of 'fallback' in the 'defaults' section of {0}".format(var_name), DeprecationWarning)
53
        defaults['fallbacks'] = [defaults.pop('fallback')]
54
    if 'fallback' in extra_defaults:
55
        #warnings.warn("Please use 'fallbacks' instead of 'fallback' in parameters for {0} = add_default_language_settings(..)".format(var_name), DeprecationWarning)
56
        extra_defaults['fallbacks'] = [extra_defaults.pop('fallback')]
57
58
    defaults.update(extra_defaults)  # Also allow to override code and fallback this way.
59
60
    # This function previously existed in appsettings, where it could reference the defaults directly.
61
    # However, this module is a more logical place for this function. To avoid circular import problems,
62
    # the 'code' and 'fallback' parameters are always passed by the appsettings module.
63
    # In case these are missing, default to the original behavior for backwards compatibility.
64
    if 'code' not in defaults:
65
        from parler import appsettings
66
        defaults['code'] = appsettings.PARLER_DEFAULT_LANGUAGE_CODE
67
    if 'fallbacks' not in defaults:
68
        from parler import appsettings
69
        defaults['fallbacks'] = [appsettings.PARLER_DEFAULT_LANGUAGE_CODE]
70
71
    if not is_supported_django_language(defaults['code']):
72
        raise ImproperlyConfigured("The value for {0}['defaults']['code'] ('{1}') does not exist in LANGUAGES".format(var_name, defaults['code']))
73
74
    for site_id, lang_choices in six.iteritems(languages_list):
75
        if site_id == 'default':
76
            continue
77
78
        if not isinstance(lang_choices, (list, tuple)):
79
            raise ImproperlyConfigured("{0}[{1}] should be a tuple of language choices!".format(var_name, site_id))
80
        for i, choice in enumerate(lang_choices):
81
            if not is_supported_django_language(choice['code']):
82
                raise ImproperlyConfigured("{0}[{1}][{2}]['code'] does not exist in LANGUAGES".format(var_name, site_id, i))
83
84
            # Copy all items from the defaults, so you can provide new fields too.
85
            for key, value in six.iteritems(defaults):
86
                choice.setdefault(key, value)
87
88
    return languages_list
89
90
91
class LanguagesSetting(dict):
92
    """
93
    This is the actual object type of the :ref:`PARLER_LANGUAGES` setting.
94
    Besides the regular :class:`dict` behavior, it also adds some additional methods.
95
    """
96
97
    def get_language(self, language_code, site_id=None):
98
        """
99
        Return the language settings for the current site
100
101
        This function can be used with other settings variables
102
        to support modules which create their own variation of the ``PARLER_LANGUAGES`` setting.
103
        For an example, see :func:`~parler.appsettings.add_default_language_settings`.
104
        """
105
        if language_code is None:
106
            # This happens when using parler in management commands.
107
            # Use translation.activate('en') if you need to have a default locale active.
108
            raise ValueError("language_code can't be null")
109
110
        if site_id is None:
111
            site_id = getattr(settings, 'SITE_ID', None)
112
113
        for lang_dict in self.get(site_id, ()):
114
            if lang_dict['code'] == language_code:
115
                return lang_dict
116
117
        # no language match, search for variant: fr-ca falls back to fr
118
        for lang_dict in self.get(site_id, ()):
119
            if lang_dict['code'].split('-')[0] == language_code.split('-')[0]:
120
                return lang_dict
121
122
        return self['default']
123
124
    def get_active_choices(self, language_code=None, site_id=None):
125
        """
126
        Find out which translations should be visible in the site.
127
        It returns a list with either a single choice (the current language),
128
        or a list with the current language + fallback language.
129
        """
130
        if language_code is None:
131
            language_code = get_language()
132
133
        lang_dict = self.get_language(language_code, site_id=site_id)
134
        if not lang_dict['hide_untranslated']:
135
            return [language_code] + [lang for lang in lang_dict['fallbacks'] if lang != language_code]
136
        else:
137
            return [language_code]
138
139
    def get_fallback_languages(self, language_code=None, site_id=None):
140
        """
141
        Find out what the fallback language is for a given language choice.
142
143
        .. versionadded 1.5
144
        """
145
        choices = self.get_active_choices(language_code, site_id=site_id)
146
        return choices[1:]
147
148
    def get_fallback_language(self, language_code=None, site_id=None):
149
        """
150
        Find out what the fallback language is for a given language choice.
151
152
        .. deprecated:: 1.5
153
           Use :func:`get_fallback_languages` instead.
154
        """
155
        choices = self.get_active_choices(language_code, site_id=site_id)
156
        if choices and len(choices) > 1:
157
            # Still take the last, like previous code.
158
            # With multiple fallback languages that means taking the base language.
159
            # Hence, upgrade the code to use get_fallback_languages() instead.
160
            return choices[-1]
161
        else:
162
            return None
163
164
    def get_default_language(self):
165
        """
166
        Return the default language.
167
        """
168
        return self['default']['code']
169
170
    def get_first_language(self, site_id=None):
171
        """
172
        Return the first language for the current site.
173
        This can be used for user interfaces, where the languages are displayed in tabs.
174
        """
175
        if site_id is None:
176
            site_id = getattr(settings, 'SITE_ID', None)
177
178
        try:
179
            return self[site_id][0]['code']
180
        except (KeyError, IndexError):
181
            # No configuration, always fallback to default language.
182
            # This is essentially a non-multilingual configuration.
183
            return self['default']['code']
184
185
186
def get_parler_languages_from_django_cms(cms_languages=None):
187
    """
188
    Converts django CMS' setting CMS_LANGUAGES into PARLER_LANGUAGES. Since
189
    CMS_LANGUAGES is a strict superset of PARLER_LANGUAGES, we do a bit of
190
    cleansing to remove irrelevant items.
191
    """
192
    valid_keys = ['code', 'fallbacks', 'hide_untranslated',
193
                  'redirect_on_fallback']
194
    if cms_languages:
195
        if sys.version_info < (3, 0, 0):
196
            int_types = (int, long)
197
        else:
198
            int_types = int
199
200
        parler_languages = copy.deepcopy(cms_languages)
201
        for site_id, site_config in cms_languages.items():
202
            if site_id and (
203
                    not isinstance(site_id, int_types) and
204
                    site_id != 'default'
205
            ):
206
                del parler_languages[site_id]
207
                continue
208
209
            if site_id == 'default':
210
                for key, value in site_config.items():
211
                    if key not in valid_keys:
212
                        del parler_languages['default'][key]
213
            else:
214
                for i, lang_config in enumerate(site_config):
215
                    for key, value in lang_config.items():
216
                        if key not in valid_keys:
217
                            del parler_languages[site_id][i][key]
218
        return parler_languages
219
    return None
220