From 0fd9b02c8052274413a346877f0438fff04514ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20S=C3=BCss?= Date: Tue, 20 Jun 2017 15:16:19 +0200 Subject: [PATCH 1/2] Email validation --- CHANGES.rst | 2 +- plone/app/discussion/interfaces.py | 10 +++++++-- .../tests/functional_test_comments.txt | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 200d2b8..3e821e4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,7 +10,7 @@ Breaking changes: New features: -- *add item here* +- Email validation [ksuess] Bug fixes: diff --git a/plone/app/discussion/interfaces.py b/plone/app/discussion/interfaces.py index a8d232c..ead8040 100644 --- a/plone/app/discussion/interfaces.py +++ b/plone/app/discussion/interfaces.py @@ -1,12 +1,18 @@ # -*- coding: utf-8 -*- """Interfaces for plone.app.discussion """ +from plone import api from plone.app.discussion import _ from zope import schema from zope.component.interfaces import IObjectEvent -from zope.interface import Interface +from zope.interface import Interface, Invalid from zope.interface.common.mapping import IIterableMapping +def isEmail(value): + reg_tool = api.portal.get_tool(name='portal_registration') + if not (value and reg_tool.isValidEmail(value)): + raise Invalid(_("Invalid email address.")) + return True class IConversation(IIterableMapping): """A conversation about a content object. @@ -150,7 +156,7 @@ class IComment(Interface): # for anonymous comments only, set to None for logged in comments author_name = schema.TextLine(title=_(u'Name'), required=False) - author_email = schema.TextLine(title=_(u'Email'), required=False) + author_email = schema.TextLine(title=_(u'Email'), required=False, constraint=isEmail) title = schema.TextLine(title=_(u'label_subject', default=u'Subject')) diff --git a/plone/app/discussion/tests/functional_test_comments.txt b/plone/app/discussion/tests/functional_test_comments.txt index e47a5b1..c9fe2bd 100644 --- a/plone/app/discussion/tests/functional_test_comments.txt +++ b/plone/app/discussion/tests/functional_test_comments.txt @@ -514,6 +514,27 @@ Try again. >>> 'Your comment awaits moderator approval' in unprivileged_browser.contents True +Email is being validated. + + >>> unprivileged_browser.getControl(name='form.widgets.text').value = "This is an anonymous comment with email" + >>> unprivileged_browser.getControl(name='form.widgets.author_email').value = "abc" + >>> unprivileged_browser.getControl(name='form.buttons.comment').click() + >>> 'Invalid email address.' in unprivileged_browser.contents + True + >>> 'Your comment awaits moderator approval' in unprivileged_browser.contents + False + +Check again with valid email. + + >>> unprivileged_browser.getControl(name='form.widgets.text').value = "This is an anonymous comment with email" + >>> unprivileged_browser.getControl(name='form.widgets.author_email').value = "email@example.org" + >>> unprivileged_browser.getControl(name='form.buttons.comment').click() + >>> 'Invalid email address.' in unprivileged_browser.contents + False + >>> 'Your comment awaits moderator approval' in unprivileged_browser.contents + True + + Posting as member should still work. Especially it should not complain about missing input for an invisible author_email field. Login as user 'jim'. From 15cf0e6336794088ca17ec0536571b6a1c2f0794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katja=20S=C3=BCss?= Date: Wed, 21 Jun 2017 12:15:32 +0200 Subject: [PATCH 2/2] Removed dependency on plone.api. Following style guidelines: one import per line --- plone/app/discussion/interfaces.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plone/app/discussion/interfaces.py b/plone/app/discussion/interfaces.py index ead8040..5799602 100644 --- a/plone/app/discussion/interfaces.py +++ b/plone/app/discussion/interfaces.py @@ -1,15 +1,19 @@ # -*- coding: utf-8 -*- """Interfaces for plone.app.discussion """ -from plone import api from plone.app.discussion import _ +from Products.CMFCore.interfaces import ISiteRoot +from Products.CMFCore.utils import getToolByName from zope import schema +from zope.component import getUtility from zope.component.interfaces import IObjectEvent -from zope.interface import Interface, Invalid +from zope.interface import Interface +from zope.interface import Invalid from zope.interface.common.mapping import IIterableMapping def isEmail(value): - reg_tool = api.portal.get_tool(name='portal_registration') + portal = getUtility(ISiteRoot) + reg_tool = getToolByName(portal, 'portal_registration') if not (value and reg_tool.isValidEmail(value)): raise Invalid(_("Invalid email address.")) return True