Skip to content

Commit

Permalink
Add option to allow fallback language in post/category URLs (nephila#572
Browse files Browse the repository at this point in the history
)

Add setting BLOG_USE_FALLBACK_LANGUAGE_IN_URL. If enabled, and a blog
post/category doesn't exist in the current language, prefer displaying the URL
in the fallback language.
  • Loading branch information
jasujm authored May 16, 2020
1 parent 464f1e9 commit ec83f5f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
History
=======

**********
Unreleased
**********

* Add BLOG_USE_FALLBACK_LANGUAGE_IN_URL setting

*******************
1.1.1 (2020-05-15)
*******************
Expand Down
27 changes: 19 additions & 8 deletions djangocms_blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ class KnockerModel:
pass


def _get_language(instance, language):
available_languages = instance.get_available_languages()
if language and language in available_languages:
return language
language = get_language()
if language and language in available_languages:
return language
language = instance.get_current_language()
if language and language in available_languages:
return language
if get_setting('USE_FALLBACK_LANGUAGE_IN_URL'):
for fallback_language in instance.get_fallback_languages():
if fallback_language in available_languages:
return fallback_language
return language


class BlogMetaMixin(ModelMeta):

def get_meta_attribute(self, param):
Expand Down Expand Up @@ -142,10 +159,7 @@ def count_all_sites(self):
return self.linked_posts.published(current_site=False).count()

def get_absolute_url(self, lang=None):
if not lang or lang not in self.get_available_languages():
lang = get_language()
if not lang or lang not in self.get_available_languages():
lang = self.get_current_language()
lang = _get_language(self, lang)
if self.has_translation(lang):
slug = self.safe_translation_getter('slug', language_code=lang)
return reverse(
Expand Down Expand Up @@ -328,10 +342,7 @@ def save_translation(self, translation, *args, **kwargs):
super(Post, self).save_translation(translation, *args, **kwargs)

def get_absolute_url(self, lang=None):
if not lang or lang not in self.get_available_languages():
lang = get_language()
if not lang or lang not in self.get_available_languages():
lang = self.get_current_language()
lang = _get_language(self, lang)
with switch_language(self, lang):
category = self.categories.first()
kwargs = {}
Expand Down
2 changes: 2 additions & 0 deletions djangocms_blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def get_setting(name):

'BLOG_PLUGIN_TEMPLATE_FOLDERS': getattr(
settings, 'BLOG_PLUGIN_TEMPLATE_FOLDERS', (('plugins', _('Default template')),)),
'BLOG_USE_FALLBACK_LANGUAGE_IN_URL': getattr(
settings, 'BLOG_USE_FALLBACK_LANGUAGE_IN_URL', False),

}
return default['BLOG_%s' % name]
1 change: 1 addition & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Global Settings
* BLOG_META_TITLE_LENGTH: Maximum length for the Meta title field (default: ``70``)
* BLOG_ABSTRACT_CKEDITOR: Configuration for the CKEditor of the abstract field (as per https://github.com/divio/djangocms-text-ckeditor/#customizing-htmlfield-editor)
* BLOG_POST_TEXT_CKEDITOR: Configuration for the CKEditor of the post content field
* BLOG_USE_FALLBACK_LANGUAGE_IN_URL: When displaying URL, prefer URL in the fallback language if an article or category is not available in the current language

******************
Read-only settings
Expand Down
28 changes: 28 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from django.utils.timezone import now
from django.utils.translation import get_language, override
from menus.menu_pool import menu_pool
from parler.tests.utils import override_parler_settings
from parler.utils.conf import add_default_language_settings
from parler.utils.context import smart_override
from taggit.models import Tag

Expand Down Expand Up @@ -978,6 +980,32 @@ def test_url_language(self):
self.assertEqual(post.get_current_language(), 'it')
self.assertEqual(post.get_absolute_url(), post.get_absolute_url('en'))

def test_url_language_use_fallback(self):
self.get_pages()
post = self._get_post(self._post_data[0]['en'])

PARLER_FALLBACK = {
1: (
{'code': 'en'},
{'code': 'it'},
),
'default': {
'fallbacks': ['fr', 'en'],
'hide_untranslated': False,
}
}
PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK)

with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK):
with override('it'):
post.set_current_language('it')
self.assertEqual(post.get_absolute_url(), post.get_absolute_url('it'))

with override_settings(BLOG_USE_FALLBACK_LANGUAGE_IN_URL=True):
with override('it'):
post.set_current_language('it')
self.assertEqual(post.get_absolute_url(), post.get_absolute_url('en'))

def test_manager(self):
self.get_pages()
post1 = self._get_post(self._post_data[0]['en'])
Expand Down

0 comments on commit ec83f5f

Please sign in to comment.