Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add ability to subclass the plugin #16

Merged
merged 1 commit into from

4 participants

@dsh2dsh

See examples/cmsplugin_custom_contact how to subclass
cmsplugin_custom and add custom fields into it. You can override
properties of subclassed ContactPlugin and use your own templates
and classes.

@dsh2dsh dsh2dsh Add ability to subclass the plugin.
See ``examples/cmsplugin_custom_contact`` how to subclass
``cmsplugin_custom`` and add custom fields into it. You can override
properties of subclassed ``ContactPlugin`` and use your own templates
and classes.
8ab1532
@mbaechtold

When is this going to be merged? It's quite common that the contact form has to be customized.

@maccesch maccesch merged commit 49ac8c7 into from
@maccesch
Owner

I always wanted to do this, thank you very much!

@mbaechtold

Sweet, thanks.

@Lacrymology

Excuse me, I'm not understanding how to subclass this.

Do I need to remove this the original from the apps? Does creating cms_plugins.py and adding the dir to installed apps suffice? Will both types of plugin appear on the list?

because I'm having trouble with this, maybe I'm missing something silly.
Thanks

@dsh2dsh

You can remove the original from the apps. It doesn't matter. If you'll remove it, you will not use the original and will use only your extended plugin. But if you need it you can use both. Just give your plugin different name like here:

class BaseContactPlugin(ContactPlugin):
    name = _("Custom Contact Form")

Don't forget, you need your custom cms_plugins.py, a model, a form and I think a template. You can use cms_plugins.py from examples dir as a start point.

@Lacrymology

oooh.. ok

I'm gonna try a hackier way to go about this, if it works, I'll post about it

@maccesch
Owner

The example is a bit rough. I'm putting together a working example right now.

@maccesch
Owner

Alright I just pushed the working example.

Please note, that it requires the original cmsplugin_contact in the INSTALLED_APPS because it uses the original admin templates.

@Lacrymology
@maccesch
Owner

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 20, 2011
  1. @dsh2dsh

    Add ability to subclass the plugin.

    dsh2dsh authored
    See ``examples/cmsplugin_custom_contact`` how to subclass
    ``cmsplugin_custom`` and add custom fields into it. You can override
    properties of subclassed ``ContactPlugin`` and use your own templates
    and classes.
