From f4032b7a69c7323d9e01d20b330f6b2dce67f723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 1 Aug 2011 16:25:45 +0200 Subject: [PATCH] Make it possible to install custom contact forms anywhere --- tabellio/contact/configure.zcml | 30 +++++----- tabellio/contact/form.py | 57 +++++++++++++++---- tabellio/contact/form_templates/view.pt | 28 +++++++++ .../form_templates/view_effectivecontact.pt | 10 ++++ .../contact/profiles/default/metadata.xml | 6 ++ tabellio/contact/profiles/default/types.xml | 3 + .../default/types/tabellio.contact.form.xml | 50 ++++++++++++++++ 7 files changed, 160 insertions(+), 24 deletions(-) create mode 100644 tabellio/contact/form_templates/view.pt create mode 100644 tabellio/contact/form_templates/view_effectivecontact.pt create mode 100644 tabellio/contact/profiles/default/metadata.xml create mode 100644 tabellio/contact/profiles/default/types.xml create mode 100644 tabellio/contact/profiles/default/types/tabellio.contact.form.xml diff --git a/tabellio/contact/configure.zcml b/tabellio/contact/configure.zcml index ee4b8d9..9156c5d 100644 --- a/tabellio/contact/configure.zcml +++ b/tabellio/contact/configure.zcml @@ -1,29 +1,33 @@ + + + + + + + - - - - + + - - diff --git a/tabellio/contact/form.py b/tabellio/contact/form.py index 61903f1..32b9f0e 100644 --- a/tabellio/contact/form.py +++ b/tabellio/contact/form.py @@ -1,20 +1,46 @@ +from five import grok from Acquisition import aq_inner +from zope.interface import implements from zope import interface from zope import schema from zope.component import getMultiAdapter, provideAdapter +from Products.CMFCore.utils import getToolByName from z3c.form import form, field, button, validator from plone.z3cform.layout import wrap_form +from z3c.form.ptcompat import ViewPageTemplateFile +from plone.dexterity.content import Item from plone.formwidget.captcha.widget import CaptchaFieldWidget from plone.formwidget.captcha.validator import CaptchaValidator from tabellio.agenda.interfaces import MessageFactory as _ + class IContactForm(interface.Interface): + title = schema.TextLine(title=_(u'Title')) + description = schema.Text(title=_(u'Description')) + subjects = schema.Text(title=_(u'Available Subjects')) + +class ContactForm(Item): + implements(IContactForm) + + +class View(grok.View): + grok.context(IContactForm) + grok.require('zope2.View') + + def contact_form(self): + effective_contact = EffectiveContact(self.context) + effective_form = EffectiveContactForm(effective_contact, self.request) + effective_form.update() + return effective_form.render() + + +class IEffectiveContact(interface.Interface): subject = schema.TextLine(title=_(u'Subject'), required=True) name = schema.TextLine(title=_(u'Name'), required=True) email = schema.TextLine(title=_(u'Name'), required=True) @@ -22,26 +48,35 @@ class IContactForm(interface.Interface): message = schema.Text(title=_(u'Message'), required=True) captcha = schema.TextLine(title=u'Captcha', required=False) -class Contact(object): - def __init__(self, context): +class EffectiveContact(object): + implements(IEffectiveContact) + + def __init__(self, context=None): self.context = context -class BaseForm(form.Form): - fields = field.Fields(IContactForm) + def absolute_url(self): + # this is used by the captcha + return self.context.absolute_url() + + +class EffectiveContactForm(form.Form): + fields = field.Fields(IEffectiveContact) fields['captcha'].widgetFactory = CaptchaFieldWidget + template = ViewPageTemplateFile('form_templates/view_effectivecontact.pt') @button.buttonAndHandler(u'Send') def handleApply(self, action): data, errors = self.extractData() - if data.has_key('captcha'): + if not errors and data.has_key('captcha'): # Verify the user input against the captcha - captcha = CaptchaValidator(self.context, self.request, None, IContactForm['captcha'], None) - if data.has_key('subject') and captcha.validate(data['captcha']): - # if captcha validation passes, print the subject - print data['subject'] + captcha = CaptchaValidator(self.context, self.request, None, IEffectiveContact['captcha'], None) + if captcha.validate(data['captcha']): + # if captcha validation passes, send the email. + plone_utils = getToolByName(self.context.context, 'plone_utils') + plone_utils.addPortalMessage(_('Your message has been sent successfully.')) + return self.request.response.redirect('./') return -ContactForm = wrap_form(BaseForm) # Register Captcha validator for the captcha field in the IContactForm -validator.WidgetValidatorDiscriminators(CaptchaValidator, field=IContactForm['captcha']) +validator.WidgetValidatorDiscriminators(CaptchaValidator, field=IEffectiveContact['captcha']) diff --git a/tabellio/contact/form_templates/view.pt b/tabellio/contact/form_templates/view.pt new file mode 100644 index 0000000..7578387 --- /dev/null +++ b/tabellio/contact/form_templates/view.pt @@ -0,0 +1,28 @@ + + + + + + +
+ +

+ +
+ +
+ +
+ + + + + + diff --git a/tabellio/contact/form_templates/view_effectivecontact.pt b/tabellio/contact/form_templates/view_effectivecontact.pt new file mode 100644 index 0000000..6e1313e --- /dev/null +++ b/tabellio/contact/form_templates/view_effectivecontact.pt @@ -0,0 +1,10 @@ + + + +
+ + + + + diff --git a/tabellio/contact/profiles/default/metadata.xml b/tabellio/contact/profiles/default/metadata.xml new file mode 100644 index 0000000..0984bcb --- /dev/null +++ b/tabellio/contact/profiles/default/metadata.xml @@ -0,0 +1,6 @@ + + 1 + + profile-plone.app.dexterity:default + + diff --git a/tabellio/contact/profiles/default/types.xml b/tabellio/contact/profiles/default/types.xml new file mode 100644 index 0000000..8b28c99 --- /dev/null +++ b/tabellio/contact/profiles/default/types.xml @@ -0,0 +1,3 @@ + + + diff --git a/tabellio/contact/profiles/default/types/tabellio.contact.form.xml b/tabellio/contact/profiles/default/types/tabellio.contact.form.xml new file mode 100644 index 0000000..ba90f06 --- /dev/null +++ b/tabellio/contact/profiles/default/types/tabellio.contact.form.xml @@ -0,0 +1,50 @@ + + + + + Contact Form + A contact form + document_icon.gif + False + True + False + + + + tabellio.contact.form.IContactForm + + + tabellio.contact.form.ContactForm + + + cmf.AddPortalContent + + + + + + + + view + False + + + + + + + + + + + + + + + + + +