From 3e83bc8b7219ea9a445ff2c4b5689c61a33ea0d2 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Tue, 6 Feb 2018 10:02:02 +0100 Subject: [PATCH] Add apphook config urlconf to settings --- djangocms_blog/cms_apps.py | 7 ++++-- djangocms_blog/settings.py | 1 + docs/features.rst | 16 +++++++++++++ docs/settings.rst | 1 + tests/test_utils/blog_urls.py | 45 +++++++++++++++++++++++++++++++++++ tests/test_views.py | 17 +++++++++++++ 6 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 tests/test_utils/blog_urls.py diff --git a/djangocms_blog/cms_apps.py b/djangocms_blog/cms_apps.py index 29b9b78..3c452ed 100644 --- a/djangocms_blog/cms_apps.py +++ b/djangocms_blog/cms_apps.py @@ -14,7 +14,7 @@ from .settings import get_setting @apphook_pool.register class BlogApp(AutoCMSAppMixin, CMSConfigApp): name = _('Blog') - _urls = ['djangocms_blog.urls'] + _urls = [get_setting('URLCONF')] app_name = 'djangocms_blog' app_config = BlogConfig _menus = [BlogCategoryMenu] @@ -30,9 +30,12 @@ class BlogApp(AutoCMSAppMixin, CMSConfigApp): }, } + def get_urls(self, page=None, language=None, **kwargs): + return [get_setting('URLCONF')] + @property def urls(self): - return self._urls + return self.get_urls() @property def menus(self): diff --git a/djangocms_blog/settings.py b/djangocms_blog/settings.py index 7c8b34d..5588eac 100644 --- a/djangocms_blog/settings.py +++ b/djangocms_blog/settings.py @@ -53,6 +53,7 @@ def get_setting(name): 'upscale': False }), + 'BLOG_URLCONF': getattr(settings, 'BLOG_URLCONF', 'djangocms_blog.urls'), 'BLOG_PAGINATION': getattr(settings, 'BLOG_PAGINATION', 10), 'BLOG_LATEST_POSTS': getattr(settings, 'BLOG_LATEST_POSTS', 5), 'BLOG_POSTS_LIST_TRUNCWORDS_COUNT': getattr( diff --git a/docs/features.rst b/docs/features.rst index 553c524..92be15c 100644 --- a/docs/features.rst +++ b/docs/features.rst @@ -31,6 +31,22 @@ Notice that the last permalink type is no longer present. Then, pick any of the three remaining permalink types in the layout section of the apphooks config linked ot the home page (see http://yoursite.com/admin/djangocms_blog/blogconfig/).' +.. _blog-custom-urlconf: + +************************ +Provide a custom URLConf +************************ + +It's possible to completely customize the urlconf by setting ``BLOG_URLCONF`` to the dotted path of +the new urlconf. + +Example:: + + BLOG_URLCONF = 'my_project.blog_urls.py' + +The custom urlconf can be created by copying the existing urlconf in `djangocms_blog/urls.py`, +saving it to a new file `my_project.blog_urls.py` and editing it according to the custom needs. + .. _multisite: diff --git a/docs/settings.rst b/docs/settings.rst index 13ddcaf..ed42f8e 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -68,6 +68,7 @@ Global Settings * BLOG_AVAILABLE_PERMALINK_STYLES: Choices of permalinks styles; * BLOG_PERMALINK_URLS: URLConf corresponding to BLOG_AVAILABLE_PERMALINK_STYLES; +* BLOG_URLCONF: Apphoo URLConf; (default: ``'djangocms_blog.urls'``); * BLOG_DEFAULT_OBJECT_NAME: Default name for Blog item (used in django CMS Wizard); * BLOG_AUTO_SETUP: Enable the blog **Auto setup** feature; (default: ``True``) * BLOG_AUTO_HOME_TITLE: Title of the home page created by **Auto setup**; diff --git a/tests/test_utils/blog_urls.py b/tests/test_utils/blog_urls.py new file mode 100644 index 0000000..40572e2 --- /dev/null +++ b/tests/test_utils/blog_urls.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, print_function, unicode_literals + +from django.conf.urls import url + +from djangocms_blog.feeds import FBInstantArticles, LatestEntriesFeed, TagFeed +from djangocms_blog.settings import get_setting +from djangocms_blog.views import ( + AuthorEntriesView, CategoryEntriesView, PostArchiveView, PostDetailView, PostListView, + TaggedListView, +) + + +def get_urls(): + urls = get_setting('PERMALINK_URLS') + details = [] + for urlconf in urls.values(): + details.append( + url(urlconf, PostDetailView.as_view(), name='post-detail'), + ) + return details + + +detail_urls = get_urls() + +urlpatterns = [ + url(r'^latests/$', + PostListView.as_view(), name='posts-latest'), + url(r'^feed/$', + LatestEntriesFeed(), name='posts-latest-feed'), + url(r'^feed/fb/$', + FBInstantArticles(), name='posts-latest-feed-fb'), + url(r'^(?P\d{4})/$', + PostArchiveView.as_view(), name='posts-archive'), + url(r'^(?P\d{4})/(?P\d{1,2})/$', + PostArchiveView.as_view(), name='posts-archive'), + url(r'^author/(?P[\w\.@+-]+)/$', + AuthorEntriesView.as_view(), name='posts-author'), + url(r'^category/(?P[\w\.@+-]+)/$', + CategoryEntriesView.as_view(), name='posts-category'), + url(r'^tag/(?P[-\w]+)/$', + TaggedListView.as_view(), name='posts-tagged'), + url(r'^tag/(?P[-\w]+)/feed/$', + TagFeed(), name='posts-tagged-feed'), +] + detail_urls diff --git a/tests/test_views.py b/tests/test_views.py index 3f94b98..e1a681b 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -2,21 +2,25 @@ from __future__ import absolute_import, print_function, unicode_literals import os.path +import sys from aldryn_apphooks_config.utils import get_app_instance from cms.api import add_plugin +from cms.appresolver import APP_RESOLVERS, get_app_patterns from cms.toolbar.items import ModalItem from cms.utils.apphook_reload import reload_urlconf from django.contrib.auth.models import AnonymousUser from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import reverse from django.http import Http404 +from django.test import override_settings from django.utils.timezone import now from django.utils.translation import ugettext_lazy as _ from parler.tests.utils import override_parler_settings from parler.utils.conf import add_default_language_settings from parler.utils.context import smart_override, switch_language +from djangocms_blog.cms_appconfig import BlogConfig from djangocms_blog.feeds import FBInstantArticles, FBInstantFeed, LatestEntriesFeed, TagFeed from djangocms_blog.models import BLOG_CURRENT_NAMESPACE from djangocms_blog.settings import get_setting @@ -31,6 +35,19 @@ from .base import BaseTest class ViewTest(BaseTest): + @override_settings(BLOG_URLCONF='tests.test_utils.blog_urls') + def test_post_list_view_custom_urlconf(self): + pages = self.get_pages() + self.get_posts() + self.get_request(pages[1], 'en', AnonymousUser()) + self.assertEqual(reverse('sample_app:posts-latest'), '/en/page-two/latests/') + + def test_post_list_view_base_urlconf(self): + pages = self.get_pages() + self.get_posts() + self.get_request(pages[1], 'en', AnonymousUser()) + self.assertEqual(reverse('sample_app:posts-latest'), '/en/page-two/') + def test_post_list_view(self): pages = self.get_pages() posts = self.get_posts()