diff --git a/tabellio/contact/form.py b/tabellio/contact/form.py index c3d8f50..975f866 100644 --- a/tabellio/contact/form.py +++ b/tabellio/contact/form.py @@ -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 diff --git a/tabellio/contact/form_templates/deputy.pt b/tabellio/contact/form_templates/deputy.pt new file mode 100644 index 0000000..472237b --- /dev/null +++ b/tabellio/contact/form_templates/deputy.pt @@ -0,0 +1,31 @@ + + + + + + +
+

Title

+
+ +
+
+ Description +
+
+ +
+ +
+ + + + + + diff --git a/tabellio/contact/form_templates/view_effectivecontact.pt b/tabellio/contact/form_templates/view_effectivecontact.pt index 6e1313e..a1a5aa5 100644 --- a/tabellio/contact/form_templates/view_effectivecontact.pt +++ b/tabellio/contact/form_templates/view_effectivecontact.pt @@ -5,6 +5,19 @@
+ diff --git a/tabellio/contact/form_templates/view_effectivedeputycontact.pt b/tabellio/contact/form_templates/view_effectivedeputycontact.pt new file mode 100644 index 0000000..e263eb7 --- /dev/null +++ b/tabellio/contact/form_templates/view_effectivedeputycontact.pt @@ -0,0 +1,10 @@ + + + +
+ + + + +