Commit 84f96845 authored by Yusei Tahara's avatar Yusei Tahara

clean up.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20857 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2badac85
...@@ -139,19 +139,23 @@ class NotificationTool(BaseTool): ...@@ -139,19 +139,23 @@ class NotificationTool(BaseTool):
notifier_list=None, priority_level=None, notifier_list=None, priority_level=None,
is_persistent=False): is_persistent=False):
""" """
This method provides a common API to send messages to users This method provides a common API to send messages to erp5 users
from object actions of worflow scripts. from object actions of worflow scripts.
sender -- a string or a Person object Note that you can't send message to person who don't have his own Person document.
recipient -- a string or a Person object sender -- a login name(reference of Person document) or a Person document
recipient -- a login name(reference of Person document) or a Person document,
a list of thereof a list of thereof
subject -- the subject of the message subject -- the subject of the message
message -- the text of the message (already translated) message -- the text of the message (already translated)
attachment_list -- attached documents (optional) attachment_list -- list of dictionary (optional)
keys are: name, content, mime_type
See buildEmailMessage function above.
priority_level -- a priority level which is used to priority_level -- a priority level which is used to
lookup user preferences and decide lookup user preferences and decide
...@@ -165,60 +169,59 @@ class NotificationTool(BaseTool): ...@@ -165,60 +169,59 @@ class NotificationTool(BaseTool):
TODO: support default notification email TODO: support default notification email
""" """
portal = self.getPortalObject()
catalog_tool = getToolByName(self, 'portal_catalog') catalog_tool = getToolByName(self, 'portal_catalog')
mailhost = getattr(portal, 'MailHost', None)
if mailhost is None:
raise ValueError, "Can't find MailHost."
# Default Values # Find Default Values
portal = self.getPortalObject()
default_from_email = portal.email_from_address default_from_email = portal.email_from_address
default_to_email = getattr(portal, 'email_to_address', default_to_email = getattr(portal, 'email_to_address',
default_from_email) default_from_email)
# Change all strings to object values # Find "From" address
from_address = None
if isinstance(sender, basestring): if isinstance(sender, basestring):
sender = catalog_tool(portal_type='Person', reference=sender)[0] sender = catalog_tool.getResultValue(portal_type='Person', reference=sender)
email_from_address = None
if sender is not None: if sender is not None:
email_value = sender.getDefaultEmailValue() email_value = sender.getDefaultEmailValue()
if email_value is not None: if email_value is not None:
email_from_address = email_value.asText() from_address = email_value.asText()
if not email_from_address: if not from_address:
# If we can not find a from address then # If we can not find a from address then
# we fallback to default values # we fallback to default values
email_from_address = default_from_email from_address = default_from_email
# If no recipient is defined, just send an email to the # Find "To" addresses
# default mail address defined at the CMF site root. to_address_list = []
if recipient is None: if not recipient:
mailhost = getattr(self.getPortalObject(), 'MailHost', None) to_address_list.append(default_to_email)
if mailhost is None: else:
raise AttributeError, "Cannot find a MailHost object" if not isinstance(recipient, (list, tuple)):
mail_message = buildEmailMessage(email_from_address, default_to_email, recipient = (recipient,)
msg=message, subject=subject, for person in recipient:
attachment_list=attachment_list) if isinstance(person, basestring):
return mailhost.send(mail_message.as_string(), default_to_email, email_from_address) person = catalog_tool.getResultValue(portal_type='Person', reference=person)
if person is None:
elif not isinstance(recipient, (list, tuple)): # For backward compatibility. I recommend to use ValueError.(yusei)
# To is a list - let us find all members raise IndexError, "Can't find person document which reference is '%s'" % person
recipient = (recipient, ) email_value = person.getDefaultEmailValue()
if email_value is None:
# Default implementation is to send an active message to everyone # For backward compatibility. I recommend to use ValueError.(yusei)
for person in recipient: raise AttributeError, "Can't find default email address of %s" % person.getRelativeUrl()
if isinstance(person, basestring): to_address_list.append(email_value.asText())
person = catalog_tool(portal_type='Person', reference=person)[0]
email_value = person.getDefaultEmailValue() # Build and Send Messages
if email_value is not None: for to_address in to_address_list:
# Activity can not handle attachment mail_message = buildEmailMessage(from_url=from_address,
# Queuing messages has to be managed by the MTA to_url=to_address,
email_value.send( msg=message,
from_url=email_from_address, subject=subject,
to_url=email_value.asText(), attachment_list=attachment_list
subject=subject, )
msg=message, mailhost.send(mail_message.as_string(), to_address, from_address)
attachment_list=attachment_list)
else:
raise AttributeError, \
"Can not contact the person %s" % person.getReference()
return return
# Future implemetation could consist in implementing # Future implemetation could consist in implementing
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment