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