Conditions | 12 |
Total Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like add_default_language_settings() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
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 | |||
223 |