83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
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)
|
|
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 EffectiveContact(object):
|
|
implements(IEffectiveContact)
|
|
|
|
def __init__(self, context=None):
|
|
self.context = context
|
|
|
|
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 not errors and data.has_key('captcha'):
|
|
# Verify the user input against the captcha
|
|
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
|
|
|
|
|
|
# Register Captcha validator for the captcha field in the IContactForm
|
|
validator.WidgetValidatorDiscriminators(CaptchaValidator, field=IEffectiveContact['captcha'])
|