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=False |
---|
9 | ##title=validates a discussion entry |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from Products.CMFPlone import MessageFactory |
---|
12 | from Products.CMFPlone import PloneMessageFactory |
---|
13 | _ = MessageFactory("quintagroup.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', PloneMessageFactory(u'Please submit a subject.'), 'subject_required') |
---|
26 | |
---|
27 | if not body_text: |
---|
28 | state.setError('body_text', PloneMessageFactory(u'Please submit a body.'), 'body_required') |
---|
29 | |
---|
30 | if require_email: |
---|
31 | if not email: |
---|
32 | state.setError('email', _(u'Please submit an email address.'), 'email_required') |
---|
33 | elif not context.portal_registration.isValidEmail(email): |
---|
34 | state.setError('email', _(u'Please submit a valid e-mail address.'), 'email_required') |
---|
35 | |
---|
36 | if hasattr(context, 'captcha_validator'): |
---|
37 | context.captcha_validator() |
---|
38 | |
---|
39 | pp = getToolByName(context, 'portal_properties') |
---|
40 | isForAnonymous = pp['qPloneComments'].getProperty('enable_anonymous_commenting', None) |
---|
41 | if isForAnonymous: |
---|
42 | pm = getToolByName(context, 'portal_membership') |
---|
43 | isAnonym = pm.isAnonymousUser() |
---|
44 | if isAnonym: |
---|
45 | req = context.REQUEST |
---|
46 | if not (req.form.has_key('Creator') and not req.form['Creator'] == ""): |
---|
47 | state.setError('Creator', _(u'Please enter your name.'), 'name_required') |
---|
48 | |
---|
49 | try: |
---|
50 | captcha_view = context.restrictedTraverse('@@captcha') |
---|
51 | if not captcha_view.verify(captcha): |
---|
52 | state.setError('captcha', _(u'You must correctly enter the word.'), 'captcha') |
---|
53 | except: |
---|
54 | # no collective.captcha installed |
---|
55 | pass |
---|
56 | |
---|
57 | if state.getErrors(): |
---|
58 | context.plone_utils.addPortalMessage(_(u'Please correct the indicated errors.')) |
---|
59 | return state.set(status='failure') |
---|
60 | else: |
---|
61 | return state |
---|