Skip to content

Commit

Permalink
Merge pull request #3912 from pgcd/allow-disabling-toolbar
Browse files Browse the repository at this point in the history
Allow disabling toolbar
  • Loading branch information
yakky committed Mar 1, 2015
2 parents c52ce48 + f733029 commit 1ead1d5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
5 changes: 5 additions & 0 deletions cms/cms_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
ADD_PAGE_LANGUAGE_BREAK = "Add page language Break"
REMOVE_PAGE_LANGUAGE_BREAK = "Remove page language Break"
COPY_PAGE_LANGUAGE_BREAK = "Copy page language Break"
TOOLBAR_DISABLE_BREAK = 'Toolbar disable Break'


@toolbar_pool.register
Expand Down Expand Up @@ -133,6 +134,10 @@ def add_admin_menu(self):
admin_menu.add_sideframe_item(_('User settings'), url=admin_reverse('cms_usersettings_change'))
admin_menu.add_break(USER_SETTINGS_BREAK)

# Disable toolbar
admin_menu.add_link_item(_('Disable toolbar'), url='?%s' % get_cms_setting('CMS_TOOLBAR_URL__DISABLE'))
admin_menu.add_break(TOOLBAR_DISABLE_BREAK)

# logout
self.add_logout_button(admin_menu)

Expand Down
6 changes: 6 additions & 0 deletions cms/middleware/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def process_request(self, request):
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')
disable = get_cms_setting('CMS_TOOLBAR_URL__DISABLE')

if disable in request.GET:
request.session['cms_toolbar_disabled'] = True
if edit_on in request.GET: # If we actively enter edit mode, we should show the toolbar in any case
request.session['cms_toolbar_disabled'] = False

if request.user.is_staff or request.user.is_anonymous():
if edit_on in request.GET and not request.session.get('cms_edit', False):
Expand Down
27 changes: 23 additions & 4 deletions cms/tests/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


class ToolbarTestBase(CMSTestCase):
def get_page_request(self, page, user, path=None, edit=False, lang_code='en'):
def get_page_request(self, page, user, path=None, edit=False, lang_code='en', disable=False):
path = path or page and page.get_absolute_url()
if edit:
path += '?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')
Expand All @@ -46,6 +46,8 @@ def get_page_request(self, page, user, path=None, edit=False, lang_code='en'):
request.GET = {'edit': None}
else:
request.GET = {'edit_off': None}
if disable:
request.GET[get_cms_setting('CMS_TOOLBAR_URL__DISABLE')] = None
request.current_page = page
mid = ToolbarMiddleware()
mid.process_request(request)
Expand Down Expand Up @@ -90,7 +92,7 @@ def test_no_page_staff(self):
# Logo + admin-menu + logout
self.assertEqual(len(items), 2, items)
admin_items = toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER, 'Test').get_items()
self.assertEqual(len(admin_items), 7, admin_items)
self.assertEqual(len(admin_items), 9, admin_items)

def test_no_page_superuser(self):
request = self.get_page_request(None, self.get_superuser(), '/')
Expand All @@ -101,7 +103,7 @@ def test_no_page_superuser(self):
# Logo + edit-mode + admin-menu + logout
self.assertEqual(len(items), 2)
admin_items = toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER, 'Test').get_items()
self.assertEqual(len(admin_items), 8, admin_items)
self.assertEqual(len(admin_items), 10, admin_items)

def test_anon(self):
page = create_page('test', 'nav_playground.html', 'en')
Expand Down Expand Up @@ -214,6 +216,23 @@ def test_hide_toolbar_non_staff(self):
self.assertFalse(request.session.get('cms_build', True))
self.assertFalse(request.session.get('cms_edit', True))

def test_hide_toolbar_disabled(self):
page = create_page("toolbar-page", "nav_playground.html", "en",
published=True)
# Edit mode should re-enable the toolbar in any case
request = self.get_page_request(page, self.get_staff(), edit=False, disable=True)
self.assertTrue(request.session.get('cms_toolbar_disabled'))
toolbar = CMSToolbar(request)
self.assertFalse(toolbar.show_toolbar)

def test_show_disabled_toolbar_with_edit(self):
page = create_page("toolbar-page", "nav_playground.html", "en",
published=True)
request = self.get_page_request(page, self.get_staff(), edit=True, disable=True)
self.assertFalse(request.session.get('cms_toolbar_disabled'))
toolbar = CMSToolbar(request)
self.assertTrue(toolbar.show_toolbar)

def test_show_toolbar_login_anonymous(self):
create_page("toolbar-page", "nav_playground.html", "en", published=True)
response = self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
Expand Down Expand Up @@ -292,7 +311,7 @@ def test_no_change_button(self):
# Logo + page-menu + admin-menu + logout
self.assertEqual(len(items), 3, items)
admin_items = toolbar.get_or_create_menu(ADMIN_MENU_IDENTIFIER, 'Test').get_items()
self.assertEqual(len(admin_items), 7, admin_items)
self.assertEqual(len(admin_items), 9, admin_items)

