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