source: products/qPloneComments/branches/plone3_extend/utils.py @ 1591

Last change on this file since 1591 was 800, checked in by crchemist, 17 years ago

Deleted mistakable folder.

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1from Products.CMFPlone import MessageFactory
2from Products.CMFCore.utils import getToolByName
3
4# Get apropriate property from (propery_sheeet) configlet
5def getProp(self, prop_name, marker=None):
6    result = marker
7    pp = getToolByName(self, 'portal_properties')
8    config_ps = getattr(pp, 'qPloneComments', None)
9    if config_ps:
10        result =  getattr(config_ps, prop_name, marker)
11    return result
12
13def publishDiscussion(self):
14    roles = ['Anonymous']
15    self.review_state = "published"
16    self.manage_permission('View', roles, acquire=1)
17    self._p_changed = 1
18    self.reindexObject()
19
20def setAnonymCommenting(context, allow=False):
21    portal = getToolByName(context, 'portal_url').getPortalObject()
22    if allow:
23        portal.manage_permission('Reply to item', ['Anonymous','Manager','Member'], 1)
24    else:
25        portal.manage_permission('Reply to item', ['Manager','Member'], 1)
26
27def manage_mails(reply, context, action):
28    def sendMails(props, actions, key):
29        for p in props:
30            if p in actions[key]:
31                send_email(reply, context, p)
32
33    prop_sheet = context.portal_properties['qPloneComments']
34    props = filter(lambda x: prop_sheet.getProperty(x), prop_sheet.propertyIds())
35
36    actions = { 'onPublish': ('enable_approve_user_notification',
37                              'enable_reply_user_notification',
38                              'enable_published_notification'),
39                'onDelete' :  ('enable_rejected_user_notification',),
40                'onApprove': ('enable_approve_notification',)}
41
42    if action == 'publishing':
43        sendMails(props, actions, 'onPublish')
44
45    elif action == 'deleting':
46        sendMails(props, actions, 'onDelete')
47
48    elif action == 'aproving':
49        sendMails(props, actions, 'onApprove')
50
51def getMsg(context, template, args):
52    return getattr(context, template)(**args)
53
54def allowEmail(context, reply, state, creator):
55    condition = getattr(context, 'emailCommentNotification', True)
56    if callable(condition):
57        condition = condition(reply=reply, state=state, creator=creator)
58    return condition
59
60def send_email(reply, context, state):
61    def getEmail(obj, context):
62        email = obj.getProperty('email', None)
63        if email is None:
64            creators = hasattr(obj, 'listCreators') and obj.listCreators() or [obj.Creator(),]
65            userid = creators and creators[0] or ""
66            creator = getToolByName(context, 'portal_membership').getMemberById(userid)
67            if creator and allowEmail(context, reply, state, creator):
68                return creator.getProperty('email', '')
69        else:
70            return email
71        return ''
72
73    def getParent(reply):
74        if reply.meta_type == 'Discussion Item':
75            reply = reply.inReplyTo()
76            return getParent(reply)
77        return reply
78
79    def getDIParent(reply):
80        r = reply.inReplyTo()
81        return r.meta_type == 'Discussion Item' and r or None
82
83    def getParentOwnerEmail(reply, context):
84        creator_id = getParent(reply).getOwnerTuple()[1]
85        creator = getToolByName(context, 'portal_membership').getMemberById(creator_id)
86        if creator and allowEmail(context, reply, state, creator):
87            return creator.getProperty('email', '')
88        return ''
89
90    args = {}
91    if reply:
92        user_email = getEmail(reply, context)
93        reply_parent = getParent(reply)
94
95    organization_name = getProp(context, 'email_subject_prefix', '')
96    creator_name = reply.getOwnerTuple()[1]
97    admin_email = context.portal_url.getPortalObject().getProperty('email_from_address')
98
99    subject = ''
100    if state == 'enable_approve_user_notification':
101        subject = 'Your comment on "%s" is now published' % getParent(context).Title()
102        if user_email:
103            template = 'notify_comment_template'
104            args={'mto': user_email,
105                  'mfrom': admin_email,
106                  'obj': reply_parent,
107                  'organization_name': organization_name,
108                  'name': creator_name}
109        else:
110            args = {}
111
112    elif state == 'enable_rejected_user_notification':
113        subject = 'Your comment on "%s" was not approved' % getParent(context).Title()
114        if user_email:
115            template = 'rejected_comment_template'
116            args={'mto': user_email,
117                  'mfrom': admin_email,
118                  'obj': reply_parent,
119                  'organization_name': organization_name,
120                  'name': creator_name}
121        else:
122            args = {}
123
124    elif state == 'enable_reply_user_notification':
125        template = 'reply_notify_template'
126        subject = 'Someone replied to your comment on "%s"' % getParent(context).Title()
127        di_parrent = getDIParent(reply)
128        if di_parrent:
129            user_email = getEmail(di_parrent, context)
130            if user_email:
131                args={'mto': user_email,
132                      'mfrom': admin_email,
133                      'obj': reply_parent,
134                      'organization_name': organization_name,
135                      'name': di_parrent.getOwnerTuple()[1]}
136            else:
137                args = {}
138        else:
139            args = {}
140
141    elif state == 'enable_published_notification':
142        template = 'published_comment_template'
143        user_email = getParentOwnerEmail(reply, context)
144        if user_email:
145            args={'mto':user_email,
146                  'mfrom':admin_email,
147                  'obj':reply_parent,
148                  'organization_name':organization_name}
149            subject = '[%s] New comment added' % organization_name
150        else:
151            args = {}
152
153    elif state == 'enable_approve_notification':
154        template = 'approve_comment_template'
155        user_email = getProp(context, "email_discussion_manager", None)
156        if user_email:
157            args={'mto':user_email,
158                  'mfrom':admin_email,
159                  'obj':reply_parent,
160                  'organization_name':organization_name}
161            subject = '[%s] New comment awaits moderation' % organization_name
162        else:
163            args = {}
164
165    if args:
166        msg = getMsg(context, template, args)
167        p_utils = context.plone_utils
168        site_props = context.portal_properties.site_properties
169        host = p_utils.getMailHost()
170        host.secureSend(msg, user_email, admin_email,
171                        subject = subject,
172                        subtype = 'plain',
173                        debug = False,
174                        charset = site_props.getProperty('default_charset', 'utf-8'),
175                        From = admin_email)
176
177def setStatusMsg(state, context, msg):
178    context.plone_utils.addPortalMessage(msg)
Note: See TracBrowser for help on using the repository browser.