def test_button_consistency_staff(self):
"""
Expand Down
3 changes: 3 additions & 0 deletions cms/toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ def __init__(self, request):
self.edit_mode = self.is_staff and self.request.session.get('cms_edit', False)
self.edit_mode_url_on = get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')
self.edit_mode_url_off = get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF')
self.disable_url = get_cms_setting('CMS_TOOLBAR_URL__DISABLE')
self.build_mode = self.is_staff and self.request.session.get('cms_build', False)
self.use_draft = self.is_staff and self.edit_mode or self.build_mode
self.show_toolbar = self.is_staff or self.request.session.get('cms_edit', False)
if self.request.session.get('cms_toolbar_disabled', False):
self.show_toolbar = False
self.obj = None
self.redirect_url = None
if settings.USE_I18N:
Expand Down
7 changes: 7 additions & 0 deletions cms/utils/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def wrapper():
'TOOLBAR_URL__EDIT_ON': 'edit',
'TOOLBAR_URL__EDIT_OFF': 'edit_off',
'TOOLBAR_URL__BUILD': 'build',
'TOOLBAR_URL__DISABLE': 'toolbar_off',
'ADMIN_NAMESPACE': 'admin',
}

Expand Down Expand Up @@ -96,6 +97,11 @@ def get_toolbar_url__build():
return get_cms_setting('TOOLBAR_URL__BUILD')


@default('CMS_TOOLBAR_URL__DISABLE')
def get_toolbar_url__disable():
return get_cms_setting('TOOLBAR_URL__DISABLE')


def get_templates():
from cms.utils.django_load import load_from_file
if getattr(settings, 'CMS_TEMPLATES_DIR', False):
Expand Down Expand Up @@ -240,6 +246,7 @@ def get_unihandecode_host():
'CMS_TOOLBAR_URL__EDIT_ON': get_toolbar_url__edit_on,
'CMS_TOOLBAR_URL__EDIT_OFF': get_toolbar_url__edit_off,
'CMS_TOOLBAR_URL__BUILD': get_toolbar_url__build,
'CMS_TOOLBAR_URL__DISABLE': get_toolbar_url__disable,
}


Expand Down
20 changes: 10 additions & 10 deletions docs/how_to/placeholders.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ then append ``?edit`` to the page's URL.
This will make the frontend editor top banner appear, and will eventually
require you to login.

If you need change ``?edit`` to custom string (eq: ``?admin_on``) you may
set ``CMS_TOOLBAR_URL__EDIT_ON`` variable in yours ``settings.py`` to
If you need to change ``?edit`` to custom string (eq: ``?admin_on``) you may
set ``CMS_TOOLBAR_URL__EDIT_ON`` variable in your ``settings.py`` to
``"admin_on"``.

Also you may change ``?edit_off`` or ``?build`` to custom string with
set ``CMS_TOOLBAR_URL__EDIT_OFF`` or ``CMS_TOOLBAR_URL__BUILD`` variables
in yours ``settings.py``.
Also you may change ``?edit_off``, ``?build`` and ``?toolbar_off`` to custom
string with set ``CMS_TOOLBAR_URL__EDIT_OFF``, ``CMS_TOOLBAR_URL__BUILD``
and ``CMS_TOOLBAR_URL__DISABLE``variables in yours ``settings.py``.

Notice: when you changing ``CMS_TOOLBAR_URL__EDIT_ON`` or
``CMS_TOOLBAR_URL__EDIT_OFF`` or ``CMS_TOOLBAR_URL__BUILD`` please be
careful because you may replace reserved strings in system (eq:
``?page``). We recommended you use unique strings for this option
(eq: ``secret_admin`` or ``company_name``).
Notice: when changing ``CMS_TOOLBAR_URL__EDIT_ON``,
``CMS_TOOLBAR_URL__EDIT_OFF``, ``CMS_TOOLBAR_URL__BUILD`` or
``CMS_TOOLBAR_URL__DISABLE``please be careful because you may replace
reserved strings in system (eq: ``?page``). We recommended you use unique
strings for this option (eq: ``secret_admin`` or ``company_name``).

You are now using the so-called *frontend edit mode*:

Expand Down
3 changes: 3 additions & 0 deletions docs/user/reference/page_admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Several options in this menu open up administration controls in the side-frame:
* *Administration ...* takes you to the site-wide administration panel
* *User settings ...* allows you to switch the language of the admin interface
and toolbar
* *Disable toolbar* allows you to completely disable the toolbar and front-end
editing, regardless of login and staff status. To reactivate them, you need
to enter *edit mode* either manually or through the backend administration.

You can also *Logout* from this menu.

Expand Down

0 comments on commit 1ead1d5

Please sign in to comment.