1 | ## Script (Python) "discussion_reply" |
---|
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,text_format='plain',username=None,password=None |
---|
9 | ##title=Reply to content |
---|
10 | |
---|
11 | from Products.PythonScripts.standard import url_quote_plus |
---|
12 | from Products.CMFCore.utils import getToolByName |
---|
13 | from quintagroup.plonecomments.utils import manage_mails |
---|
14 | from Products.CMFPlone import MessageFactory |
---|
15 | from Products.CMFPlone import PloneMessageFactory |
---|
16 | _ = MessageFactory("quintagroup.plonecomments") |
---|
17 | |
---|
18 | mtool = getToolByName(context, 'portal_membership') |
---|
19 | dtool = getToolByName(context, 'portal_discussion') |
---|
20 | |
---|
21 | req = context.REQUEST |
---|
22 | pp = getToolByName(context,'portal_properties') |
---|
23 | # Get properties |
---|
24 | isForAnonymous = pp['qPloneComments'].getProperty('enable_anonymous_commenting', False) |
---|
25 | ifModerate = pp['qPloneComments'].getProperty('enable_moderation', False) |
---|
26 | requireEmail = pp['qPloneComments'].getProperty('require_email', False) |
---|
27 | if username or password: |
---|
28 | # The user username/password inputs on on the comment form were used, |
---|
29 | # which might happen when anonymous commenting is enabled. If they typed |
---|
30 | # something in to either of the inputs, we send them to 'logged_in'. |
---|
31 | # 'logged_in' will redirect them back to this script if authentication |
---|
32 | # succeeds with a query string which will post the message appropriately |
---|
33 | # and show them the result. if 'logged_in' fails, the user will be |
---|
34 | # presented with the stock login failure page. This all depends |
---|
35 | # heavily on cookiecrumbler, but I believe that is a Plone requirement. |
---|
36 | came_from = '%s?subject=%s&body_text=%s' % (req['URL'], subject, body_text) |
---|
37 | came_from = url_quote_plus(came_from) |
---|
38 | portal_url = context.portal_url() |
---|
39 | |
---|
40 | return req.RESPONSE.redirect( |
---|
41 | '%s/logged_in?__ac_name=%s' |
---|
42 | '&__ac_password=%s' |
---|
43 | '&came_from=%s' % (portal_url, |
---|
44 | url_quote_plus(username), |
---|
45 | url_quote_plus(password), |
---|
46 | came_from, |
---|
47 | ) |
---|
48 | ) |
---|
49 | |
---|
50 | # if (the user is already logged in) or (if anonymous commenting is enabled and |
---|
51 | # they posted without typing a username or password into the form), we do |
---|
52 | # the following |
---|
53 | |
---|
54 | ######################################################### |
---|
55 | # Get discussion item (reply) author and creating reply # |
---|
56 | comment_creator = req.get('Creator', None) |
---|
57 | if isForAnonymous and comment_creator: |
---|
58 | # Get entered anonymous name |
---|
59 | comment_creator = comment_creator |
---|
60 | else: |
---|
61 | member = mtool.getAuthenticatedMember() |
---|
62 | comment_creator = member.getUserName() |
---|
63 | tb = dtool.getDiscussionFor(context) |
---|
64 | if requireEmail: |
---|
65 | if mtool.isAnonymousUser(): |
---|
66 | email = req.get('email', '') |
---|
67 | else: |
---|
68 | email = mtool.getAuthenticatedMember().getProperty('email') |
---|
69 | |
---|
70 | id = tb.createReply(title=subject, text=body_text, Creator=comment_creator, email=email) |
---|
71 | else: |
---|
72 | id = tb.createReply(title=subject, text=body_text, Creator=comment_creator) |
---|
73 | |
---|
74 | reply = tb.getReply(id) |
---|
75 | |
---|
76 | # TODO THIS NEEDS TO GO AWAY! |
---|
77 | if hasattr(dtool.aq_explicit, 'cookReply'): |
---|
78 | dtool.cookReply(reply, text_format='plain') |
---|
79 | |
---|
80 | parent = tb.aq_parent |
---|
81 | # Send notification e-mail |
---|
82 | |
---|
83 | manage_mails(reply, context, 'aproving') |
---|
84 | if not ifModerate: |
---|
85 | manage_mails(reply, context, 'publishing') |
---|
86 | |
---|
87 | # return to the discussable object. |
---|
88 | redirect_target = context.plone_utils.getDiscussionThread(tb)[0] |
---|
89 | view = redirect_target.getTypeInfo().getActionInfo('object/view', |
---|
90 | redirect_target)['url'] |
---|
91 | anchor = reply.getId() |
---|
92 | |
---|
93 | # Inform user about awaiting moderation |
---|
94 | portal_status_message=_(u'Comment published.') |
---|
95 | if ifModerate and reply: |
---|
96 | portal_status_message=_(u'Currently, all comments require approval before being published. Please check back later.') |
---|
97 | |
---|
98 | from Products.CMFPlone.utils import transaction_note |
---|
99 | transaction_note('Added comment to %s at %s' % (parent.title_or_id(), |
---|
100 | reply.absolute_url())) |
---|
101 | |
---|
102 | context.plone_utils.addPortalMessage(portal_status_message) |
---|
103 | context.plone_utils.addPortalMessage(PloneMessageFactory(u'Comment added.')) |
---|
104 | |
---|
105 | target = '%s#%s' % (view, anchor) |
---|
106 | return req.RESPONSE.redirect(target) |
---|