48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
from Acquisition import aq_inner
|
|
|
|
from zope import interface
|
|
from zope import schema
|
|
|
|
from zope.component import getMultiAdapter, provideAdapter
|
|
|
|
from z3c.form import form, field, button, validator
|
|
|
|
from plone.z3cform.layout import wrap_form
|
|
|
|
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):
|
|
subject = schema.TextLine(title=_(u'Subject'), required=True)
|
|
name = schema.TextLine(title=_(u'Name'), required=True)
|
|
email = schema.TextLine(title=_(u'Name'), required=True)
|
|
phone = schema.TextLine(title=_(u'Phone'), required=False)
|
|
message = schema.Text(title=_(u'Message'), required=True)
|
|
captcha = schema.TextLine(title=u'Captcha', required=False)
|
|
|
|
class Contact(object):
|
|
def __init__(self, context):
|
|
self.context = context
|
|
|
|
class BaseForm(form.Form):
|
|
fields = field.Fields(IContactForm)
|
|
fields['captcha'].widgetFactory = CaptchaFieldWidget
|
|
|
|
@button.buttonAndHandler(u'Send')
|
|
def handleApply(self, action):
|
|
data, errors = self.extractData()
|
|
if 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']
|
|
return
|
|
|
|
ContactForm = wrap_form(BaseForm)
|
|
|
|
# Register Captcha validator for the captcha field in the IContactForm
|
|
validator.WidgetValidatorDiscriminators(CaptchaValidator, field=IContactForm['captcha'])
|