Skip to content

Commit

Permalink
Merge pull request #429 from nephila/feature/urlconf_settings
Browse files Browse the repository at this point in the history
Add apphook config urlconf to settings
  • Loading branch information
yakky authored Feb 19, 2018
2 parents 958d392 + 3e83bc8 commit ae4b930
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
7 changes: 5 additions & 2 deletions djangocms_blog/cms_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@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]
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions djangocms_blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions docs/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**;
Expand Down
45 changes: 45 additions & 0 deletions tests/test_utils/blog_urls.py
Original file line number Diff line number Diff line change
@@ -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<year>\d{4})/$',
PostArchiveView.as_view(), name='posts-archive'),
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$',
PostArchiveView.as_view(), name='posts-archive'),
url(r'^author/(?P<username>[\w\.@+-]+)/$',
AuthorEntriesView.as_view(), name='posts-author'),
url(r'^category/(?P<category>[\w\.@+-]+)/$',
CategoryEntriesView.as_view(), name='posts-category'),
url(r'^tag/(?P<tag>[-\w]+)/$',
TaggedListView.as_view(), name='posts-tagged'),
url(r'^tag/(?P<tag>[-\w]+)/feed/$',
TagFeed(), name='posts-tagged-feed'),
] + detail_urls
17 changes: 17 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +35,19 @@

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()
Expand Down

0 comments on commit ae4b930

Please sign in to comment.