Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #16 from dsh2dsh/master

add ability to subclass the plugin
  • Loading branch information...
commit 49ac8c78da0cb8d0dbea4a3e6f7d8086141815bf 2 parents 5ae9123 + 8ab1532
@maccesch authored
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)
Please sign in to comment.
Something went wrong with that request. Please try again.