From 446b4b83e97673d9392c3fad15e638282f69d0f2 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Mon, 2 Jan 2017 00:09:25 +0100 Subject: [PATCH 1/2] Fix post language url --- djangocms_blog/models.py | 4 ++-- tests/test_models.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 706be47..44b3545 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -234,10 +234,10 @@ class Post(KnockerModel, ModelMeta, TranslatableModel): 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 = self.get_current_language() 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() with switch_language(self, lang): category = self.categories.first() kwargs = {} diff --git a/tests/test_models.py b/tests/test_models.py index a03dd1e..c5aeb52 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -608,6 +608,20 @@ class ModelsTest(BaseTest): post.app_config = self.app_config_1 self.assertTrue(re.match(r'.*/%s/$' % post.slug, post.get_absolute_url())) + def test_url_language(self): + self.get_pages() + post = self._get_post(self._post_data[0]['en']) + post = self._get_post(self._post_data[0]['it'], post, 'it') + + with override('it'): + self.assertEqual(post.get_current_language(), 'en') + self.assertEqual(post.get_absolute_url(), post.get_absolute_url('it')) + + post.set_current_language('it') + with override('en'): + self.assertEqual(post.get_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']) From 6a4218eeeafd9c4564df695c7255bb011d5c27c3 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Mon, 2 Jan 2017 00:09:34 +0100 Subject: [PATCH 2/2] Fix code style --- cms_helper.py | 5 ++++- djangocms_blog/admin.py | 1 + djangocms_blog/cms_appconfig.py | 2 ++ djangocms_blog/cms_apps.py | 4 +++- djangocms_blog/cms_menus.py | 5 +++-- djangocms_blog/urls.py | 1 + 6 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cms_helper.py b/cms_helper.py index ad35c1b..ae6b887 100755 --- a/cms_helper.py +++ b/cms_helper.py @@ -7,7 +7,9 @@ import os from tempfile import mkdtemp -def gettext(s): return s +def gettext(s): + return s + HELPER_SETTINGS = dict( ROOT_URLCONF='tests.test_utils.urls', @@ -138,5 +140,6 @@ def run(): from djangocms_helper import runner runner.cms('djangocms_blog') + if __name__ == '__main__': run() diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index 5d8a799..c3b09ae 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -308,6 +308,7 @@ class BlogConfigAdmin(BaseAppHookConfig, TranslatableAdmin): menu_pool.clear(all=True) return super(BlogConfigAdmin, self).save_model(request, obj, form, change) + admin.site.register(BlogCategory, BlogCategoryAdmin) admin.site.register(Post, PostAdmin) admin.site.register(BlogConfig, BlogConfigAdmin) diff --git a/djangocms_blog/cms_appconfig.py b/djangocms_blog/cms_appconfig.py index e7e028d..29a4c9c 100644 --- a/djangocms_blog/cms_appconfig.py +++ b/djangocms_blog/cms_appconfig.py @@ -134,4 +134,6 @@ class BlogConfigForm(AppDataForm): label=_('Send notifications on post update'), required=False, initial=False, help_text=_('Emits a desktop notification -if enabled- when editing a published post') ) + + setup_config(BlogConfigForm, BlogConfig) diff --git a/djangocms_blog/cms_apps.py b/djangocms_blog/cms_apps.py index 62f8780..7cfdc05 100644 --- a/djangocms_blog/cms_apps.py +++ b/djangocms_blog/cms_apps.py @@ -11,6 +11,7 @@ from .menu import BlogCategoryMenu from .settings import get_setting +@apphook_pool.register class BlogApp(AutoCMSAppMixin, CMSConfigApp): name = _('Blog') urls = ['djangocms_blog.urls'] @@ -28,5 +29,6 @@ class BlogApp(AutoCMSAppMixin, CMSConfigApp): 'object_name': get_setting('DEFAULT_OBJECT_NAME') }, } -apphook_pool.register(BlogApp) + + BlogApp.setup() diff --git a/djangocms_blog/cms_menus.py b/djangocms_blog/cms_menus.py index bfdf74d..2e61642 100644 --- a/djangocms_blog/cms_menus.py +++ b/djangocms_blog/cms_menus.py @@ -104,8 +104,6 @@ class BlogCategoryMenu(CMSAttachMenu): return nodes -menu_pool.register_menu(BlogCategoryMenu) - class BlogNavModifier(Modifier): """ @@ -159,7 +157,9 @@ class BlogNavModifier(Modifier): node.selected = True return nodes + menu_pool.register_modifier(BlogNavModifier) +menu_pool.register_menu(BlogCategoryMenu) def clear_menu_cache(**kwargs): @@ -168,5 +168,6 @@ def clear_menu_cache(**kwargs): """ menu_pool.clear(all=True) + post_save.connect(clear_menu_cache, sender=BlogCategory) post_delete.connect(clear_menu_cache, sender=BlogCategory) diff --git a/djangocms_blog/urls.py b/djangocms_blog/urls.py index 84b9db2..2d68439 100644 --- a/djangocms_blog/urls.py +++ b/djangocms_blog/urls.py @@ -20,6 +20,7 @@ def get_urls(): ) return details + detail_urls = get_urls() urlpatterns = [