Changeset 368 in products for qPloneComments/tags/2.0/utils.py
- Timestamp:
- Jul 4, 2006 2:42:36 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
qPloneComments/tags/2.0/utils.py
- Property svn:eol-style deleted
r242 r368 1 from Products.CMFPlone import MessageFactory2 1 from Products.CMFCore.utils import getToolByName 3 2 … … 11 10 return result 12 11 12 13 # Send notification e-mail on Discussion_Reply 14 # Possible values for state: ["approve", "published"] 15 def send_email(reply, context, state="approve"): 16 # Check is notification active 17 notify = False 18 if state=="approve": 19 notify = getProp(context, "enable_approve_notification", False) 20 elif state=="published": 21 notify = getProp(context, "enable_published_notification", False) 22 if not notify: 23 return 0 24 25 # Get parent object 26 parent = reply.inReplyTo() 27 mt = parent.meta_type 28 while mt == 'Discussion Item': 29 try: 30 parent = parent.inReplyTo() 31 mt = parent.meta_type 32 except: 33 break 34 35 # Get portal admin e-mail 36 portal = getToolByName(context, 'portal_url').getPortalObject() 37 from_address = portal.getProperty('email_from_address') 38 39 # Get email address based on state 40 to_address = None 41 if state=="published": 42 creator_id = parent.Creator() 43 if creator_id: 44 mtool = getToolByName(context, 'portal_membership') 45 creator = mtool.getMemberById(creator_id) 46 if creator: 47 to_address = creator.getProperty('email', None) 48 elif state=="approve": 49 to_address = getProp(context, "email_discussion_manager", None) 50 51 # Combine and send email 52 if to_address: 53 if state=="published": 54 template = getattr(context, 'published_comment_template') 55 elif state=="approve": 56 template = getattr(context, 'approve_comment_template') 57 58 organization_name = getProp(context, "email_subject_prefix", "") 59 message = template(obj=parent, mto=to_address, 60 mfrom=from_address, organization_name=organization_name) 61 try: 62 host = context.MailHost 63 host.send( message ) 64 except: 65 return 0 66 return 1 67 68 13 69 def publishDiscussion(self): 14 70 roles = ['Anonymous'] … … 17 73 self._p_changed = 1 18 74 self.reindexObject() 75 19 76 20 77 def setAnonymCommenting(context, allow=False): … … 25 82 portal.manage_permission('Reply to item', ['Manager','Member'], 1) 26 83 27 def 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 42 if action == 'publishing': 43 sendMails(props, actions, 'onPublish') 44 45 elif action == 'deleting': 46 sendMails(props, actions, 'onDelete') 47 48 elif action == 'aproving': 49 sendMails(props, actions, 'onApprove') 50 51 def getMsg(context, template, args): 52 return getattr(context, template)(**args) 53 54 def allowEmail(context, reply, state, creator): 55 condition = getattr(context, 'emailCommentNotification', True) 56 if callable(condition): 57 condition = condition(reply=reply, state=state, creator=creator) 58 return condition 59 60 def send_email(reply, context, state): 61 def getEmail(obj, context): 62 email = obj.getProperty('email', None) 63 if email is None: 64 creators = hasattr(obj, 'listCreators') and obj.listCreators() or [obj.Creator(),] 65 userid = creators and creators[0] or "" 66 creator = getToolByName(context, 'portal_membership').getMemberById(userid) 67 if creator and allowEmail(context, reply, state, creator): 68 return creator.getProperty('email', '') 69 else: 70 return email 71 return '' 72 73 def getParent(reply): 74 if reply.meta_type == 'Discussion Item': 75 reply = reply.inReplyTo() 76 return getParent(reply) 77 return reply 78 79 def getDIParent(reply): 80 r = reply.inReplyTo() 81 return r.meta_type == 'Discussion Item' and r or None 82 83 def getParentOwnerEmail(reply, context): 84 creator_id = getParent(reply).getOwnerTuple()[1] 85 creator = getToolByName(context, 'portal_membership').getMemberById(creator_id) 86 if creator and allowEmail(context, reply, state, creator): 87 return creator.getProperty('email', '') 88 return '' 89 90 args = {} 91 if reply: 92 user_email = getEmail(reply, context) 93 reply_parent = getParent(reply) 94 95 organization_name = getProp(context, 'email_subject_prefix', '') 96 creator_name = reply.getOwnerTuple()[1] 97 admin_email = context.portal_url.getPortalObject().getProperty('email_from_address') 98 99 subject = '' 100 if state == 'enable_approve_user_notification': 101 subject = 'Your comment on "%s" is now published' % getParent(context).Title() 102 if user_email: 103 template = 'notify_comment_template' 104 args={'mto': user_email, 105 'mfrom': admin_email, 106 'obj': reply_parent, 107 'organization_name': organization_name, 108 'name': creator_name} 109 else: 110 args = {} 111 112 elif state == 'enable_rejected_user_notification': 113 subject = 'Your comment on "%s" was not approved' % getParent(context).Title() 114 if user_email: 115 template = 'rejected_comment_template' 116 args={'mto': user_email, 117 'mfrom': admin_email, 118 'obj': reply_parent, 119 'organization_name': organization_name, 120 'name': creator_name} 121 else: 122 args = {} 123 124 elif state == 'enable_reply_user_notification': 125 template = 'reply_notify_template' 126 subject = 'Someone replied to your comment on "%s"' % getParent(context).Title() 127 di_parrent = getDIParent(reply) 128 if di_parrent: 129 user_email = getEmail(di_parrent, context) 130 if user_email: 131 args={'mto': user_email, 132 'mfrom': admin_email, 133 'obj': reply_parent, 134 'organization_name': organization_name, 135 'name': di_parrent.getOwnerTuple()[1]} 136 else: 137 args = {} 138 else: 139 args = {} 140 141 elif state == 'enable_published_notification': 142 template = 'published_comment_template' 143 user_email = getParentOwnerEmail(reply, context) 144 if user_email: 145 args={'mto':user_email, 146 'mfrom':admin_email, 147 'obj':reply_parent, 148 'organization_name':organization_name} 149 subject = '[%s] New comment added' % organization_name 150 else: 151 args = {} 152 153 elif state == 'enable_approve_notification': 154 template = 'approve_comment_template' 155 user_email = getProp(context, "email_discussion_manager", None) 156 if user_email: 157 args={'mto':user_email, 158 'mfrom':admin_email, 159 'obj':reply_parent, 160 'organization_name':organization_name} 161 subject = '[%s] New comment awaits moderation' % organization_name 162 else: 163 args = {} 164 165 if args: 166 msg = getMsg(context, template, args) 167 p_utils = context.plone_utils 168 site_props = context.portal_properties.site_properties 169 host = p_utils.getMailHost() 170 host.secureSend(msg, user_email, admin_email, 171 subject = subject, 172 subtype = 'plain', 173 debug = False, 174 charset = site_props.getProperty('default_charset', 'utf-8'), 175 From = admin_email) 176 177 def getTranslFunction(context): 178 return MessageFactory('plonecomments') 179 180 def setStatusMsg(state, context, msg): 181 transl = getTranslFunction(context) 182 context.plone_utils.addPortalMessage(transl(msg)) 84
Note: See TracChangeset
for help on using the changeset viewer.