diff --git a/cms/middleware/toolbar.py b/cms/middleware/toolbar.py index 3385ab9b135..6ff2e5f5544 100644 --- a/cms/middleware/toolbar.py +++ b/cms/middleware/toolbar.py @@ -2,14 +2,16 @@ """ Edit Toolbar middleware """ -from cms.utils.conf import get_cms_setting -from cms.toolbar.toolbar import CMSToolbar -from cms.utils.i18n import force_language from django.contrib.admin.models import LogEntry, ADDITION, CHANGE -from menus.menu_pool import menu_pool +from django.core.urlresolvers import resolve from django.http import HttpResponse from django.template.loader import render_to_string + +from cms.toolbar.toolbar import CMSToolbar +from cms.utils.conf import get_cms_setting +from cms.utils.i18n import force_language from cms.utils.placeholder import get_toolbar_plugin_struct +from menus.menu_pool import menu_pool def toolbar_plugin_processor(instance, placeholder, rendered_content, original_context): @@ -50,12 +52,30 @@ class ToolbarMiddleware(object): Middleware to set up CMS Toolbar. """ + def is_cms_request(self,request): + cms_app_name = get_cms_setting('APP_NAME') + toolbar_hide = get_cms_setting('TOOLBAR_HIDE') + + if not toolbar_hide or not cms_app_name: + return True + + try: + match = resolve(request.path_info) + except: + return False + + return match.app_name == cms_app_name + + def process_request(self, request): """ If we should show the toolbar for this request, put it on request.toolbar. Then call the request_hook on the toolbar. """ + if not self.is_cms_request(request): + return + edit_on = get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON') edit_off = get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF') build = get_cms_setting('CMS_TOOLBAR_URL__BUILD') @@ -96,11 +116,17 @@ def process_request(self, request): request.toolbar = CMSToolbar(request) def process_view(self, request, view_func, view_args, view_kwarg): + if not self.is_cms_request(request): + return + response = request.toolbar.request_hook() if isinstance(response, HttpResponse): return response def process_response(self, request, response): + if not self.is_cms_request(request): + return response + from django.utils.cache import add_never_cache_headers if ((hasattr(request, 'toolbar') and request.toolbar.edit_mode) or diff --git a/cms/test_utils/project/nonroot_urls.py b/cms/test_utils/project/nonroot_urls.py index 8ce065794a8..fdc3b66e942 100644 --- a/cms/test_utils/project/nonroot_urls.py +++ b/cms/test_utils/project/nonroot_urls.py @@ -15,9 +15,10 @@ urlpatterns += i18n_patterns('', url(r'^admin/', include(admin.site.urls)), - url(r'^content/', include('cms.urls')), + url(r'^content/', include('cms.urls', app_name=get_cms_setting('APP_NAME'))), ) + if settings.DEBUG and is_installed('debug_toolbar'): import debug_toolbar urlpatterns += [ diff --git a/cms/tests/toolbar.py b/cms/tests/toolbar.py index 635cedc154f..5294af82dcb 100644 --- a/cms/tests/toolbar.py +++ b/cms/tests/toolbar.py @@ -56,7 +56,8 @@ def get_page_request(self, page, user, path=None, edit=False, lang_code='en', di request.current_page = page mid = ToolbarMiddleware() mid.process_request(request) - request.toolbar.populate() + if hasattr(request,'toolbar'): + request.toolbar.populate() return request def get_anon(self): @@ -90,6 +91,36 @@ def _fake_logentry(self, instance_id, user, text, model=Page): session.save() +@override_settings(ROOT_URLCONF='cms.test_utils.project.nonroot_urls') +class ToolbarMiddlewareTest(ToolbarTestBase): + + def test_no_app_setted_show_toolbar_in_non_cms_urls(self): + request = self.get_page_request(None, self.get_anon(), '/') + self.assertTrue(hasattr(request,'toolbar')) + + def test_no_app_setted_show_toolbar_in_cms_urls(self): + page = create_page('foo','col_two.html','en',published=True) + request = self.get_page_request(page, self.get_anon()) + self.assertTrue(hasattr(request,'toolbar')) + + @override_settings(CMS_APP_NAME='cms') + def test_app_setted_hide_toolbar_in_non_cms_urls_toolbar_hide_unsetted(self): + request = self.get_page_request(None, self.get_anon(), '/') + self.assertTrue(hasattr(request,'toolbar')) + + @override_settings(CMS_APP_NAME='cms') + @override_settings(CMS_TOOLBAR_HIDE=True) + def test_app_setted_hide_toolbar_in_non_cms_urls(self): + request = self.get_page_request(None, self.get_anon(), '/') + self.assertFalse(hasattr(request,'toolbar')) + + @override_settings(CMS_APP_NAME='cms') + def test_app_setted_show_toolbar_in_cms_urls(self): + page = create_page('foo','col_two.html','en',published=True) + request = self.get_page_request(page, self.get_anon()) + self.assertTrue(hasattr(request,'toolbar')) + + @override_settings(CMS_PERMISSION=False) class ToolbarTests(ToolbarTestBase): diff --git a/cms/utils/conf.py b/cms/utils/conf.py index 6cd386a000e..397e5ed1b45 100644 --- a/cms/utils/conf.py +++ b/cms/utils/conf.py @@ -61,6 +61,8 @@ def wrapper(): 'TOOLBAR_URL__BUILD': 'build', 'TOOLBAR_URL__DISABLE': 'toolbar_off', 'ADMIN_NAMESPACE': 'admin', + 'APP_NAME':None, + 'TOOLBAR_HIDE':False } diff --git a/docs/reference/configuration.rst b/docs/reference/configuration.rst index 8e50ef423a6..7c826989699 100644 --- a/docs/reference/configuration.rst +++ b/docs/reference/configuration.rst @@ -916,6 +916,23 @@ a login form when ``?edit`` is appended to a URL. The default behaviour is to show the toolbar to anonymous users. +CMS_TOOLBAR_HIDE +================ + +default + ``False`` + +If True, the toolbar is hidden in the pages out django CMS. +To determine the internal url of django cms, you need to assign CMS_APP_NAME to use when you include ``'cms.urls'`` + +Example:: + + urlpatterns += i18n_patterns('', + url(r'^admin/', include(admin.site.urls)), + url(r'^content/', include('cms.urls', app_name=settings.CMS_APP_NAME)), + ) + + CMS_DEFAULT_X_FRAME_OPTIONS ===========================