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