Skip to content

Commit

Permalink
Merge pull request #3871 from yakky/feature/plugin_action_urls
Browse files Browse the repository at this point in the history
Implement models based attribute for add/edit/move/delete_url
  • Loading branch information
yakky committed Feb 23, 2015
2 parents 3ecf871 + c10ee15 commit c8f06b5
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 8 deletions.
34 changes: 34 additions & 0 deletions cms/models/pluginmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,40 @@ def delete(self, no_mp=False, *args, **kwargs):
else:
super(CMSPlugin, self).delete(*args, **kwargs)

@property
def add_url(self):
"""
Returns a custom url to add plugin instances
"""
return None

@property
def edit_url(self):
"""
Returns a custom url to edit plugin instances
"""
return None

@property
def move_url(self):
"""
Returns a custom url to move plugin instances
"""
return None

@property
def delete_url(self):
"""
Returns a custom url to delete plugin instances
"""
return None

@property
def copy_url(self):
"""
Returns a custom url to copy plugin instances
"""
return None

reversion_register(CMSPlugin)

Expand Down
10 changes: 5 additions & 5 deletions cms/templates/cms/toolbar/plugin.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
'plugin_restriction': [{% for cls in allowed_child_classes %}"{{ cls }}"{% if not forloop.last %},{% endif %}{% endfor %}],
'onClose': {% if refresh_page %}'REFRESH_PAGE'{% else %}{% if redirect_on_close %}'{{ redirect_on_close }}'{% else %}false{% endif %}{% endif %},
'urls': {
'add_plugin': '{% if add_url %}{{ add_url }}{% else %}{% cms_admin_url "cms_page_add_plugin" %}}{% endif %}',
'edit_plugin': '{% if edit_url %}{{ edit_url }}{% elif instance %}{% cms_admin_url "cms_page_edit_plugin" instance.pk %}{% endif %}',
'move_plugin': '{% if move_url %}{{ move_url }}{% else %}{% cms_admin_url "cms_page_move_plugin" %}{% endif %}',
'delete_plugin': '{% if delete_url %}{{ delete_url }}{% elif instance %}{% cms_admin_url "cms_page_delete_plugin" instance.pk %}{% endif %}',
'copy_plugin': '{% cms_admin_url "cms_page_copy_plugins" %}'
'add_plugin': '{% if instance.add_url %}{{ instance.add_url }}{% elif add_url %}{{ add_url }}{% else %}{% cms_admin_url "cms_page_add_plugin" %}}{% endif %}',
'edit_plugin': '{% if instance.edit_url %}{{ instance.edit_url }}{% elif edit_url %}{{ edit_url }}{% elif instance %}{% cms_admin_url "cms_page_edit_plugin" instance.pk %}{% endif %}',
'move_plugin': '{% if instance.move_url %}{{ instance.move_url }}{% elif move_url %}{{ move_url }}{% else %}{% cms_admin_url "cms_page_move_plugin" %}{% endif %}',
'delete_plugin': '{% if instance.delete_url %}{{ instance.delete_url }}{% elif delete_url %}{{ delete_url }}{% elif instance %}{% cms_admin_url "cms_page_delete_plugin" instance.pk %}{% endif %}',
'copy_plugin': '{% if instance.copy_url %}{{ instance.copy_url }}{% else %}{% cms_admin_url "cms_page_copy_plugins" %}{% endif %}'
} {% endlanguage %}
});
});
Expand Down
20 changes: 20 additions & 0 deletions cms/test_utils/project/mti_pluginapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ class TestPluginAlphaModel(CMSPlugin):
"""
alpha = models.CharField('name', blank=False, default='test plugin alpha', max_length=32)

@property
def add_url(self):
return '/admin/custom/view/'

@property
def edit_url(self):
return '/admin/custom/view/%s/' % self.pk

@property
def move_url(self):
return '/admin/custom/move/'

@property
def delete_url(self):
return '/admin/custom/delete/%s/' % self.pk

@property
def copy_url(self):
return '/admin/custom/copy/'


class TestPluginBetaModel(TestPluginAlphaModel):
"""
Expand Down
24 changes: 22 additions & 2 deletions cms/tests/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ def test_markup_menu_items(self):
self.assertContains(response,
'<div class="cms_submenu-item"><a href="/some/other/url/" data-rel="ajax_add"')

