[797] | 1 | ## Controller Python Script "sendto" |
---|
| 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= |
---|
| 9 | ##title=Send an URL to a friend |
---|
| 10 | ## |
---|
| 11 | REQUEST=context.REQUEST |
---|
| 12 | |
---|
| 13 | from Products.CMFPlone.utils import transaction_note |
---|
| 14 | from Products.CMFPlone.PloneTool import AllowSendto |
---|
| 15 | from Products.CMFCore.utils import getToolByName |
---|
[1871] | 16 | from Products.CMFPlone import PloneMessageFactory as _ |
---|
[797] | 17 | from ZODB.POSException import ConflictError |
---|
| 18 | |
---|
| 19 | plone_utils = getToolByName(context, 'plone_utils') |
---|
| 20 | mtool = getToolByName(context, 'portal_membership') |
---|
| 21 | site_properties = getToolByName(context, 'portal_properties').site_properties |
---|
| 22 | pretty_title_or_id = plone_utils.pretty_title_or_id |
---|
| 23 | |
---|
| 24 | if not mtool.checkPermission(AllowSendto, context): |
---|
[1871] | 25 | context.plone_utils.addPortalMessage(_(u'You are not allowed to send this link.'), 'error') |
---|
[797] | 26 | return state.set(status='failure') |
---|
| 27 | |
---|
| 28 | at = getToolByName(context, 'portal_actions') |
---|
| 29 | show = False |
---|
| 30 | actions = at.listActionInfos(object=context) |
---|
| 31 | # Check for visbility of sendto action |
---|
| 32 | for action in actions: |
---|
| 33 | if action['id'] == 'sendto' and action['category'] == 'document_actions': |
---|
| 34 | show = True |
---|
| 35 | if not show: |
---|
[1871] | 36 | context.plone_utils.addPortalMessage(_(u'You are not allowed to send this link.'), 'error') |
---|
[797] | 37 | return state.set(status='failure') |
---|
| 38 | |
---|
| 39 | # Find the view action. |
---|
| 40 | context_state = context.restrictedTraverse("@@plone_context_state") |
---|
| 41 | url = context_state.view_url() |
---|
| 42 | |
---|
| 43 | variables = {'send_from_address' : REQUEST.send_from_address, |
---|
| 44 | 'send_to_address' : REQUEST.send_to_address, |
---|
| 45 | 'subject' : pretty_title_or_id(context), |
---|
| 46 | 'url' : url, |
---|
| 47 | 'title' : pretty_title_or_id(context), |
---|
| 48 | 'description' : context.Description(), |
---|
| 49 | 'comment' : REQUEST.get('comment', None), |
---|
| 50 | 'envelope_from' : site_properties.email_from_address |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | try: |
---|
| 54 | plone_utils.sendto( **variables ) |
---|
| 55 | except ConflictError: |
---|
| 56 | raise |
---|
| 57 | except: # TODO To many things could possibly go wrong. So we catch all. |
---|
| 58 | exception = context.plone_utils.exceptionString() |
---|
[1871] | 59 | message = _(u'Unable to send mail: ${exception}', |
---|
[797] | 60 | mapping={u'exception' : exception}) |
---|
| 61 | context.plone_utils.addPortalMessage(message, 'error') |
---|
| 62 | return state.set(status='failure') |
---|
| 63 | |
---|
| 64 | tmsg='Sent page %s to %s' % (url, REQUEST.send_to_address) |
---|
| 65 | transaction_note(tmsg) |
---|
| 66 | |
---|
[1871] | 67 | context.plone_utils.addPortalMessage(_(u'Mail sent.')) |
---|
[797] | 68 | return state |
---|