104 lines
3.7 KiB
Python
104 lines
3.7 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 zope.schema.interfaces import IContextSourceBinder
|
|
from zope.schema.vocabulary import SimpleVocabulary
|
|
|
|
from tabellio.contact.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()
|
|
|
|
|
|
@grok.provider(IContextSourceBinder)
|
|
def get_possible_subjects(context):
|
|
terms = []
|
|
for line in context.context.subjects.splitlines():
|
|
try:
|
|
topic, email = line.strip().split('|')
|
|
except ValueError:
|
|
continue
|
|
terms.append(SimpleVocabulary.createTerm(topic, topic.encode('ascii', 'replace'), topic))
|
|
if len(terms) == 0:
|
|
terms.append(SimpleVocabulary.createTerm('-', '-', '-'))
|
|
return SimpleVocabulary(terms)
|
|
|
|
class IEffectiveContact(interface.Interface):
|
|
subject = schema.Choice(title=_(u'Subject'), required=True,
|
|
source=get_possible_subjects)
|
|
name = schema.TextLine(title=_(u'Name'), required=True)
|
|
email = schema.TextLine(title=_(u'Email'), 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')
|
|
|
|
def updateWidgets(self):
|
|
super(EffectiveContactForm, self).updateWidgets()
|
|
self.widgets['message'].rows = 10
|
|
|
|
@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'])
|