Skip to content

Commit

Permalink
Merge pull request #3069 from itcrab/feature/change-edit-variable-for…
Browse files Browse the repository at this point in the history
…-login-to-admin

Add feature change "?edit" variable (and "?edit_off", "?build") to custom
  • Loading branch information
digi604 committed May 15, 2014
2 parents 0006da5 + e602f47 commit d897cf0
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 58 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,5 @@ Please see Install/2.4 release notes *before* attempting to upgrade to version 2
- Fix in cms fix-mptt command

==== 3.0.2 ===
- Add 'as' form to render_placeholder templatetag to save the result in context
- Add 'as' form to render_placeholder templatetag to save the result in context
- Added changeable strings for "?edit", "?edit_off" and "?build" urls
10 changes: 5 additions & 5 deletions cms/admin/pageadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,11 +1099,11 @@ def publish_page(self, request, page_id, language):
path = page.get_absolute_url(language, fallback=True)
else:
public_page = Page.objects.get(publisher_public=page.pk)
path = '%s?edit_off' % public_page.get_absolute_url(language, fallback=True)
path = '%s?%s' % (public_page.get_absolute_url(language, fallback=True), get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF'))
else:
path = '%s?edit_off' % referrer
path = '%s?%s' % (referrer, get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF'))
else:
path = '/?edit_off'
path = '/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF')

return HttpResponseRedirect(path)

Expand Down Expand Up @@ -1188,7 +1188,7 @@ def revert_page(self, request, page_id, language):
referer = request.META.get('HTTP_REFERER', '')
path = '../../'
if reverse('admin:index') not in referer:
path = '%s?edit_off' % referer.split('?')[0]
path = '%s?%s' % (referer.split('?')[0], get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF'))
return HttpResponseRedirect(path)

@create_revision()
Expand Down Expand Up @@ -1293,7 +1293,7 @@ def preview_page(self, request, object_id, language):
"""Redirecting preview function based on draft_id
"""
page = get_object_or_404(Page, id=object_id)
attrs = "?edit"
attrs = "?%s" % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')
attrs += "&language=" + language
with force_language(language):
url = page.get_absolute_url(language) + attrs
Expand Down
6 changes: 3 additions & 3 deletions cms/cms_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def post_template_populate(self):
def add_structure_mode(self):
switcher = self.toolbar.add_button_list('Mode Switcher', side=self.toolbar.RIGHT,
extra_classes=['cms_toolbar-item-cms-mode-switcher'])
switcher.add_button(_("Structure"), '?build', active=self.toolbar.build_mode,
switcher.add_button(_("Structure"), '?%s' % get_cms_setting('CMS_TOOLBAR_URL__BUILD'), active=self.toolbar.build_mode,
disabled=not self.toolbar.build_mode)
switcher.add_button(_("Content"), '?edit', active=not self.toolbar.build_mode,
switcher.add_button(_("Content"), '?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'), active=not self.toolbar.build_mode,
disabled=self.toolbar.build_mode)


Expand Down Expand Up @@ -375,7 +375,7 @@ def add_page_menu(self):
)
)
current_page_menu.add_break(PAGE_MENU_FIRST_BREAK)
current_page_menu.add_link_item(_('Edit this Page'), disabled=self.toolbar.edit_mode, url='?edit')
current_page_menu.add_link_item(_('Edit this Page'), disabled=self.toolbar.edit_mode, url='?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
page_info_url = "%s?language=%s" % (
reverse('admin:cms_page_change', args=(self.page.pk,)),
self.toolbar.language
Expand Down
12 changes: 9 additions & 3 deletions cms/middleware/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
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
Expand Down Expand Up @@ -52,19 +53,24 @@ 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 'edit' in request.GET and not request.session.get('cms_edit', False):

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')

if edit_on in request.GET and not request.session.get('cms_edit', False):
if not request.session.get('cms_edit', False):
menu_pool.clear()
request.session['cms_edit'] = True
if request.session.get('cms_build', False):
request.session['cms_build'] = False
if 'edit_off' in request.GET and request.session.get('cms_edit', True):
if edit_off in request.GET and request.session.get('cms_edit', True):
if request.session.get('cms_edit', True):
menu_pool.clear()
request.session['cms_edit'] = False
if request.session.get('cms_build', False):
request.session['cms_build'] = False
if 'build' in request.GET and not request.session.get('cms_build', False):
if build in request.GET and not request.session.get('cms_build', False):
request.session['cms_build'] = True
if request.user.is_staff:
try:
Expand Down
2 changes: 1 addition & 1 deletion cms/templates/cms/toolbar/items/live_draft.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load i18n %}
<div class="cms_toolbar-item cms_toolbar-item_switch{% if not request.toolbar.edit_mode %} cms_toolbar-item_switch-active{% endif %}">
<a href="{% if request.toolbar.edit_mode %}?edit_off{% else %}?edit{% endif %}">
<a href="{% if request.toolbar.edit_mode %}?{{ request.toolbar.edit_mode_url_off }}{% else %}?{{ request.toolbar.edit_mode_url_on }}{% endif %}">
<span class="cms_toolbar-item_switch-on">{% trans "Live" %}</span>
<span class="cms_toolbar-item_switch-off">{% trans "Draft" %}</span>
</a>
Expand Down
2 changes: 1 addition & 1 deletion cms/templates/cms/toolbar/toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{% endfor %}
{% if request.toolbar.can_change %}
<div class="cms_toolbar-item cms_toolbar-item_switch{% if not request.toolbar.edit_mode %} cms_toolbar-item_switch-active{% endif %}">
<a href="{% if request.toolbar.edit_mode %}?edit_off{% else %}?edit{% endif %}">
<a href="{% if request.toolbar.edit_mode %}?{{ request.toolbar.edit_mode_url_off }}{% else %}?{{ request.toolbar.edit_mode_url_on }}{% endif %}">
<span class="cms_toolbar-item_switch-on">{% trans "Live" %}</span>
<span class="cms_toolbar-item_switch-off">{% trans "Draft" %}</span>
</a>
Expand Down
2 changes: 1 addition & 1 deletion cms/templates/cms/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<div class="wrapper">
<div class="inner">
<h1 class="logo">Installation successful!</h1>
<p>Switch to <a href="/?edit">edit mode</a>,<br />log in and start <a href="{% url "admin:cms_page_add" %}?title=Home" id="add-page">adding pages</a>.</p>
<p>Switch to <a href="/?{{ request.toolbar.edit_mode_url_on }}">edit mode</a>,<br />log in and start <a href="{% url "admin:cms_page_add" %}?title=Home" id="add-page">adding pages</a>.</p>
<p class="buttons">
<a href="//docs.django-cms.org/" class="btn" target="_blank">Documentation</a>
<a href="//www.django-cms.org/" class="btn" target="_blank">Support</a>
Expand Down
14 changes: 7 additions & 7 deletions cms/tests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def test_revert_page_redirects(self):
response = self.client.get(reverse('admin:cms_page_revert_page', args=(self.page.pk, 'en')))
self.assertEqual(response.status_code, 302)
url = response['Location']
self.assertTrue(url.endswith('?edit_off'))
self.assertTrue(url.endswith('?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF')))

def test_remove_plugin_requires_post(self):
ph = Placeholder.objects.create(slot='test')
Expand Down Expand Up @@ -789,11 +789,11 @@ def test_preview_page(self):
request = self.get_request('/?public=true')
response = self.admin_class.preview_page(request, page.pk, 'en')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], '%s?edit&language=en' % base_url)
self.assertEqual(response['Location'], '%s?%s&language=en' % (base_url, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')))
request = self.get_request()
response = self.admin_class.preview_page(request, page.pk, 'en')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], '%s?edit&language=en' % base_url)
self.assertEqual(response['Location'], '%s?%s&language=en' % (base_url, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')))
current_site = Site.objects.create(domain='django-cms.org', name='django-cms')
page.site = current_site
page.save()
Expand All @@ -802,7 +802,7 @@ def test_preview_page(self):
response = self.admin_class.preview_page(request, page.pk, 'en')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'],
'http://django-cms.org%s?edit&language=en' % base_url)
'http://django-cms.org%s?%s&language=en' % (base_url, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')))

def test_too_many_plugins_global(self):
conf = {
Expand Down Expand Up @@ -1382,17 +1382,17 @@ def test_render_edit_mode(self):
self.assertEqual(Placeholder.objects.all().count(), 4)
with self.login_user_context(user):
with self.assertNumQueries(FuzzyInt(40, 66)):
output = force_unicode(self.client.get('/en/?edit').content)
output = force_unicode(self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')).content)
self.assertIn('<b>Test</b>', output)
self.assertEqual(Placeholder.objects.all().count(), 9)
self.assertEqual(StaticPlaceholder.objects.count(), 2)
for placeholder in Placeholder.objects.all():
add_plugin(placeholder, TextPlugin, 'en', body='<b>Test</b>')
with self.assertNumQueries(FuzzyInt(40, 60)):
output = force_unicode(self.client.get('/en/?edit').content)
output = force_unicode(self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')).content)
self.assertIn('<b>Test</b>', output)
with self.assertNumQueries(FuzzyInt(18, 34)):
force_unicode(self.client.get('/en/?edit').content)
force_unicode(self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')).content)
with self.assertNumQueries(FuzzyInt(13, 15)):
force_unicode(self.client.get('/en/').content)

Expand Down
5 changes: 3 additions & 2 deletions cms/tests/apphooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
import sys
from cms.utils.conf import get_cms_setting

from django.core.urlresolvers import clear_url_caches, reverse

Expand Down Expand Up @@ -209,14 +210,14 @@ def test_get_page_for_apphook_on_preview_or_edit(self):
with self.login_user_context(superuser):
with force_language("en"):
path = reverse('sample-settings')
request = self.get_request(path + '?edit')
request = self.get_request(path + '?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
request.LANGUAGE_CODE = 'en'
attached_to_page = applications_page_check(request, path=path[1:]) # strip leading slash
response = self.client.get(path+"?edit")
self.assertContains(response, '?redirect=')
with force_language("de"):
path = reverse('sample-settings')
request = self.get_request(path + '?edit')
request = self.get_request(path + '?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
request.LANGUAGE_CODE = 'de'
attached_to_page = applications_page_check(request, path=path[1:]) # strip leading slash
self.assertEqual(attached_to_page.pk, public_page.pk)
Expand Down
15 changes: 8 additions & 7 deletions cms/tests/frontend.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import sys
import datetime
from cms.utils.conf import get_cms_setting
from cms.apphook_pool import apphook_pool
from cms.exceptions import AppAlreadyRegistered
from cms.test_utils.project.placeholderapp.models import Example1
Expand Down Expand Up @@ -162,7 +163,7 @@ def setUp(self):
def test_toolbar_login(self):
User = get_user_model()
create_page('Home', 'simple.html', 'en', published=True)
url = '%s/?edit' % self.live_server_url
url = '%s/?%s' % (self.live_server_url, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.assertTrue(User.objects.all().count(), 1)
self.driver.get(url)
self.assertRaises(NoSuchElementException, self.driver.find_element_by_class_name, 'cms_toolbar-item_logout')
Expand Down Expand Up @@ -190,7 +191,7 @@ def test_toolbar_login_view(self):
apphook=Example1App)


url = '%s/%s/?edit' % (self.live_server_url, 'apphook/detail/%s' % ex1.pk)
url = '%s/%s/?%s' % (self.live_server_url, 'apphook/detail/%s' % ex1.pk, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.driver.get(url)
username_input = self.driver.find_element_by_id("id_cms-username")
username_input.send_keys(getattr(self.user, User.USERNAME_FIELD))
Expand All @@ -214,7 +215,7 @@ def test_toolbar_login_cbv(self):
)
create_page('apphook', 'simple.html', 'en', published=True,
apphook=Example1App)
url = '%s/%s/?edit' % (self.live_server_url, 'apphook/detail/class/%s' % ex1.pk)
url = '%s/%s/?%s' % (self.live_server_url, 'apphook/detail/class/%s' % ex1.pk, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.driver.get(url)
username_input = self.driver.find_element_by_id("id_cms-username")
username_input.send_keys(getattr(self.user, User.USERNAME_FIELD))
Expand Down Expand Up @@ -285,7 +286,7 @@ def setUp(self):
super(PlaceholderBasicTests, self).setUp()

def _login(self):
url = '%s/?edit' % self.live_server_url
url = '%s/?%s' % (self.live_server_url, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.driver.get(url)

self.assertRaises(NoSuchElementException, self.driver.find_element_by_class_name, 'cms_toolbar-item_logout')
Expand All @@ -300,14 +301,14 @@ def _login(self):

def test_copy_from_language(self):
self._login()
self.driver.get('%s/it/?edit' % self.live_server_url)
self.driver.get('%s/it/?%s' % (self.live_server_url, get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON')))

# check if there are no plugins in italian version of the page

italian_plugins = self.page.placeholders.all()[0].get_plugins_list('it')
self.assertEqual(len(italian_plugins), 0)

build_button = self.driver.find_element_by_css_selector('.cms_toolbar-item-cms-mode-switcher a[href="?build"]')
build_button = self.driver.find_element_by_css_selector('.cms_toolbar-item-cms-mode-switcher a[href="?%s"]' % get_cms_setting('CMS_TOOLBAR_URL__BUILD'))
build_button.click()

submenu = self.driver.find_element_by_css_selector('.cms_dragbar .cms_submenu')
Expand Down Expand Up @@ -335,7 +336,7 @@ def test_copy_to_from_clipboard(self):
self.assertEqual(CMSPlugin.objects.count(), 1)
self._login()

build_button = self.driver.find_element_by_css_selector('.cms_toolbar-item-cms-mode-switcher a[href="?build"]')
build_button = self.driver.find_element_by_css_selector('.cms_toolbar-item-cms-mode-switcher a[href="?%s"]' % get_cms_setting('CMS_TOOLBAR_URL__BUILD'))
build_button.click()

cms_draggable = self.driver.find_element_by_css_selector('.cms_draggable:nth-child(1)')
Expand Down
2 changes: 1 addition & 1 deletion cms/tests/multilingual.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,6 @@ def test_wrong_plugin_language(self):
page.publish('en')
superuser = self.get_superuser()
with self.login_user_context(superuser):
response = self.client.get('/en/?edit')
response = self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.assertEqual(response.status_code, 200)

3 changes: 2 additions & 1 deletion cms/tests/placeholder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
import itertools
from cms.utils.conf import get_cms_setting
from cms.toolbar.toolbar import CMSToolbar
from sekizai.context import SekizaiContext
import warnings
Expand Down Expand Up @@ -590,7 +591,7 @@ def test_placeholder_pk_thousands_format(self):
user = self.get_superuser()
self.client.login(username=getattr(user, get_user_model().USERNAME_FIELD),
password=getattr(user, get_user_model().USERNAME_FIELD))
response = self.client.get("/en/?edit")
response = self.client.get("/en/?%s" % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
for placeholder in page.placeholders.all():
self.assertContains(
response, "'placeholder_id': '%s'" % placeholder.pk)
Expand Down
3 changes: 2 additions & 1 deletion cms/tests/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import with_statement
import datetime
import json
from cms.utils.conf import get_cms_setting
from cms.test_utils.util.fuzzy_int import FuzzyInt
from django.core.cache import cache
import os
Expand Down Expand Up @@ -1184,7 +1185,7 @@ def test_add_plugin_with_m2m(self):
self.assertEqual(ArticlePluginModel.objects.count(), 1)
plugin = ArticlePluginModel.objects.all()[0]
self.assertEqual(self.section_count, plugin.sections.count())
response = self.client.get('/en/?edit')
response = self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.assertEqual(response.status_code, 200)
self.assertEqual(plugin.sections.through._meta.db_table, 'manytomany_rel_articlepluginmodel_sections')

Expand Down
3 changes: 2 additions & 1 deletion cms/tests/publisher.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
from cms.utils.conf import get_cms_setting
from cms.test_utils.util.fuzzy_int import FuzzyInt

from djangocms_text_ckeditor.models import Text
Expand Down Expand Up @@ -175,7 +176,7 @@ def test_publish_home(self):
with self.login_user_context(superuser):
response = self.client.get(reverse("admin:cms_page_publish_page", args=[page.pk, 'en']))
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], "http://testserver/en/?edit_off")
self.assertEqual(response['Location'], "http://testserver/en/?%s" % get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF'))

def test_publish_single(self):
name = self._testMethodName
Expand Down
5 changes: 3 additions & 2 deletions cms/tests/reversion_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import with_statement
import shutil
from os.path import join
from cms.utils.conf import get_cms_setting

from djangocms_text_ckeditor.models import Text
from django.conf import settings
Expand Down Expand Up @@ -195,9 +196,9 @@ def test_undo_slug_collision(self):
self.assertEqual(Title.objects.get(page=page1).slug, 'page3')
response = self.client.get(reverse("admin:cms_page_changelist"))
self.assertEqual(response.status_code, 200)
response = self.client.get('/en/?edit')
response = self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.assertEqual(response.status_code, 200)
response = self.client.get('/en/page1/?edit')
response = self.client.get('/en/page1/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.assertEqual(response.status_code, 200)


Expand Down
2 changes: 1 addition & 1 deletion cms/tests/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_site_preview(self):
with self.login_user_context(self.get_superuser()):
response = self.client.get(reverse('admin:cms_page_preview_page', args=[page.pk, 'de']))
self.assertEqual(response.status_code, 302)
self.assertEqual(response._headers['location'][1], 'http://sample2.com/de/?edit&language=de')
self.assertEqual(response._headers['location'][1], 'http://sample2.com/de/?%s&language=de' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))

def test_site_publish(self):
self._login_context.__exit__(None, None, None)
Expand Down
Loading

0 comments on commit d897cf0

Please sign in to comment.