def test_behaviour_of_active_translations_and_hide_untranslated(self): self.create_event(title='test event', start_date='2015-01-01') self.assertEqual(Event.objects.active_translations('en').count(), 1) self.assertEqual(Event.objects.active_translations('de').count(), 1) self.assertEqual(Event.objects.active_translations('jp').count(), 1) parler_languages = add_default_language_settings({ 1: ( {'code': 'en'}, {'code': 'de'}, ), 'default': { 'fallback': 'en', 'hide_untranslated': True, } }) with override_parler_settings(PARLER_LANGUAGES=parler_languages): self.assertEqual( Event.objects.active_translations('en').count(), 1 ) self.assertEqual( Event.objects.active_translations('de').count(), 0 ) self.assertEqual( Event.objects.active_translations('jp').count(), 0 )
def test_index_multilingual(self): self.index = ArticleIndex() content0 = self.rand_str(prefix='content0_') self.setup_categories() article_1 = self.create_article(content=content0, lead_in=u'lead in text', title=u'a title') article_2 = self.create_article(content=content0, lead_in=u'lead in text', title=u'second title') for article in (article_1, article_2): for category in (self.category1, self.category2): article.categories.add(category) with switch_language(article_2, 'de'): article_2.title = u'de title' article_2.lead_in = u'de lead in' article_2.save() LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): with smart_override('de'): language = get_current_language() # english-only article is excluded qs = self.index.index_queryset(language) self.assertEqual(qs.count(), 1) self.assertEqual( qs.translated(language, title__icontains='title').count(), 1) # the language is correctly setup for article_de in qs: self.assertEqual(self.index.get_title(article_de), 'de title') self.assertEqual(self.index.get_description(article_de), 'de lead in')
def test_post_list_view_fallback(self): pages = self.get_pages() self.get_posts() PARLER_FALLBACK = { # noqa: N806 1: ( {"code": "en"}, {"code": "it"}, {"code": "fr", "hide_untranslated": True}, ), "default": {"fallback": "en", "hide_untranslated": False}, } with smart_override("fr"): view_obj = PostListView() request = self.get_page_request(pages[1], self.user, lang="fr", edit=True) view_obj.request = request view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 3) PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK) # noqa: N806 with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK): view_obj = PostListView() request = self.get_page_request(pages[1], self.user, lang="fr", edit=True) view_obj.request = request view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 0)
def PARLER_LANGUAGES(self): return add_default_language_settings({ 1: [{ 'code': lang[0] } for lang in properties.LANGUAGES], 'default': { 'fallbacks': [lang[0] for lang in properties.LANGUAGES], 'hide_untranslated': False } })
def test_dashboard_detail_not_translated_no_fallback(self): """ If the fallback is disabled, dashboard is available only in the language in which is translated """ author = self.create_person() code = "{0}-".format(self.language) dashboard = Dashboard.objects.create( title=self.rand_str(), slug=self.rand_str(prefix=code), app_config=self.app_config, author=author, owner=author.user, publishing_date=now(), is_published=True, ) dashboard.save() dashboard.categories.add(self.category1) PARLER_LANGUAGES = { 1: ( { 'code': 'de' }, { 'code': 'fr' }, { 'code': 'en' }, ), 'default': { 'hide_untranslated': True, } } LANGUAGES = add_default_language_settings(PARLER_LANGUAGES) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): # current language - it still exists dashboard = Dashboard.objects.get(pk=dashboard.pk) language = settings.LANGUAGES[0][0] with switch_language(self.category1, language): url = reverse('dashboards_app:dashboard-detail', kwargs={'slug': dashboard.slug}) response = self.client.get(url) self.assertContains(response, dashboard.title) # non existing language - it does NOT exists language = settings.LANGUAGES[1][0] with switch_language(self.category1, language): url = reverse('dashboards_app:dashboard-detail', kwargs={'slug': dashboard.slug}) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_article_detail_not_translated_fallback(self): """ If the fallback is configured, article is available in any (configured) language """ author = self.create_person() code = "{0}-".format(self.language) with override(settings.LANGUAGES[0][0]): article = Article.objects.create( title=self.rand_str(), slug=self.rand_str(prefix=code), app_config=self.app_config, author=author, owner=author.user, publishing_date=now(), is_published=True, ) article.save() article.categories.add(self.category1) url_one = reverse( 'aldryn_newsblog:article-detail', kwargs={'slug': article.slug} ) # Parler settings should be same as cms settings and vice versa # ensure that if hide_untranslated = True we don't have a fallback # redirect. LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): language = settings.LANGUAGES[1][0] with switch_language(article, language): slug = article.safe_translation_getter('slug', None, language_code=language, any_language=True) url = reverse( 'aldryn_newsblog:article-detail', kwargs={'slug': slug} ) self.assertNotEquals(url, url_one) response = self.client.get(url) self.assertEquals(response.status_code, 404) # Test again with redirect_on_fallback = False with self.settings(CMS_LANGUAGES=self.NO_REDIRECT_CMS_SETTINGS): language = settings.LANGUAGES[1][0] with switch_language(article, language): slug = article.safe_translation_getter('slug', None) url = reverse( 'aldryn_newsblog:article-detail', kwargs={'slug': slug, } ) self.assertNotEquals(url, url_one) response = self.client.get(url) self.assertEquals(response.status_code, 404)
def test_post_list_view_fallback(self): page1, page2 = self.get_pages() post1, post2 = self.get_posts() PARLER_FALLBACK = { 1: ( { 'code': 'en' }, { 'code': 'it' }, { 'code': 'fr', 'hide_untranslated': True }, ), 'default': { 'fallback': 'en', 'hide_untranslated': False, } } view_obj = PostListView() request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True) activate('fr') view_obj.request = request view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 2) PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK) with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK): view_obj = PostListView() request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True) activate('fr') view_obj.request = request view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 0)
def test_post_list_view_fallback(self): pages = self.get_pages() self.get_posts() PARLER_FALLBACK = { 1: ( { 'code': 'en' }, { 'code': 'it' }, { 'code': 'fr', 'hide_untranslated': True }, ), 'default': { 'fallback': 'en', 'hide_untranslated': False, } } with smart_override('fr'): view_obj = PostListView() request = self.get_page_request(pages[1], self.user, lang='fr', edit=True) view_obj.request = request view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 3) PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK) with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK): view_obj = PostListView() request = self.get_page_request(pages[1], self.user, lang='fr', edit=True) view_obj.request = request view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 0)
def test_article_detail_not_translated_fallback(self): """ If the fallback is configured, article is available in any (configured) language """ author = self.create_person() code = "{0}-".format(self.language) with override(settings.LANGUAGES[0][0]): article = Article.objects.create( title=self.rand_str(), slug=self.rand_str(prefix=code), app_config=self.app_config, author=author, owner=author.user, publishing_date=now()) article.save() article.categories.add(self.category1) url_one = reverse( 'aldryn_newsblog:article-detail', kwargs={'slug': article.slug} ) LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_SHOW) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): # Non-existing language - an article will be returned, even though # it is the wrong language. language = settings.LANGUAGES[1][0] with switch_language(article, language): url = reverse( 'aldryn_newsblog:article-detail', kwargs={'slug': article.slug, } ) self.assertNotEquals(url, url_one) response = self.client.get(url) # This is a redirect to the new language self.assertEquals(response.status_code, 302) # Test again with redirect_on_fallback = False with self.settings(CMS_LANGUAGES=self.NO_REDIRECT_CMS_SETTINGS): language = settings.LANGUAGES[1][0] with switch_language(article, language): url = reverse( 'aldryn_newsblog:article-detail', kwargs={'slug': article.slug, } ) self.assertNotEquals(url, url_one) response = self.client.get(url) # This is a redirect to the new language self.assertEquals(response.status_code, 200)
def test_article_detail_not_translated_no_fallback(self): """ If the fallback is disabled, article is available only in the language in which is translated """ author = self.create_person() code = "{0}-".format(self.language) article = Article.objects.create( title=self.rand_str(), slug=self.rand_str(prefix=code), app_config=self.app_config, author=author, owner=author.user, publishing_date=now(), is_published=True, ) article.save() article.categories.add(self.category1) PARLER_LANGUAGES = { 1: ( {'code': 'de'}, {'code': 'fr'}, {'code': 'en'}, ), 'default': { 'hide_untranslated': True, } } LANGUAGES = add_default_language_settings(PARLER_LANGUAGES) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): # current language - it still exists article = Article.objects.get(pk=article.pk) language = settings.LANGUAGES[0][0] with switch_language(self.category1, language): url = reverse('aldryn_newsblog:article-detail', kwargs={'slug': article.slug}) response = self.client.get(url) self.assertContains(response, article.title) # non existing language - it does NOT exists language = settings.LANGUAGES[1][0] with switch_language(self.category1, language): url = reverse('aldryn_newsblog:article-detail', kwargs={'slug': article.slug}) response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_articles_by_category(self): """ Tests that we can find articles by their categories, in ANY of the languages they are translated to. """ LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): author = self.create_person() for category in (self.category1, self.category2): articles = [] code = "{0}-".format(self.language) for _ in range(11): article = Article.objects.create( title=self.rand_str(), slug=self.rand_str(prefix=code), app_config=self.app_config, author=author, owner=author.user, publishing_date=now(), is_published=True, ) # Make sure there are translations in place for the # articles. for language, _ in settings.LANGUAGES[1:]: with switch_language(article, language): code = "{0}-".format(language) article.title = self.rand_str(prefix=code) article.save() article.categories.add(category) articles.append(article) for language, _ in settings.LANGUAGES: with switch_language(category, language): url = reverse( 'aldryn_newsblog:article-list-by-category', kwargs={'category': category.slug}) response = self.client.get(url) for article in articles: if language in article.get_available_languages(): article.set_current_language(language) self.assertContains(response, article.title) else: article.set_current_language(language) self.assertNotContains(response, article.title)
def test_dashboards_by_category(self): """ Tests that we can find dashboards by their categories, in ANY of the languages they are translated to. """ LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): author = self.create_person() for category in (self.category1, self.category2): dashboards = [] code = "{0}-".format(self.language) for _ in range(11): dashboard = Dashboard.objects.create( title=self.rand_str(), slug=self.rand_str(prefix=code), app_config=self.app_config, author=author, owner=author.user, publishing_date=now(), is_published=True, ) # Make sure there are translations in place for the # dashboards. for language, _ in settings.LANGUAGES[1:]: with switch_language(dashboard, language): code = "{0}-".format(language) dashboard.title = self.rand_str(prefix=code) dashboard.save() dashboard.categories.add(category) dashboards.append(dashboard) for language, _ in settings.LANGUAGES: with switch_language(category, language): url = reverse( 'dashboards_app:dashboard-list-by-category', kwargs={'category': category.slug}) response = self.client.get(url) for dashboard in dashboards: if language in dashboard.get_available_languages(): dashboard.set_current_language(language) self.assertContains(response, dashboard.title) else: dashboard.set_current_language(language) self.assertNotContains(response, dashboard.title)
def test_articles_by_category(self): """ Tests that we can find articles by their categories, in ANY of the languages they are translated to. """ LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): author = self.create_person() for category in (self.category1, self.category2): articles = [] code = "{0}-".format(self.language) for _ in range(11): article = Article.objects.create( title=self.rand_str(), slug=self.rand_str(prefix=code), app_config=self.app_config, author=author, owner=author.user, publishing_date=now()) # Make sure there are translations in place for the # articles. for language, _ in settings.LANGUAGES[1:]: with switch_language(article, language): code = "{0}-".format(language) article.title = self.rand_str(prefix=code) article.save() article.categories.add(category) articles.append(article) for language, _ in settings.LANGUAGES: with switch_language(category, language): url = reverse( 'aldryn_newsblog:article-list-by-category', kwargs={'category': category.slug}) response = self.client.get(url) for article in articles: if language in article.get_available_languages(): article.set_current_language(language) self.assertContains(response, article.title) else: article.set_current_language(language) self.assertNotContains(response, article.title)
def test_post_list_view_fallback(self): posts = self.get_posts() pages = self.get_pages() PARLER_FALLBACK = { 1: ( {'code': 'en'}, {'code': 'it'}, {'code': 'fr', 'hide_untranslated': True}, ), 'default': { 'fallback': 'en', 'hide_untranslated': False, } } with smart_override('fr'): view_obj = PostListView() request = self.get_page_request(pages[1], self.user, lang='fr', edit=True) view_obj.request = request view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 3) PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK) with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK): view_obj = PostListView() request = self.get_page_request(pages[1], self.user, lang='fr', edit=True) view_obj.request = request view_obj.namespace, view_obj.config = get_app_instance(request) view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 0)
def test_index_multilingual(self): self.index = ArticleIndex() content0 = self.rand_str(prefix='content0_') self.setup_categories() article_1 = self.create_article( content=content0, lead_in=u'lead in text', title=u'a title') article_2 = self.create_article( content=content0, lead_in=u'lead in text', title=u'second title') for article in (article_1, article_2): for tag_name in ('tag 1', 'tag2'): article.tags.add(tag_name) for category in (self.category1, self.category2): article.categories.add(category) with switch_language(article_2, 'de'): article_2.title = u'de title' article_2.lead_in = u'de lead in' article_2.save() LANGUAGES = add_default_language_settings(PARLER_LANGUAGES_HIDE) with override_parler_settings(PARLER_LANGUAGES=LANGUAGES): with smart_override('de'): language = get_current_language() # english-only article is excluded qs = self.index.index_queryset(language) self.assertEqual(qs.count(), 1) self.assertEqual( qs.translated(language, title__icontains='title').count(), 1 ) # the language is correctly setup for article_de in qs: self.assertEqual( self.index.get_title(article_de), 'de title') self.assertEqual( self.index.get_description(article_de), 'de lead in')
def test_post_list_view_fallback(self): page1, page2 = self.get_pages() post1, post2 = self.get_posts() PARLER_FALLBACK = { 1: ( {'code': 'en'}, {'code': 'it'}, {'code': 'fr', 'hide_untranslated': True}, ), 'default': { 'fallback': 'en', 'hide_untranslated': False, } } view_obj = PostListView() request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True) activate('fr') view_obj.request = request view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 2) PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK) with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK): view_obj = PostListView() request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True) activate('fr') view_obj.request = request view_obj.kwargs = {} view_obj.object_list = view_obj.get_queryset() view_obj.get_context_data(object_list=view_obj.object_list) self.assertEqual(view_obj.get_queryset().count(), 0)
""" Overview of all settings which can be customized. """ from django.conf import settings from parler.utils import normalize_language_code from parler.utils.conf import add_default_language_settings PARLER_DEFAULT_LANGUAGE_CODE = getattr(settings, 'PARLER_DEFAULT_LANGUAGE_CODE', settings.LANGUAGE_CODE) PARLER_SHOW_EXCLUDED_LANGUAGE_TABS = getattr(settings, 'PARLER_SHOW_EXCLUDED_LANGUAGE_TABS', False) PARLER_LANGUAGES = getattr(settings, 'PARLER_LANGUAGES', {}) PARLER_ENABLE_CACHING = getattr(settings, 'PARLER_ENABLE_CACHING', True) # Cleanup settings PARLER_DEFAULT_LANGUAGE_CODE = normalize_language_code(PARLER_DEFAULT_LANGUAGE_CODE) PARLER_LANGUAGES = add_default_language_settings( PARLER_LANGUAGES, code=PARLER_DEFAULT_LANGUAGE_CODE, fallback=PARLER_DEFAULT_LANGUAGE_CODE )
""" from django.conf import settings from parler.utils import ( get_parler_languages_from_django_cms, normalize_language_code, ) from parler.utils.conf import add_default_language_settings PARLER_DEFAULT_LANGUAGE_CODE = getattr(settings, 'PARLER_DEFAULT_LANGUAGE_CODE', settings.LANGUAGE_CODE) PARLER_SHOW_EXCLUDED_LANGUAGE_TABS = getattr(settings, 'PARLER_SHOW_EXCLUDED_LANGUAGE_TABS', False) PARLER_LANGUAGES = getattr(settings, 'PARLER_LANGUAGES', {}) if not PARLER_LANGUAGES: if hasattr(settings, 'CMS_LANGUAGES'): PARLER_LANGUAGES = get_parler_languages_from_django_cms( getattr(settings, 'CMS_LANGUAGES')) PARLER_ENABLE_CACHING = getattr(settings, 'PARLER_ENABLE_CACHING', True) # Have to fill the default section explicitly to avoid circular imports PARLER_LANGUAGES.setdefault('default', {}) PARLER_LANGUAGES['default'].setdefault('code', PARLER_DEFAULT_LANGUAGE_CODE) PARLER_LANGUAGES['default'].setdefault('fallbacks', [PARLER_DEFAULT_LANGUAGE_CODE]) # Cleanup settings PARLER_DEFAULT_LANGUAGE_CODE = normalize_language_code(PARLER_DEFAULT_LANGUAGE_CODE) PARLER_LANGUAGES = add_default_language_settings(PARLER_LANGUAGES)
PARLER_DEFAULT_LANGUAGE_CODE = getattr(settings, 'PARLER_DEFAULT_LANGUAGE_CODE', settings.LANGUAGE_CODE) PARLER_SHOW_EXCLUDED_LANGUAGE_TABS = getattr( settings, 'PARLER_SHOW_EXCLUDED_LANGUAGE_TABS', False) PARLER_LANGUAGES = getattr(settings, 'PARLER_LANGUAGES', {}) if not PARLER_LANGUAGES: if hasattr(settings, 'CMS_LANGUAGES'): PARLER_LANGUAGES = get_parler_languages_from_django_cms( getattr(settings, 'CMS_LANGUAGES')) PARLER_ENABLE_CACHING = getattr(settings, 'PARLER_ENABLE_CACHING', True) # Have to fill the default section explicitly to avoid circular imports PARLER_LANGUAGES.setdefault('default', {}) PARLER_LANGUAGES['default'].setdefault('code', PARLER_DEFAULT_LANGUAGE_CODE) PARLER_LANGUAGES['default'].setdefault('fallbacks', [PARLER_DEFAULT_LANGUAGE_CODE]) # Cleanup settings PARLER_DEFAULT_LANGUAGE_CODE = normalize_language_code( PARLER_DEFAULT_LANGUAGE_CODE) PARLER_LANGUAGES = add_default_language_settings(PARLER_LANGUAGES) # Activate translations by default. Flag to compensate for Django >= 1.8 default `get_language` behavior PARLER_DEFAULT_ACTIVATE = getattr(settings, 'PARLER_DEFAULT_ACTIVATE', False)
""" Overview of all settings which can be customized. """ from django.conf import settings from parler.utils import normalize_language_code from parler.utils.conf import add_default_language_settings PARLER_DEFAULT_LANGUAGE_CODE = getattr(settings, 'PARLER_DEFAULT_LANGUAGE_CODE', settings.LANGUAGE_CODE) PARLER_SHOW_EXCLUDED_LANGUAGE_TABS = getattr( settings, 'PARLER_SHOW_EXCLUDED_LANGUAGE_TABS', False) PARLER_LANGUAGES = getattr(settings, 'PARLER_LANGUAGES', {}) PARLER_ENABLE_CACHING = getattr(settings, 'PARLER_ENABLE_CACHING', True) # Cleanup settings PARLER_DEFAULT_LANGUAGE_CODE = normalize_language_code( PARLER_DEFAULT_LANGUAGE_CODE) PARLER_LANGUAGES = add_default_language_settings( PARLER_LANGUAGES, code=PARLER_DEFAULT_LANGUAGE_CODE, fallback=PARLER_DEFAULT_LANGUAGE_CODE)