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