Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1461 from kux/issue_1452

Plugin cancel button doesn't work when using a PlaceholderField with a custom model - Issue 1452
  • Loading branch information...
commit f23a2e6a369798b46ba696860ca410423883b60b 2 parents 4b968ce + 013758c
Patrick Lauber digi604 authored
24 cms/admin/placeholderadmin.py
@@ -202,7 +202,29 @@ def edit_plugin(self, request, plugin_id):
202 202 post_request = request.POST.copy()
203 203 post_request['_continue'] = True
204 204 request.POST = post_request
205   -
  205 +
  206 + if request.POST.get("_cancel", False):
  207 + # cancel button was clicked
  208 + context = {
  209 + 'CMS_MEDIA_URL': settings.CMS_MEDIA_URL,
  210 + 'plugin': cms_plugin,
  211 + 'is_popup': True,
  212 + 'name': unicode(cms_plugin),
  213 + "type": cms_plugin.get_plugin_name(),
  214 + 'plugin_id': plugin_id,
  215 + 'icon': force_escape(escapejs(cms_plugin.get_instance_icon_src())),
  216 + 'alt': force_escape(escapejs(cms_plugin.get_instance_icon_alt())),
  217 + 'cancel': True,
  218 + }
  219 + instance = cms_plugin.get_plugin_instance()[0]
  220 + if not instance:
  221 + # cancelled before any content was added to plugin
  222 + cms_plugin.delete()
  223 + context.update({
  224 + "deleted":True,
  225 + })
  226 + return render_to_response('admin/cms/page/plugin_forms_ok.html', context, RequestContext(request))
  227 +
206 228 if not instance:
207 229 # instance doesn't exist, call add view
208 230 response = plugin_admin.add_view(request)
19 cms/test_utils/testcases.py
@@ -26,6 +26,9 @@
26 26 URL_CMS_PLUGIN_REMOVE = urljoin(URL_CMS_PAGE_CHANGE, "remove-plugin/")
27 27 URL_CMS_TRANSLATION_DELETE = urljoin(URL_CMS_PAGE_CHANGE, "delete-translation/")
28 28
  29 +URL_CMS_PAGE_HISTORY = urljoin(URL_CMS_PAGE_CHANGE, "history/%d/")
  30 +URL_CMS_PLUGIN_HISTORY_EDIT = urljoin(URL_CMS_PAGE_HISTORY, "edit-plugin/")
  31 +
