[1] | 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 |
---|
[63] | 8 | ##parameters=subject='',body_text='', email='', require_email |
---|
[1] | 9 | ##title=validates a discussion entry |
---|
| 10 | from Products.CMFCore.utils import getToolByName |
---|
| 11 | from Products.CMFPlone import MessageFactory |
---|
[806] | 12 | from Products.CMFPlone import PloneMessageFactory |
---|
[331] | 13 | _ = MessageFactory('plonecomments') |
---|
[1] | 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: |
---|
[806] | 25 | state.setError('subject', PloneMessageFactory(u'Please submit a subject.'), 'subject_required') |
---|
[1] | 26 | |
---|
| 27 | if not body_text: |
---|
[806] | 28 | state.setError('body_text', PloneMessageFactory(u'Please submit a body.'), 'body_required') |
---|
[1] | 29 | |
---|
[124] | 30 | if require_email: |
---|
[1] | 31 | if not (email and context.portal_registration.isValidEmail(email)): |
---|
[331] | 32 | state.setError('email', _(u'Please submit email.'), 'email_required') |
---|
[1] | 33 | |
---|
| 34 | if hasattr(context, 'captcha_validator'): |
---|
| 35 | context.captcha_validator() |
---|
[124] | 36 | |
---|
[1] | 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'] == ""): |
---|
[331] | 45 | state.setError('Creator', _(u'Please enter your name.'), 'name_required') |
---|
[124] | 46 | |
---|
[1] | 47 | try: |
---|
| 48 | captcha_view = context.restrictedTraverse('@@captcha') |
---|
| 49 | if not captcha_view.verify(captcha): |
---|
[331] | 50 | state.setError('captcha', _(u'You must correctly enter the word.'), 'captcha') |
---|
[1] | 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 |
---|