Skip to content

Commit

Permalink
render the right form and template
Browse files Browse the repository at this point in the history
  • Loading branch information
ebertti committed Oct 4, 2013
1 parent 74e6e36 commit 32718e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cmsplugin_contact/cms_plugins.py
Expand Up @@ -7,13 +7,15 @@

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cmsplugin_contact.utils import class_for_path

try:
from cms.plugins.text.settings import USE_TINYMCE
except ImportError:
USE_TINYMCE = False

from models import Contact
from forms import ContactForm, AkismetContactForm, RecaptchaContactForm, HoneyPotContactForm
from forms import AkismetContactForm, RecaptchaContactForm, HoneyPotContactForm
from admin import ContactAdminForm


Expand All @@ -25,13 +27,12 @@ class ContactPlugin(CMSPluginBase):
name = _("Contact Form")
render_template = "cmsplugin_contact/contact.html"
form = ContactAdminForm
contact_form = ContactForm
subject_template = "cmsplugin_contact/subject.txt"
email_template = "cmsplugin_contact/email.txt"

fieldsets = (
(None, {
'fields': ('form_name', 'site_email', 'email_label', 'subject_label', 'content_label', 'thanks', 'submit'),
'fields': ('form_name', 'form_layout', 'site_email', 'thanks', 'submit'),
}),
(_('Spam Protection'), {
'fields': ('spam_protection_method', 'akismet_api_key', 'recaptcha_public_key', 'recaptcha_private_key', 'recaptcha_theme')
Expand Down Expand Up @@ -76,15 +77,18 @@ def get_form(self, request, obj=None, **kwargs):
return super(ContactPlugin, self).get_form(request, obj, **kwargs)

def create_form(self, instance, request):

ContactFormBase = class_for_path(instance.form_layout)

if instance.get_spam_protection_method_display() == 'Akismet':
AkismetContactForm.aksimet_api_key = instance.akismet_api_key
class ContactForm(self.contact_form, AkismetContactForm):
class ContactForm(ContactFormBase, AkismetContactForm):
pass
FormClass = ContactForm
elif instance.get_spam_protection_method_display() == 'ReCAPTCHA':
#if you really want the user to be able to set the key in
# every form, this should be more flexible
class ContactForm(self.contact_form, RecaptchaContactForm):
class ContactForm(ContactFormBase, RecaptchaContactForm):
recaptcha_public_key = (
instance.recaptcha_public_key or
getattr(settings, "RECAPTCHA_PUBLIC_KEY", None)
Expand All @@ -97,7 +101,7 @@ class ContactForm(self.contact_form, RecaptchaContactForm):

FormClass = ContactForm
else:
class ContactForm(self.contact_form, HoneyPotContactForm):
class ContactForm(ContactFormBase, HoneyPotContactForm):
pass
FormClass = ContactForm

Expand Down Expand Up @@ -134,7 +138,7 @@ def render(self, context, instance, placeholder):
request = context['request']

form = self.create_form(instance, request)

instance.render_template = form.template
if request.method == "POST" and form.is_valid():
self.send(form, instance.form_name, instance.site_email, attachments=request.FILES)
context.update({
Expand Down
9 changes: 9 additions & 0 deletions cmsplugin_contact/utils.py
@@ -0,0 +1,9 @@
# coding=utf-8
import importlib


def class_for_path(class_path):
module_name, class_name = class_path.rsplit(".", 1)
m = importlib.import_module(module_name)
c = getattr(m, class_name)
return c

0 comments on commit 32718e0

Please sign in to comment.