Skip to content

Commit

Permalink
Merge pull request django-cms#4192 from yakky/merge/4056
Browse files Browse the repository at this point in the history
Merge/4056
  • Loading branch information
yakky committed Jun 13, 2015
2 parents 86627ee + 40018ce commit f8d4753
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
34 changes: 30 additions & 4 deletions cms/middleware/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cms/test_utils/project/nonroot_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += [
Expand Down
33 changes: 32 additions & 1 deletion cms/tests/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):

Expand Down
2 changes: 2 additions & 0 deletions cms/utils/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def wrapper():
'TOOLBAR_URL__BUILD': 'build',
'TOOLBAR_URL__DISABLE': 'toolbar_off',
'ADMIN_NAMESPACE': 'admin',
'APP_NAME':None,
'TOOLBAR_HIDE':False
}


Expand Down
17 changes: 17 additions & 0 deletions docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========================

Expand Down

0 comments on commit f8d4753

Please sign in to comment.