From ccc05c7e8f825d84033e9cbbbe072c895f4dd7c3 Mon Sep 17 00:00:00 2001 From: PlayDay Date: Mon, 29 Apr 2019 03:03:19 +0300 Subject: [PATCH 1/3] (UPDATE) add explicit language codes validation in favor of keeping default languages ordering in project --- example/app/templatetags/sqlformat.py | 3 ++- modeltrans/apps.py | 3 ++- modeltrans/conf.py | 29 ++++++++++++++++----------- tests/test_conf.py | 18 ++++++++++++++++- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/example/app/templatetags/sqlformat.py b/example/app/templatetags/sqlformat.py index bb30080..258a4ee 100644 --- a/example/app/templatetags/sqlformat.py +++ b/example/app/templatetags/sqlformat.py @@ -1,8 +1,9 @@ from __future__ import unicode_literals -import sqlparse from django import template +import sqlparse + register = template.Library() diff --git a/modeltrans/apps.py b/modeltrans/apps.py index b187047..9e0dd6c 100644 --- a/modeltrans/apps.py +++ b/modeltrans/apps.py @@ -4,7 +4,7 @@ import django.apps from django.apps import AppConfig -from .conf import check_fallback_chain +from .conf import check_fallback_chain, check_lang_code_duplicates from .translator import translate_model @@ -14,6 +14,7 @@ class RegistrationConfig(AppConfig): def ready(self): check_fallback_chain() + check_lang_code_duplicates() for Model in django.apps.apps.get_models(): translate_model(Model) diff --git a/modeltrans/conf.py b/modeltrans/conf.py index f043d2c..ce4ffd2 100644 --- a/modeltrans/conf.py +++ b/modeltrans/conf.py @@ -26,12 +26,10 @@ def get_available_languages_setting(): defaults to the list of language codes extracted from django setting LANGUAGES """ languages = tuple( - set( - getattr( - settings, - "MODELTRANS_AVAILABLE_LANGUAGES", - (code for code, _ in getattr(settings, "LANGUAGES")), - ) + getattr( + settings, + "MODELTRANS_AVAILABLE_LANGUAGES", + (code for code, _ in getattr(settings, "LANGUAGES")), ) ) @@ -48,14 +46,13 @@ def get_available_languages(include_default=True): """ Returns a tuple of available languages for django-modeltrans. """ - MODELTRANS_AVAILABLE_LANGUAGES = get_available_languages_setting() + MODELTRANS_AVAILABLE_LANGUAGES = tuple(get_available_languages_setting()) if include_default: - return tuple( - set(itertools.chain(MODELTRANS_AVAILABLE_LANGUAGES, (get_default_language(),))) - ) - else: - return MODELTRANS_AVAILABLE_LANGUAGES + default_language = get_default_language() + if default_language not in MODELTRANS_AVAILABLE_LANGUAGES: + return tuple(itertools.chain((default_language,), MODELTRANS_AVAILABLE_LANGUAGES)) + return MODELTRANS_AVAILABLE_LANGUAGES def check_fallback_chain(): @@ -77,6 +74,14 @@ def check_fallback_chain(): raise ImproperlyConfigured(message_fmt.format(l)) +def check_lang_code_duplicates(): + available_languages = get_available_languages() + if len(available_languages) != len(set(available_languages)): + raise ImproperlyConfigured( + "Languages should not contains duplicates: {}".format(available_languages) + ) + + def get_fallback_chain(lang): """ Returns the list of fallback languages for language `lang`. diff --git a/tests/test_conf.py b/tests/test_conf.py index bd06697..b63d7c4 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -1,7 +1,12 @@ from django.core.exceptions import ImproperlyConfigured from django.test import TestCase, override_settings -from modeltrans.conf import check_fallback_chain, get_available_languages_setting +from modeltrans.conf import ( + check_fallback_chain, + check_lang_code_duplicates, + get_available_languages, + get_available_languages_setting, +) class FallbackConfTest(TestCase): @@ -37,3 +42,14 @@ def test_available_languages_should_be_str(self): message = "MODELTRANS_AVAILABLE_LANGUAGES should be an iterable of strings" with self.assertRaisesMessage(ImproperlyConfigured, message): get_available_languages_setting() + + @override_settings(MODELTRANS_AVAILABLE_LANGUAGES=("nl", "nl", "de")) + def test_lang_code_duplicates_validation(self): + message = "Languages should not contains duplicates: {}".format(get_available_languages()) + with self.assertRaisesMessage(ImproperlyConfigured, message): + check_lang_code_duplicates() + + @override_settings(MODELTRANS_AVAILABLE_LANGUAGES=("nl", "bg", "ar"), LANGUAGE_CODE="en") + def test_languages_ordering(self): + self.assertEqual(("en", "nl", "bg", "ar"), get_available_languages()) + self.assertEqual(("nl", "bg", "ar"), get_available_languages(include_default=False)) From bcc67b9fd8ca551487ca96bf5d1089cbf6224fba Mon Sep 17 00:00:00 2001 From: PlayDay Date: Fri, 3 May 2019 23:27:22 +0300 Subject: [PATCH 2/3] (UPDATE) error message for check_lang_code_duplicates --- example/app/templatetags/sqlformat.py | 3 +-- modeltrans/conf.py | 5 ++++- tests/test_conf.py | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/example/app/templatetags/sqlformat.py b/example/app/templatetags/sqlformat.py index 258a4ee..bb30080 100644 --- a/example/app/templatetags/sqlformat.py +++ b/example/app/templatetags/sqlformat.py @@ -1,8 +1,7 @@ from __future__ import unicode_literals -from django import template - import sqlparse +from django import template register = template.Library() diff --git a/modeltrans/conf.py b/modeltrans/conf.py index ce4ffd2..93d946e 100644 --- a/modeltrans/conf.py +++ b/modeltrans/conf.py @@ -78,7 +78,10 @@ def check_lang_code_duplicates(): available_languages = get_available_languages() if len(available_languages) != len(set(available_languages)): raise ImproperlyConfigured( - "Languages should not contains duplicates: {}".format(available_languages) + ( + "MODELTRANS_AVAILABLE_LANGUAGES or LANGUAGES " + "should not contain duplicates, current list: {}" + ).format(available_languages) ) diff --git a/tests/test_conf.py b/tests/test_conf.py index b63d7c4..066b1c6 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -45,7 +45,9 @@ def test_available_languages_should_be_str(self): @override_settings(MODELTRANS_AVAILABLE_LANGUAGES=("nl", "nl", "de")) def test_lang_code_duplicates_validation(self): - message = "Languages should not contains duplicates: {}".format(get_available_languages()) + message = ( + "MODELTRANS_AVAILABLE_LANGUAGES or LANGUAGES should not contain duplicates, current list: {}" + ).format(get_available_languages()) with self.assertRaisesMessage(ImproperlyConfigured, message): check_lang_code_duplicates() From d8e03fc356d09c477bf467400c0f51fc1e0b9148 Mon Sep 17 00:00:00 2001 From: PlayDay Date: Wed, 5 Jun 2019 09:26:53 +0300 Subject: [PATCH 3/3] (FIX) tox python 3.5 errors --- tests/test_conf.py | 3 --- tox.ini | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_conf.py b/tests/test_conf.py index b27e5da..066b1c6 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -53,8 +53,5 @@ def test_lang_code_duplicates_validation(self): @override_settings(MODELTRANS_AVAILABLE_LANGUAGES=("nl", "bg", "ar"), LANGUAGE_CODE="en") def test_languages_ordering(self): - """ - Test. - """ self.assertEqual(("en", "nl", "bg", "ar"), get_available_languages()) self.assertEqual(("nl", "bg", "ar"), get_available_languages(include_default=False)) diff --git a/tox.ini b/tox.ini index 3b23571..e6228cf 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ basepython = # workaround for Error installing '...django-tables2': editable mode is not supported for pyproject.toml-style projects. # https://github.com/pypa/pip/issues/6434 -install_command = python -m pip install --no-use-pep517 {opts} {packages} +# install_command = python -m pip install --no-use-pep517 {opts} {packages} usedevelop = true pip_pre = true