source: products/qPloneComments/branches/jcbrand-abuseFeature/utils.py @ 943

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

Added init.py to tests dir.

  • Property svn:eol-style set to native
File size: 8.1 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                'onAnonymousReportAbuse': ('enable_anonymous_report_abuse',),
42                'onAuthenticatedReportAbuse': ('enable_authenticated_report_abuse')}
43
44    if action == 'publishing':
45        sendMails(props, actions, 'onPublish')
46
47    elif action == 'deleting':
48        sendMails(props, actions, 'onDelete')
49
50    elif action == 'aproving':
51        sendMails(props, actions, 'onApprove')
52
53    elif action == 'report_abuse':
54        pm = getToolByName(context, 'portal_membership')
55        if pm.isAnonymousUser():
56            sendMails(props, actions, 'onAnonymousReportAbuse')
57        else:
58            sendMails(props, actions, 'onAuthenticatedReportAbuse')
59
60def getMsg(context, template, args):
61    return getattr(context, template)(**args)
62
63def allowEmail(context, reply, state, creator):
64    condition = getattr(context, 'emailCommentNotification', True)
65    if callable(condition):
66        condition = condition(reply=reply, state=state, creator=creator)
67    return condition
68
69def send_email(reply, context, state):
70    def getEmail(obj, context):
71        email = obj.getProperty('email', None)
72        if email is None:
73            creators = hasattr(obj, 'listCreators') and obj.listCreators() or [obj.Creator(),]
74            userid = creators and creators[0] or ""
75            creator = getToolByName(context, 'portal_membership').getMemberById(userid)
76            if creator and allowEmail(context, reply, state, creator):
77                return creator.getProperty('email', '')
78        else:
79            return email
80        return ''
81
82    def getParent(reply):
83        if reply.meta_type == 'Discussion Item':
84            reply = reply.inReplyTo()
85            return getParent(reply)
86        return reply
87
88    def getDIParent(reply):
89        r = reply.inReplyTo()
90        return r.meta_type == 'Discussion Item' and r or None
91
92    def getParentOwnerEmail(reply, context):
93        creator_id = getParent(reply).getOwnerTuple()[1]
94        creator = getToolByName(context, 'portal_membership').getMemberById(creator_id)
95        if creator and allowEmail(context, reply, state, creator):
96            return creator.getProperty('email', '')
97        return ''
98
99    args = {}
100    if reply:
101        user_email = getEmail(reply, context)
102        reply_parent = getParent(reply)
103
104    organization_name = getProp(context, 'email_subject_prefix', '')
105    creator_name = reply.getOwnerTuple()[1]
106    admin_email = context.portal_url.getPortalObject().getProperty('email_from_address')
107
108    subject = ''
109    if state == 'enable_approve_user_notification':
110        subject = 'Your comment on "%s" is now published' % getParent(context).Title()
111        if user_email:
112            template = 'notify_comment_template'
113            args={'mto': user_email,
114                  'mfrom': admin_email,
115                  'obj': reply_parent,
116                  'organization_name': organization_name,
117                  'name': creator_name}
118        else:
119            args = {}
120
121    elif state == 'enable_rejected_user_notification':
122        subject = 'Your comment on "%s" was not approved' % getParent(context).Title()
123        if user_email:
124            template = 'rejected_comment_template'
125            args={'mto': user_email,
126                  'mfrom': admin_email,
127                  'obj': reply_parent,
128                  'organization_name': organization_name,
129                  'name': creator_name}
130        else:
131            args = {}
132
133    elif state == 'enable_reply_user_notification':
134        template = 'reply_notify_template'
135        subject = 'Someone replied to your comment on "%s"' % getParent(context).Title()
136        di_parrent = getDIParent(reply)
137        if di_parrent:
138            user_email = getEmail(di_parrent, context)
139            if user_email:
140                args={'mto': user_email,
141                      'mfrom': admin_email,
142                      'obj': reply_parent,
143                      'organization_name': organization_name,
144                      'name': di_parrent.getOwnerTuple()[1]}
145            else:
146                args = {}
147        else:
148            args = {}
149
150    elif state == 'enable_published_notification':
151        template = 'published_comment_template'
152        user_email = getParentOwnerEmail(reply, context)
153        if user_email:
154            args={'mto':user_email,
155                  'mfrom':admin_email,
156                  'obj':reply_parent,
157                  'organization_name':organization_name}
158            subject = '[%s] New comment added' % organization_name
159        else:
160            args = {}
161
162    elif state == 'enable_approve_notification':
163        template = 'approve_comment_template'
164        user_email = getProp(context, "email_discussion_manager", None)
165        if user_email:
166            args={'mto':user_email,
167                  'mfrom':admin_email,
168                  'obj':reply_parent,
169                  'organization_name':organization_name}
170            subject = '[%s] New comment awaits moderation' % organization_name
171        else:
172            args = {}
173
174    elif state in ('enable_authenticated_report_abuse', 'enable_anonymous_report_abuse'):
175        template = 'report_abuse_template'
176        user_email = getProp(context, "email_discussion_manager", None)
177        if user_email:
178            message = context.REQUEST.get('message')
179            comment_id = context.REQUEST.get('comment_id')
180            pd = context.portal_discussion
181            dl = pd.getDiscussionFor(context)
182            comment = dl._container.get(comment_id)
183            args = {'mto': user_email,
184                    'mfrom': admin_email,
185                    'obj': reply_parent,
186                    'message':message,
187                    'organization_name': organization_name,
188                    'name': creator_name,
189                    'comment_id':comment_id,
190                    'comment_desc':comment.description,
191                    'comment_text':comment.text
192                    }
193            subject = '[%s] A comment on "%s" has been reported for abuse.' \
194                            % (organization_name, getParent(context).Title())
195        else:
196            args = {}
197
198    if args:
199        msg = getMsg(context, template, args)
200        p_utils = context.plone_utils
201        site_props = context.portal_properties.site_properties
202        host = p_utils.getMailHost()
203        host.secureSend(msg, user_email, admin_email,
204                        subject = subject,
205                        subtype = 'plain',
206                        debug = False,
207                        charset = site_props.getProperty('default_charset', 'utf-8'),
208                        From = admin_email)
209
210def setStatusMsg(state, context, msg):
211    context.plone_utils.addPortalMessage(msg)
212
Note: See TracBrowser for help on using the repository browser.