source: products/qPloneComments/trunk/utils.py @ 1220

Last change on this file since 1220 was 1187, checked in by liebster, 15 years ago

Merged revisions 1946-1951,1957-1962,1964-1965,1967-1970,1972-1986,1990-1993,2199-2200 via svnmerge from
http://svn.quintagroup.com/products/qPloneComments/branches/jcbrand-abuseFeature

........

r1946 | jcbrand | 2009-04-01 16:52:52 +0300 (Срд, 01 Апр 2009) | 3 lines


Add enable_report_abuse property to propertiestool.xml
Add checkbox for this property on the prefs_comments_setup_form.cpt

........

r1947 | jcbrand | 2009-04-01 17:29:20 +0300 (Срд, 01 Апр 2009) | 2 lines


Add report_abuse.pt containing the report_abuse form.

........

r1948 | jcbrand | 2009-04-01 17:30:35 +0300 (Срд, 01 Апр 2009) | 2 lines


Add utility method necesary for the report abuse form.

........

r1949 | jcbrand | 2009-04-01 17:31:05 +0300 (Срд, 01 Апр 2009) | 2 lines


Add the report abuse button and form (hidden).

........

r1950 | jcbrand | 2009-04-01 17:43:31 +0300 (Срд, 01 Апр 2009) | 1 line


Add qcomments.kss and register

........

r1951 | jcbrand | 2009-04-01 17:43:54 +0300 (Срд, 01 Апр 2009) | 1 line


Add qcomments.js and register

........

r1957 | jcbrand | 2009-04-02 12:52:54 +0300 (Чтв, 02 Апр 2009) | 2 lines


Add new mail action for the enable_report_abuse state.

........

r1958 | jcbrand | 2009-04-02 12:54:05 +0300 (Чтв, 02 Апр 2009) | 2 lines


Add a form and new hidden input containing 'comment_id'

........

r1959 | jcbrand | 2009-04-02 12:59:12 +0300 (Чтв, 02 Апр 2009) | 2 lines


Add CommentsKSS kss class and submit_abuse_report method.

........

r1960 | jcbrand | 2009-04-02 13:00:14 +0300 (Чтв, 02 Апр 2009) | 2 lines


Bugfix, preventDefault for all the forms.

........

r1961 | jcbrand | 2009-04-02 13:00:30 +0300 (Чтв, 02 Апр 2009) | 2 lines


remove comment_id passing.

........

r1962 | jcbrand | 2009-04-02 13:00:53 +0300 (Чтв, 02 Апр 2009) | 2 lines


get values from request if able.

........

r1964 | jcbrand | 2009-04-02 13:01:18 +0300 (Чтв, 02 Апр 2009) | 2 lines


Add comments.CommentsKSS

........

r1965 | jcbrand | 2009-04-02 13:01:39 +0300 (Чтв, 02 Апр 2009) | 2 lines


Include browser.

........

r1967 | jcbrand | 2009-04-02 14:26:07 +0300 (Чтв, 02 Апр 2009) | 1 line


Add email template for the report_abuse function

........

r1968 | jcbrand | 2009-04-02 14:26:41 +0300 (Чтв, 02 Апр 2009) | 2 lines


Use the new email template.

........

r1969 | jcbrand | 2009-04-02 14:29:41 +0300 (Чтв, 02 Апр 2009) | 3 lines


Register remove_abuse_report_form as a kss command.
Add new code to it that adds an optional html. (Used for status message)

........

r1970 | jcbrand | 2009-04-02 15:18:24 +0300 (Чтв, 02 Апр 2009) | 3 lines


Add form action.
Add place holder for abuse count info.

........

r1972 | jcbrand | 2009-04-02 16:08:42 +0300 (Чтв, 02 Апр 2009) | 2 lines


Add new portal_properties settings.

........

r1973 | jcbrand | 2009-04-02 19:39:27 +0300 (Чтв, 02 Апр 2009) | 1 line


Add controller python script for abuse reports

........

r1974 | jcbrand | 2009-04-02 19:39:55 +0300 (Чтв, 02 Апр 2009) | 1 line


Add validation script for abuse reporting

........

r1975 | jcbrand | 2009-04-02 19:40:43 +0300 (Чтв, 02 Апр 2009) | 1 line


Add template and metadata for rendering the report abuse form. Moved the macro form report_abuse.pt to this template

........

r1976 | jcbrand | 2009-04-02 19:41:22 +0300 (Чтв, 02 Апр 2009) | 1 line


remove old template

........

r1977 | jcbrand | 2009-04-02 19:41:52 +0300 (Чтв, 02 Апр 2009) | 3 lines


Fix form action.
Fix bug with form nesting.

........

r1978 | jcbrand | 2009-04-02 19:42:46 +0300 (Чтв, 02 Апр 2009) | 1 line


Add new utility methods. Use newly registered kss command

........

r1979 | jcbrand | 2009-04-02 19:43:20 +0300 (Чтв, 02 Апр 2009) | 2 lines


Bugfixes after the introduction of new portal_properties props.

........

r1980 | jcbrand | 2009-04-02 19:44:14 +0300 (Чтв, 02 Апр 2009) | 2 lines