This page is out of date. Refresh to see the latest.
View
8 README.rst
@@ -57,3 +57,11 @@ AKISMET_API_KEY
---------------
The same as for ReCAPTCHA goes fo Akismet.
+
+Extending
+=========
+
+See ``examples/cmsplugin_custom_contact`` how to subclass
+``cmsplugin_custom`` and add custom fields into it. You can override
+properties of subclassed ``ContactPlugin`` and use your own templates
+and classes.
View
30 cmsplugin_contact/cms_plugins.py
@@ -9,7 +9,7 @@
from cms.plugins.text.settings import USE_TINYMCE
from cms.plugins.text.widgets.wymeditor_widget import WYMEditor
from models import Contact
-from forms import AkismetContactForm, RecaptchaContactForm, HoneyPotContactForm
+from forms import ContactForm, AkismetContactForm, RecaptchaContactForm, HoneyPotContactForm
from admin import ContactAdminForm
class ContactPlugin(CMSPluginBase):
@@ -17,6 +17,8 @@ class ContactPlugin(CMSPluginBase):
name = _("Contact Form")
render_template = "cmsplugin_contact/contact.html"
form = ContactAdminForm
+ contact_form = ContactForm
+ email_template = "cmsplugin_contact/email.txt"
fieldsets = (
(None, {
@@ -64,16 +66,24 @@ def get_form(self, request, obj=None, **kwargs):
def create_form(self, instance, request):
if instance.get_spam_protection_method_display() == 'Akismet':
AkismetContactForm.aksimet_api_key = instance.akismet_api_key
- FormClass = AkismetContactForm
+ class ContactForm(self.contact_form, AkismetContactForm):
+ pass
+ FormClass = ContactForm
elif instance.get_spam_protection_method_display() == 'ReCAPTCHA':
- RecaptchaContactForm.recaptcha_public_key = getattr(settings, "RECAPTCHA_PUBLIC_KEY", \
- instance.recaptcha_public_key)
- RecaptchaContactForm.recaptcha_private_key = getattr(settings, "RECAPTCHA_PRIVATE_KEY", \
- instance.recaptcha_private_key)
+ RecaptchaContactForm.recaptcha_public_key = getattr(
+ settings, "RECAPTCHA_PUBLIC_KEY",
+ instance.recaptcha_public_key)
+ RecaptchaContactForm.recaptcha_private_key = getattr(
+ settings, "RECAPTCHA_PRIVATE_KEY",
+ instance.recaptcha_private_key)
RecaptchaContactForm.recaptcha_theme = instance.recaptcha_theme
- FormClass = RecaptchaContactForm
+ class ContactForm(self.contact_form, RecaptchaContactForm):
+ pass
+ FormClass = ContactForm
else:
- FormClass = HoneyPotContactForm
+ class ContactForm(self.contact_form, HoneyPotContactForm):
+ pass
+ FormClass = ContactForm
if request.method == "POST":
return FormClass(request, data=request.POST)
@@ -88,8 +98,8 @@ def send(self, form, site_email):
email_message = EmailMessage(
render_to_string("cmsplugin_contact/subject.txt", {
'subject': subject,
- }).splitlines()[0],
- render_to_string("cmsplugin_contact/email.txt", {
+ }),
+ render_to_string(self.email_template, {
'data': form.cleaned_data,
}),
form.cleaned_data['email'],
View
13 cmsplugin_contact/forms.py
@@ -2,29 +2,24 @@
#import settings
from cmsplugin_contact.nospam.forms import HoneyPotForm, RecaptchaForm, AkismetForm
-class HoneyPotContactForm(HoneyPotForm):
+class ContactForm(forms.Form):
email = forms.EmailField()
subject = forms.CharField(required=False)
content = forms.CharField(widget=forms.Textarea())
+
+class HoneyPotContactForm(HoneyPotForm):
+ pass
class AkismetContactForm(AkismetForm):
akismet_fields = {
'comment_author_email': 'email',
'comment_content': 'content'
}
- email = forms.EmailField()
- subject = forms.CharField(required=False)
- content = forms.CharField(widget=forms.Textarea())
-
akismet_api_key = None
class RecaptchaContactForm(RecaptchaForm):
- email = forms.EmailField()
- subject = forms.CharField(required=False)
- content = forms.CharField(widget=forms.Textarea())
-
recaptcha_public_key = None
recaptcha_private_key = None
recaptcha_theme = None
View
9 cmsplugin_contact/models.py
@@ -2,7 +2,8 @@
from django.utils.translation import ugettext_lazy as _
from cms.models import CMSPlugin
-class Contact(CMSPlugin):
+# Feel free to extend this class instead of Contact.
+class BaseContact(CMSPlugin):
SPAM_PROTECTION_CHOICES = (
(0, 'Honeypot'),
(1, 'Akismet'),
@@ -31,6 +32,12 @@ class Contact(CMSPlugin):
recaptcha_public_key = models.CharField(max_length=255, blank=True)
recaptcha_private_key = models.CharField(max_length=255, blank=True)
recaptcha_theme = models.CharField(max_length=20, choices=THEME_CHOICES, default='clean', verbose_name=_('ReCAPTCHA theme'))
+
+ class Meta:
+ abstract = True
def __unicode__(self):
return self.site_email
+
+class Contact(BaseContact):
+ pass
View
0  examples/cmsplugin_custom_contact/__init__.py
No changes.
View
38 examples/cmsplugin_custom_contact/cms_plugins.py
@@ -0,0 +1,38 @@
+from django.db import models
+from django.utils.translation import ugettext_lazy as _
+
+from cms.plugin_pool import plugin_pool
+
+from cmsplugin_contact.cms_plugins import ContactPlugin
+from cmsplugin_contact.forms import ContactForm
+from cmsplugin_contact.model import BaseContact
+
+class CustomContact(BaseContact):
+ custom_label = models.CharField(
+ _('Custom sender label'),
+ default=_('Your custom value'), max_length=20)
+
+class CustomContactForm(ContactForm):
+ custom = forms.CharField()
+
+class CustomContactPlugin(ContactPlugin):
+ model = CustomContact
+ name = _("Custom Contact Form")
+ render_template = "cmsplugin_contact/contact.html"
+ contact_form = CustomContactForm
+ email_template = "cmsplugin_contact/email.txt"
+
+ fieldsets = (
+ (None, {
+ 'fields': ('site_email', 'email_label', 'custom_label',
+ 'subject_label', 'content_label', 'thanks',
+ 'submit'),
+ }),
+ (_('Spam Protection'), {
+ 'fields': ('spam_protection_method', 'akismet_api_key',
+ 'recaptcha_public_key', 'recaptcha_private_key',
+ 'recaptcha_theme')
+ })
+ )
+
+plugin_pool.register_plugin(CustomContactPlugin)
Something went wrong with that request. Please try again.