Skip to content

Commit

Permalink
Merge pull request #1474 from ojii/simple-plugins
Browse files Browse the repository at this point in the history
Saner defaults for plugins
  • Loading branch information
beniwohli committed Oct 11, 2012
2 parents 764702a + 3001362 commit f2f44b0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
13 changes: 8 additions & 5 deletions cms/plugin_base.py
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
import re
from cms.exceptions import SubClassNeededError, Deprecated
from cms.models import CMSPlugin
from django import forms
from django.conf import settings
from django.contrib import admin
from django.core.exceptions import ImproperlyConfigured
from django.db.models.options import get_verbose_name
from django.forms.models import ModelForm
from django.utils.encoding import smart_str
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -71,7 +71,7 @@ def __new__(cls, name, bases, attrs):
]
# Set default name
if not new_plugin.name:
new_plugin.name = get_verbose_name(new_plugin.__name__)
new_plugin.name = re.sub("([a-z])([A-Z])","\g<1> \g<2>", name)
return new_plugin


Expand All @@ -83,7 +83,7 @@ class CMSPluginBase(admin.ModelAdmin):
form = None
change_form_template = "admin/cms/page/plugin_change_form.html"
# Should the plugin be rendered in the admin?
admin_preview = True
admin_preview = False

render_template = None
# Should the plugin be rendered at all, or doesn't it have any output?
Expand All @@ -106,8 +106,11 @@ def __init__(self, model=None, admin_site=None):
self.placeholder = None
self.page = None


def render(self, context, instance, placeholder):
raise NotImplementedError("render needs to be implemented")
context['instance'] = instance
context['placeholder'] = placeholder
return context

@property
def parent(self):
Expand Down Expand Up @@ -223,4 +226,4 @@ def pluginmedia(self):
def get_plugin_media(self, request, context, plugin):
raise Deprecated(
"CMSPluginBase.get_plugin_media is deprecated in favor of django-sekizai"
)
)
17 changes: 17 additions & 0 deletions cms/tests/plugins.py
Expand Up @@ -25,6 +25,7 @@
from cms.test_utils.util.context_managers import SettingsOverride
from cms.utils.copy_plugins import copy_plugins_to
from django.conf import settings
from django.contrib import admin
from django.contrib.auth.models import User
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
Expand Down Expand Up @@ -999,3 +1000,19 @@ def test_link_or_page(self):
picture.url = "test"
self.assertRaises(ValidationError, picture.clean)


class SimplePluginTests(TestCase):
def test_simple_naming(self):
class MyPlugin(CMSPluginBase):
render_template = 'base.html'
self.assertEqual(MyPlugin.name, 'My Plugin')

def test_simple_context(self):
class MyPlugin(CMSPluginBase):
render_template = 'base.html'
plugin = MyPlugin(ArticlePluginModel, admin.site)
context = {}
out_context = plugin.render(context, 1, 2)
self.assertEqual(out_context['instance'], 1)
self.assertEqual(out_context['placeholder'], 2)
self.assertIs(out_context, context)
2 changes: 1 addition & 1 deletion docs/extending_cms/api_references.rst
Expand Up @@ -183,7 +183,7 @@ cms.plugin_base

.. attribute:: admin_preview

Defaults to ``True``, if ``False`` no preview is done in the admin.
Defaults to ``False``, if ``True`` there will be a preview in the admin.

.. attribute:: change_form_template

Expand Down
16 changes: 10 additions & 6 deletions docs/extending_cms/custom_plugins.rst
Expand Up @@ -106,12 +106,8 @@ In there, you place your plugins. For our example, include the following code::

class HelloPlugin(CMSPluginBase):
model = CMSPlugin
name = _("Hello Plugin")
render_template = "hello_plugin.html"

def render(self, context, instance, placeholder):
return context

plugin_pool.register_plugin(HelloPlugin)

Now we're almost done. All that's left is to add the template. Add the
Expand Down Expand Up @@ -139,10 +135,11 @@ There are three required attributes on those classes:
closely in a bit).
* ``name``: The name of your plugin as displayed in the admin. It is generally
good practice to mark this string as translatable using
:func:`django.utils.translation.ugettext_lazy`, however this is optional.
:func:`django.utils.translation.ugettext_lazy`, however this is optional. By
default the name is a nicer version of the class name.
* ``render_template``: The template to render this plugin with.

In addition to those three attributes, you must also define a
In addition to those three attributes, you can also define a
:meth:`render` method on your subclasses. It is specifically this `render`
method that is the **view** for your plugin.

Expand All @@ -156,6 +153,13 @@ This method must return a dictionary or an instance of
:class:`django.template.Context`, which will be used as context to render the
plugin template.

.. versionadded:: 2.4

By default this method will add ``instance`` and ``placeholder`` to the
context, which means for simple plugins, there is no need to overwrite this
method.



*********************
Storing configuration
Expand Down

0 comments on commit f2f44b0

Please sign in to comment.