Bugfixes

........

r1981 | jcbrand | 2009-04-02 19:44:31 +0300 (Чтв, 02 Апр 2009) | 2 lines


Register the remove_abuse_report_form kss action.

........

r1982 | jcbrand | 2009-04-03 11:41:32 +0300 (Птн, 03 Апр 2009) | 2 lines


Fix bug whereby emails were sent twice

........

r1983 | jcbrand | 2009-04-03 11:42:08 +0300 (Птн, 03 Апр 2009) | 1 line


Bugfixes related to anonymous comment adding

........

r1984 | jcbrand | 2009-04-03 11:59:44 +0300 (Птн, 03 Апр 2009) | 2 lines


Add macro for the captcha widget.

........

r1985 | jcbrand | 2009-04-03 12:00:22 +0300 (Птн, 03 Апр 2009) | 2 lines


Render a new captcha after hiding form.

........

r1986 | jcbrand | 2009-04-03 12:00:32 +0300 (Птн, 03 Апр 2009) | 2 lines


Change fade out time.

........

r1990 | jcbrand | 2009-04-03 13:37:13 +0300 (Птн, 03 Апр 2009) | 1 line


Add test suite

........

r1991 | jcbrand | 2009-04-03 13:37:38 +0300 (Птн, 03 Апр 2009) | 1 line


remove redundant line

........

r1992 | jcbrand | 2009-04-03 13:48:21 +0300 (Птн, 03 Апр 2009) | 1 line


bugfix

........

r1993 | jcbrand | 2009-04-03 13:56:51 +0300 (Птн, 03 Апр 2009) | 1 line


remove explicit microseconds

........

r2199 | jcbrand | 2009-07-25 02:07:58 +0300 (Сбт, 25 Июл 2009) | 1 line


Fixed a bug with syntax recognition of an older version of jquery

........

r2200 | jcbrand | 2009-07-25 02:08:58 +0300 (Сбт, 25 Июл 2009) | 1 line


Log email errors, so that the report abuse form still closes

