| 1 | ## Controller Script Python "validate_talkback" |
|---|
| 2 | ##bind container=container |
|---|
| 3 | ##bind context=context |
|---|
| 4 | ##bind namespace= |
|---|
| 5 | ##bind script=script |
|---|
| 6 | ##bind state=state |
|---|
| 7 | ##bind subpath=traverse_subpath |
|---|
| 8 | ##parameters=subject='',body_text='', email='', require_email |
|---|
| 9 | ##title=validates a discussion entry |
|---|
| 10 | from Products.CMFCore.utils import getToolByName |
|---|
| 11 | from Products.CMFPlone import PloneMessageFactory as _ |
|---|
| 12 | from Products.CMFPlone import MessageFactory |
|---|
| 13 | _ = MessageFactory('plonecomments') |
|---|
| 14 | |
|---|
| 15 | dtool = context.portal_discussion |
|---|
| 16 | try: |
|---|
| 17 | content = context.parentsInThread()[0] |
|---|
| 18 | except AttributeError: |
|---|
| 19 | content = context |
|---|
| 20 | |
|---|
| 21 | if not dtool.isDiscussionAllowedFor(content): |
|---|
| 22 | raise Exception, "Discussion not allowed." |
|---|
| 23 | |
|---|
| 24 | if not subject: |
|---|
| 25 | state.setError('subject', _(u'Please submit a subject.'), 'subject_required') |
|---|
| 26 | |
|---|
| 27 | if not body_text: |
|---|
| 28 | state.setError('body_text', _(u'Please submit a body.'), 'body_required') |
|---|
| 29 | |
|---|
| 30 | if require_email: |
|---|
| 31 | if not (email and context.portal_registration.isValidEmail(email)): |
|---|
| 32 | state.setError('email', _(u'Please submit email.'), 'email_required') |
|---|
| 33 | |
|---|
| 34 | if hasattr(context, 'captcha_validator'): |
|---|
| 35 | context.captcha_validator() |
|---|
| 36 | |
|---|
| 37 | pp = getToolByName(context, 'portal_properties') |
|---|
| 38 | isForAnonymous = pp['qPloneComments'].getProperty('enable_anonymous_commenting', None) |
|---|
| 39 | if isForAnonymous: |
|---|
| 40 | pm = getToolByName(context, 'portal_membership') |
|---|
| 41 | isAnonym = pm.isAnonymousUser() |
|---|
| 42 | if isAnonym: |
|---|
| 43 | req = context.REQUEST |
|---|
| 44 | if not (req.form.has_key('Creator') and not req.form['Creator'] == ""): |
|---|
| 45 | state.setError('Creator', _(u'Please enter your name.'), 'name_required') |
|---|
| 46 | |
|---|
| 47 | try: |
|---|
| 48 | captcha_view = context.restrictedTraverse('@@captcha') |
|---|
| 49 | if not captcha_view.verify(captcha): |
|---|
| 50 | state.setError('captcha', _(u'You must correctly enter the word.'), 'captcha') |
|---|
| 51 | except: |
|---|
| 52 | # no collective.captcha installed |
|---|
| 53 | pass |
|---|
| 54 | |
|---|
| 55 | if state.getErrors(): |
|---|
| 56 | context.plone_utils.addPortalMessage(_(u'Please correct the indicated errors.')) |
|---|
| 57 | return state.set(status='failure') |
|---|
| 58 | else: |
|---|
| 59 | return state |
|---|