def test_markup_plugin_template(self):
page = create_page("toolbar-page-1", "col_two.html", "en", published=True)
plugin_1 = add_plugin(page.placeholders.get(slot='col_left'), language='en',
plugin_type='TestPluginAlpha', alpha='alpha')
plugin_2 = add_plugin(page.placeholders.get(slot='col_left'), language='en',
plugin_type='TextPlugin', body='text')
superuser = self.get_superuser()
with self.login_user_context(superuser):
response = self.client.get('/en/?%s' % get_cms_setting('CMS_TOOLBAR_URL__EDIT_ON'))
self.assertEqual(response.status_code, 200)
response_text = response.render().rendered_content
self.assertTrue(re.search('edit_plugin.+/admin/custom/view/%s' % plugin_1.pk, response_text))
self.assertTrue(re.search('move_plugin.+/admin/custom/move/', response_text))
self.assertTrue(re.search('delete_plugin.+/admin/custom/delete/%s/' % plugin_1.pk, response_text))
self.assertTrue(re.search('add_plugin.+/admin/custom/view/', response_text))
self.assertTrue(re.search('copy_plugin.+/admin/custom/copy/', response_text))

self.assertTrue(re.search('edit_plugin.+/en/admin/cms/page/edit-plugin/%s' % plugin_2.pk, response_text))
self.assertTrue(re.search('delete_plugin.+/en/admin/cms/page/delete-plugin/%s/' % plugin_2.pk, response_text))

def test_show_toolbar_to_staff(self):
page = create_page("toolbar-page", "nav_playground.html", "en",
published=True)
Expand Down Expand Up @@ -981,7 +1001,7 @@ def test_view_method(self):
request = self.get_page_request(page, user, edit=True)
response = detail_view(request, ex1.pk, template_string=template_text)
self.assertContains(
response,"'edit_plugin': '/admin/placeholderapp/example1/edit-field/%s/en/" % ex1.pk)
response, "'edit_plugin': '/admin/placeholderapp/example1/edit-field/%s/en/" % ex1.pk)

def test_view_url(self):
user = self.get_staff()
Expand All @@ -999,7 +1019,7 @@ def test_view_url(self):
request = self.get_page_request(page, user, edit=True)
response = detail_view(request, ex1.pk, template_string=template_text)
self.assertContains(
response,"'edit_plugin': '/admin/placeholderapp/example1/edit-field/%s/en/" % ex1.pk)
response, "'edit_plugin': '/admin/placeholderapp/example1/edit-field/%s/en/" % ex1.pk)

def test_method_attribute(self):
user = self.get_staff()
Expand Down
42 changes: 41 additions & 1 deletion docs/reference/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,44 @@ Example::
plugin.set_translatable_content({'body': u'<p>This is a different text!</p>\n'})
# returns True

See also: `translatable_content_excluded_fields`_, `get_translatable_content`_
See also: `translatable_content_excluded_fields`_, `get_translatable_content`_

add_url
-------

Returns the url to call to add a plugin instance; useful to implement plugin-specific
logic in a custom view

Default: None (``cms_page_add_plugin`` view is used)

edit_url
--------

Returns the url to call to edit a plugin instance; useful to implement plugin-specific
logic in a custom view

Default: None (``cms_page_edit_plugin`` view is used)

move_url
--------

Returns the url to call to move a plugin instance; useful to implement plugin-specific
logic in a custom view

Default: None (``cms_page_move_plugin`` view is used)

delete_url
----------

Returns the url to call to delete a plugin instance; useful to implement plugin-specific
logic in a custom view

Default: None (``cms_page_delete_plugin`` view is used)

copy_url
--------

Returns the url to call to copy a plugin instance; useful to implement plugin-specific
logic in a custom view

Default: None (``cms_page_copy_plugins`` view is used)

0 comments on commit c8f06b5

Please sign in to comment.