........

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1import logging
2import smtplib
3from Products.CMFPlone import MessageFactory
4from Products.CMFCore.utils import getToolByName
5from Products.CMFPlone import PloneMessageFactory as _
6
7log = logging.getLogger('Products.qPloneComments.utils.py')
8
9# Get apropriate property from (propery_sheeet) configlet
10def getProp(self, prop_name, marker=None):
11    result = marker
12    pp = getToolByName(self, 'portal_properties')
13    config_ps = getattr(pp, 'qPloneComments', None)
14    if config_ps:
15        result =  getattr(config_ps, prop_name, marker)
16    return result
17
18def publishDiscussion(self):
19    roles = ['Anonymous']
20    self.review_state = "published"
21    self.manage_permission('View', roles, acquire=1)
22    self._p_changed = 1
23    self.reindexObject()
24
25def setAnonymCommenting(context, allow=False):
26    portal = getToolByName(context, 'portal_url').getPortalObject()
27    if allow:
28        portal.manage_permission('Reply to item', ['Anonymous','Manager','Member'], 1)
29    else:
30        portal.manage_permission('Reply to item', ['Manager','Member'], 1)
31
32def manage_mails(reply, context, action):
33    def sendMails(props, actions, key):
34        for p in props:
35            if p in actions[key]:
36                send_email(reply, context, p)
37
38    prop_sheet = context.portal_properties['qPloneComments']
39    props = filter(lambda x: prop_sheet.getProperty(x), prop_sheet.propertyIds())
40
41    actions = { 'onPublish': ('enable_approve_user_notification',
42                              'enable_reply_user_notification',
43                              'enable_published_notification'),
44                'onDelete' :  ('enable_rejected_user_notification',),
45                'onApprove': ('enable_approve_notification',),
46                'onAnonymousReportAbuse': ('enable_anonymous_report_abuse',),
47                'onAuthenticatedReportAbuse': ('enable_authenticated_report_abuse')}
48
49    if action == 'publishing':
50        sendMails(props, actions, 'onPublish')
51
52    elif action == 'deleting':
53        sendMails(props, actions, 'onDelete')
54
55    elif action == 'aproving':
56        sendMails(props, actions, 'onApprove')
57
58    elif action == 'report_abuse':
59        pm = getToolByName(context, 'portal_membership')
60        if pm.isAnonymousUser():
61            sendMails(props, actions, 'onAnonymousReportAbuse')
62        else:
63            sendMails(props, actions, 'onAuthenticatedReportAbuse')
64
65def getMsg(context, template, args):
66    return getattr(context, template)(**args)
67
68def allowEmail(context, reply, state, creator):
69    condition = getattr(context, 'emailCommentNotification', True)
70    if callable(condition):
71        condition = condition(reply=reply, state=state, creator=creator)
72    return condition
73
74def send_email(reply, context, state):
75    def getEmail(obj, context):
76        email = obj.getProperty('email', None)
77        if email is None:
78            creators = hasattr(obj, 'listCreators') and obj.listCreators() or [obj.Creator(),]
79            userid = creators and creators[0] or ""
80            creator = getToolByName(context, 'portal_membership').getMemberById(userid)
81            if creator and allowEmail(context, reply, state, creator):
82                return creator.getProperty('email', '')
83        else:
84            return email
85        return ''
86
87    def getParent(reply):
88        if reply.meta_type == 'Discussion Item':
89            reply = reply.inReplyTo()
90            return getParent(reply)
91        return reply
92
93    def getDIParent(reply):
94        r = reply.inReplyTo()
95        return r.meta_type == 'Discussion Item' and r or None
96
97    def getParentOwnerEmail(reply, context):
98        creator_id = getParent(reply).getOwnerTuple()[1]
99        creator = getToolByName(context, 'portal_membership').getMemberById(creator_id)
100        if creator and allowEmail(context, reply, state, creator):
101            return creator.getProperty('email', '')
102        return ''
103
104    args = {}
105    if reply:
106        user_email = getEmail(reply, context)
107        reply_parent = getParent(reply)
108
109    organization_name = getProp(context, 'email_subject_prefix', '')
110    creator_name = reply.getOwnerTuple()[1]
111    admin_email = context.portal_url.getPortalObject().getProperty('email_from_address')
112
113    subject = ''
114    if state == 'enable_approve_user_notification':
115        subject = 'Your comment on "%s" is now published' % getParent(context).Title()
116        if user_email:
117            template = 'notify_comment_template'
118            args={'mto': user_email,
119                  'mfrom': admin_email,
120                  'obj': reply_parent,
121                  'organization_name': organization_name,
122                  'name': creator_name}
123        else:
124            args = {}
125
126    elif state == 'enable_rejected_user_notification':
127        subject = 'Your comment on "%s" was not approved' % getParent(context).Title()
128        if user_email:
129            template = 'rejected_comment_template'
130            args={'mto': user_email,
131                  'mfrom': admin_email,
132                  'obj': reply_parent,
133                  'organization_name': organization_name,
134                  'name': creator_name}
135        else:
136            args = {}
137
138    elif state == 'enable_reply_user_notification':
139        template = 'reply_notify_template'
140        subject = 'Someone replied to your comment on "%s"' % getParent(context).Title()
141        di_parrent = getDIParent(reply)
142        if di_parrent:
143            user_email = getEmail(di_parrent, context)
144            if user_email:
145                args={'mto': user_email,
146                      'mfrom': admin_email,
147                      'obj': reply_parent,
148                      'organization_name': organization_name,
149                      'name': di_parrent.getOwnerTuple()[1]}
150            else:
151                args = {}
152        else:
153            args = {}
154
155    elif state == 'enable_published_notification':
156        template = 'published_comment_template'
157        user_email = getParentOwnerEmail(reply, context)
158        if user_email:
159            args={'mto':user_email,
160                  'mfrom':admin_email,
161                  'obj':reply_parent,
162                  'organization_name':organization_name}
163            subject = '[%s] New comment added' % organization_name
164        else:
165            args = {}
166
167    elif state == 'enable_approve_notification':
168        template = 'approve_comment_template'
169        user_email = getProp(context, "email_discussion_manager", None)
170        if user_email:
171            args={'mto':user_email,
172                  'mfrom':admin_email,
173                  'obj':reply_parent,
174                  'organization_name':organization_name}
175            subject = '[%s] New comment awaits moderation' % organization_name
176        else:
177            args = {}
178
179    elif state in ('enable_authenticated_report_abuse', 'enable_anonymous_report_abuse'):
180        template = 'report_abuse_template'
181        user_email = getProp(context, "email_discussion_manager", None)
182        if user_email:
183            message = context.REQUEST.get('message')
184            comment_id = context.REQUEST.get('comment_id')
185            pd = context.portal_discussion
186            dl = pd.getDiscussionFor(context)
187            comment = dl._container.get(comment_id)
188            args = {'mto': user_email,
189                    'mfrom': admin_email,
190                    'obj': reply_parent,
191                    'message':message,
192                    'organization_name': organization_name,
193                    'name': creator_name,
194                    'comment_id':comment_id,
195                    'comment_desc':comment.description,
196                    'comment_text':comment.text
197                    }
198            subject = '[%s] A comment on "%s" has been reported for abuse.' \
199                            % (organization_name, getParent(context).Title())
200        else:
201            args = {}
202
203    if args:
204        msg = getMsg(context, template, args)
205        p_utils = context.plone_utils
206        site_props = context.portal_properties.site_properties
207        host = p_utils.getMailHost()
208        try:
209            host.secureSend(msg, user_email, admin_email,
210                            subject = subject,
211                            subtype = 'plain',
212                            debug = False,
213                            charset = site_props.getProperty('default_charset', 'utf-8'),
214                            From = admin_email)
215        except smtplib.SMTPRecipientsRefused:
216            log.error(_('SMTPRecipientsRefused: Could not send the email'
217            'notification. Have you configured an email server for Plone?'))
218           
219
220
221def setStatusMsg(state, context, msg):
222    context.plone_utils.addPortalMessage(msg)
223
Note: See TracBrowser for help on using the repository browser.