29 32
30 33 class _Warning(object):
31 34 def __init__(self, message, category, filename, lineno):
@@ -218,6 +221,22 @@ def get_request(self, path=None, language=None, post_data=None, enforce_csrf_che
218 221 request.user = getattr(self, 'user', AnonymousUser())
219 222 request.LANGUAGE_CODE = language
220 223 request._dont_enforce_csrf_checks = not enforce_csrf_checks
  224 +
  225 + class MockStorage(object):
  226 +
  227 + def __len__(self):
  228 + return 0
  229 +
  230 + def __iter__(self):
  231 + return iter([])
  232 +
  233 + def add(self, level, message, extra_tags=''):
  234 + pass
  235 +
  236 + def update(self, response):
  237 + pass
  238 +
  239 + request._messages = MockStorage()
221 240 return request
222 241
223 242 def check_published_page_attributes(self, page):
57 cms/tests/placeholder.py
@@ -396,14 +396,8 @@ def test_excercise_get_attached_fields_notplugins(self):
396 396 result = [f.name for f in list(ph._get_attached_fields())]
397 397 self.assertEqual(result, ['placeholder']) # Simple PH - still one placeholder field name
398 398
399   -class PlaceholderAdminTest(CMSTestCase):
400   - placeholderconf = {'test': {
401   - 'limits': {
402   - 'global': 2,
403   - 'TextPlugin': 1,
404   - }
405   - }
406   - }
  399 +
  400 +class PlaceholderAdminTestBase(CMSTestCase):
407 401 def get_placeholder(self):
408 402 return Placeholder.objects.create(slot='test')
409 403
@@ -414,6 +408,15 @@ def get_admin(self):
414 408 def get_post_request(self, data):
415 409 return self.get_request(post_data=data)
416 410
  411 +
  412 +class PlaceholderAdminTest(PlaceholderAdminTestBase):
  413 + placeholderconf = {'test': {
  414 + 'limits': {
  415 + 'global': 2,
  416 + 'TextPlugin': 1,
  417 + }
  418 + }
  419 + }
417 420 def test_global_limit(self):
418 421 placeholder = self.get_placeholder()
419 422 admin = self.get_admin()
@@ -452,8 +455,44 @@ def test_type_limit(self):
452 455 self.assertEqual(response.status_code, 400)
453 456 self.assertEqual(response.content, "This placeholder already has the maximum number (1) of TextPlugin plugins.")
454 457
  458 + def test_edit_plugin_and_cancel(self):
  459 + placeholder = self.get_placeholder()
  460 + admin = self.get_admin()
  461 + data = {
  462 + 'plugin_type': 'TextPlugin',
  463 + 'placeholder': placeholder.pk,
  464 + 'language': 'en',
  465 + }
  466 + superuser = self.get_superuser()
  467 + with UserLoginContext(self, superuser):
  468 + with SettingsOverride(CMS_PLACEHOLDER_CONF=self.placeholderconf):
  469 + request = self.get_post_request(data)
  470 + response = admin.add_plugin(request)
  471 + self.assertEqual(response.status_code, 200)
  472 + plugin_id = int(response.content)
  473 + data = {
  474 + 'body': 'Hello World',
  475 + }
  476 + request = self.get_post_request(data)
  477 + response = admin.edit_plugin(request, plugin_id)
  478 + self.assertEqual(response.status_code, 200)
  479 + text_plugin = Text.objects.get(pk=plugin_id)
  480 + self.assertEquals('Hello World', text_plugin.body)
  481 +
  482 + # edit again, but this time press cancel
  483 + data = {
  484 + 'body': 'Hello World!!',
  485 + '_cancel': True,
  486 + }
  487 + request = self.get_post_request(data)
  488 + response = admin.edit_plugin(request, plugin_id)
  489 + self.assertEqual(response.status_code, 200)
  490 + text_plugin = Text.objects.get(pk=plugin_id)
  491 + self.assertEquals('Hello World', text_plugin.body)
  492 +
  493 +
455 494
456   -class PlaceholderPluginPermissionTests(PlaceholderAdminTest):
  495 +class PlaceholderPluginPermissionTests(PlaceholderAdminTestBase):
457 496
458 497 def _testuser(self):
459 498 u = User(username="test", is_staff = True, is_active = True, is_superuser = False)
30 cms/tests/plugins.py
@@ -19,7 +19,7 @@
19 19 from cms.test_utils.project.pluginapp.plugins.manytomany_rel.models import (
20 20 ArticlePluginModel)
21 21 from cms.test_utils.testcases import (CMSTestCase, URL_CMS_PAGE,
22   - URL_CMS_PAGE_ADD, URL_CMS_PLUGIN_ADD, URL_CMS_PLUGIN_EDIT, URL_CMS_PAGE_CHANGE, URL_CMS_PLUGIN_REMOVE)
  22 + URL_CMS_PAGE_ADD, URL_CMS_PLUGIN_ADD, URL_CMS_PLUGIN_EDIT, URL_CMS_PAGE_CHANGE, URL_CMS_PLUGIN_REMOVE, URL_CMS_PLUGIN_HISTORY_EDIT)
23 23 from cms.sitemaps.cms_sitemap import CMSSitemap
24 24 from cms.test_utils.util.context_managers import SettingsOverride
25 25 from cms.utils.copy_plugins import copy_plugins_to
@@ -119,11 +119,37 @@ def test_add_edit_plugin(self):
119 119 "body":"Hello World!!",
120 120 "_cancel":True,
121 121 }
122   - response = self.client.post(URL_CMS_PAGE_ADD, data)
  122 + edit_url = '%s%d/' % (URL_CMS_PLUGIN_EDIT, created_plugin_id)
  123 + response = self.client.post(edit_url, data)
123 124 self.assertEquals(response.status_code, 200)
124 125 txt = Text.objects.all()[0]
125 126 self.assertEquals("Hello World", txt.body)
126 127
  128 + def test_plugin_history_view(self):
  129 + """
  130 + Test plugin history view
  131 + """
  132 + from reversion.models import Version
  133 + page_data = self.get_new_page_data()
  134 + # two versions created by simply creating the page
  135 + response = self.client.post(URL_CMS_PAGE_ADD, page_data)
  136 + page = Page.objects.all()[0]
  137 + page_id = int(page.pk)
  138 + # page version 3
  139 + created_plugin_id = self._create_text_plugin_on_page(page)
  140 + # page version 4
  141 + txt = self._edit_text_plugin(created_plugin_id, "Hello Foo")
  142 + self.assertEquals("Hello Foo", txt.body)
  143 + # page version 5
  144 + txt = self._edit_text_plugin(created_plugin_id, "Hello Bar")
  145 + self.assertEquals("Hello Bar", txt.body)
  146 + versions = [v.pk for v in Version.objects.get_for_object(page)]
  147 + history_url = '%s%d/' % (
  148 + URL_CMS_PLUGIN_HISTORY_EDIT % (page_id, versions[-2]),
  149 + created_plugin_id)
  150 + response = self.client.get(history_url)
  151 + self.assertEquals(response.status_code, 200)
  152 + self.assertIn('Hello Foo', response.content)
127 153
128 154 def test_plugin_order(self):
129 155 """

0 comments on commit f23a2e6

Please sign in to comment.
Something went wrong with that request. Please try again.