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 |
---|
16 | from ZODB.POSException import ConflictError |
---|
17 | |
---|
18 | plone_utils = getToolByName(context, 'plone_utils') |
---|
19 | mtool = getToolByName(context, 'portal_membership') |
---|
20 | site_properties = getToolByName(context, 'portal_properties').site_properties |
---|
21 | pretty_title_or_id = plone_utils.pretty_title_or_id |
---|
22 | empty_title = plone_utils.getEmptyTitle() |
---|
23 | |
---|
24 | if not mtool.checkPermission(AllowSendto, context): |
---|
25 | return state.set( |
---|
26 | status='failure', |
---|
27 | portal_status_message='You are not allowed to send this link.') |
---|
28 | |
---|
29 | at = getToolByName(context, 'portal_actions') |
---|
30 | show = False |
---|
31 | actions = at.listActionInfos(object=context) |
---|
32 | # Check for visbility of sendto action |
---|
33 | for action in actions: |
---|
34 | if action['id'] == 'sendto' and action['category'] == 'document_actions': |
---|
35 | show = True |
---|
36 | if not show: |
---|
37 | return state.set( |
---|
38 | status='failure', |
---|
39 | portal_status_message='You are not allowed to send this link.') |
---|
40 | |
---|
41 | # Try to find the view action. If not found, use absolute_url() |
---|
42 | url = context.absolute_url() |
---|
43 | ti = context.getTypeInfo() |
---|
44 | if ti is not None: |
---|
45 | view = ti.getActionById('view', '') |
---|
46 | if view: |
---|
47 | url = '/'.join((url, view)) |
---|
48 | |
---|
49 | variables = {'send_from_address' : REQUEST.send_from_address, |
---|
50 | 'send_to_address' : REQUEST.send_to_address, |
---|
51 | 'subject' : pretty_title_or_id(context), |
---|
52 | 'url' : url, |
---|
53 | 'title' : pretty_title_or_id(context), |
---|
54 | 'description' : context.Description(), |
---|
55 | 'comment' : REQUEST.get('comment', None), |
---|
56 | 'envelope_from' : site_properties.email_from_address |
---|
57 | } |
---|
58 | |
---|
59 | try: |
---|
60 | plone_utils.sendto( **variables ) |
---|
61 | except ConflictError: |
---|
62 | raise |
---|
63 | except: # TODO To many things could possibly go wrong. So we catch all. |
---|
64 | exception = context.plone_utils.exceptionString() |
---|
65 | message = context.translate("Unable to send mail: ${exception}", |
---|
66 | {'exception': exception}) |
---|
67 | return state.set(status='failure', portal_status_message=message) |
---|
68 | |
---|
69 | tmsg='Sent page %s to %s' % (url, REQUEST.send_to_address) |
---|
70 | transaction_note(tmsg) |
---|
71 | |
---|
72 | return state.set(portal_status_message='Mail sent.') |
---|