add form to contact deputies

This commit is contained in:
Frédéric Péters 2011-11-07 16:25:49 +01:00
parent 2044b0200b
commit c7e8e68c1a
4 changed files with 167 additions and 9 deletions

View File

@ -2,8 +2,7 @@ from five import grok
from Acquisition import aq_inner
from zope.interface import implements
from zope import interface
from zope import schema
from zope import interface, schema, component
from zope.component import getMultiAdapter, provideAdapter
from Products.CMFCore.utils import getToolByName
@ -17,9 +16,13 @@ from plone.dexterity.content import Item
from plone.formwidget.captcha.widget import CaptchaFieldWidget
from plone.formwidget.captcha.validator import CaptchaValidator
from zope.schema import ValidationError
from zope.schema.interfaces import IContextSourceBinder
from zope.schema.vocabulary import SimpleVocabulary
from plone.registry.interfaces import IRegistry
from tabellio.config.interfaces import ITabellioSettings
from tabellio.contact.interfaces import MessageFactory as _
@ -51,7 +54,10 @@ def get_possible_subjects(context):
topic, email = line.strip().split('|')
except ValueError:
continue
terms.append(SimpleVocabulary.createTerm(topic, topic.encode('ascii', 'replace'), topic))
if email == '->deputy':
terms.append(SimpleVocabulary.createTerm('-deputy', '-deputy', topic))
else:
terms.append(SimpleVocabulary.createTerm(topic, topic.encode('ascii', 'replace'), topic))
if len(terms) == 0:
terms.append(SimpleVocabulary.createTerm('-', '-', '-'))
return SimpleVocabulary(terms)
@ -90,12 +96,110 @@ class EffectiveContactForm(form.Form):
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('./')
try:
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(_(u'Your message has been sent successfully.'))
portal = getToolByName(self.context.context, 'portal_url').getPortalObject()
return self.request.response.redirect(portal.absolute_url())
except ValidationError:
pass
return
def cmp_person(x, y):
t = cmp(x.lastname.lower(), y.lastname.lower())
if t: return t
return cmp(x.firstname.lower(), y.lastname.lower())
@grok.provider(IContextSourceBinder)
def get_deputies(context):
portal = getToolByName(context.context, 'portal_url').getPortalObject()
settings = component.getUtility(IRegistry).forInterface(ITabellioSettings, False)
path = settings.deputiesPath
current = portal
for part in settings.deputiesPath.split('/'):
if not part:
continue
current = getattr(current, part)
deputies = []
for object in current.objectValues():
if object.portal_type != 'themis.datatypes.deputy':
continue
if not object.active:
continue
deputies.append(object)
deputies.sort(cmp_person)
terms = []
for deputy in deputies:
deputy_id = deputy.getId()
terms.append(SimpleVocabulary.createTerm(deputy_id, deputy_id, deputy.Title()))
return SimpleVocabulary(terms)
class Deputy(grok.View):
grok.context(IContactForm)
grok.require('zope2.View')
grok.name('deputy')
def deputy_contact_form(self):
effective_contact = EffectiveDeputyContact(self.context)
effective_form = EffectiveDeputyContactForm(effective_contact, self.request)
effective_form.update()
return effective_form.render()
class IEffectiveDeputyContact(interface.Interface):
deputy = schema.Choice(title=_(u'Deputy'), required=True,
source=get_deputies)
subject = schema.TextLine(title=_(u'Subject'), required=True)
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 EffectiveDeputyContact(object):
implements(IEffectiveDeputyContact)
def __init__(self, context=None):
self.context = context
def absolute_url(self):
# this is used by the captcha
return self.context.absolute_url()
class EffectiveDeputyContactForm(form.Form):
fields = field.Fields(IEffectiveDeputyContact)
fields['captcha'].widgetFactory = CaptchaFieldWidget
template = ViewPageTemplateFile('form_templates/view_effectivedeputycontact.pt')
def updateWidgets(self):
super(EffectiveDeputyContactForm, 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
try:
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(_(u'Your message has been sent successfully.'))
portal = getToolByName(self.context.context, 'portal_url').getPortalObject()
return self.request.response.redirect(portal.absolute_url())
except ValidationError:
pass
return

View File

@ -0,0 +1,31 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
lang="en"
metal:use-macro="context/main_template/macros/master"
i18n:domain="tabellio.contact">
<body>
<metal:main fill-slot="main">
<tal:main-macro metal:define-macro="main">
<div tal:replace="structure provider:plone.abovecontenttitle" />
<h1 metal:use-macro="context/kss_generic_macros/macros/generic_title_view">Title</h1>
<div tal:replace="structure provider:plone.belowcontenttitle" />
<div tal:condition="context/Description" id="parent-fieldname-description">
<div tal:replace="context/Description">
Description
</div>
</div>
<div tal:replace="structure view/deputy_contact_form"/>
<div tal:replace="structure provider:plone.belowcontentbody" />
</tal:main-macro>
</metal:main>
</body>
</html>

View File

@ -5,6 +5,19 @@
<form method="post" action="." tal:attributes="action context/absolute_url">
<metal:use use-macro="context/@@ploneform-macros/fields" />
<metal:use use-macro="context/@@ploneform-macros/actions" />
<script type="text/javascript">
(function($) {
$().ready(function() {
$('#form-widgets-subject').change(function() { /* screw accessibility :/ */
console.log($(this));
if ($(this).attr('value') == '-deputy') {
window.location = window.location + '/deputy';
return false;
}
});
});
})(jQuery);
</script>
</form>
</body>
</html>

View File

@ -0,0 +1,10 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:tal="http://xml.zope.org/namespaces/tal">
<body>
<form method="post" action="deputy" tal:attributes="action string:${context/absolute_url}/deputy">
<metal:use use-macro="context/@@ploneform-macros/fields" />
<metal:use use-macro="context/@@ploneform-macros/actions" />
</form>
</body>
</html>