Permalink
Browse files

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.
  • Loading branch information...
1 parent 38551dd commit 8ab153207127d880e69a2423b95eedca3b94a1f0 @dsh2dsh dsh2dsh committed Aug 20, 2011
View
@@ -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.
@@ -9,14 +9,16 @@
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):
model = Contact
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
@@ -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
@@ -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
@@ -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)

0 comments on commit 8ab1532

Please sign in to comment.