rule manager include message and mail handling working

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2162 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2007-11-09 11:35:02 +00:00
parent fba622c294
commit bc3960ca31
7 changed files with 70 additions and 16 deletions

View file

@ -41,8 +41,10 @@ Working with message instances
>>> from cybertools.composer.message.instance import MessageInstance
>>> mi = MessageInstance(None, manager.messages['feedback_text'])
>>> print mi.applyTemplate()
Dear $person.firstname $person.lastname,
>>> for key, value in mi.applyTemplate().items():
... print key + ':', value
subject:
text: Dear $person.firstname $person.lastname,
You have been registered for the following events.
$@@list_registrations
Best regards, Jack

View file

@ -42,8 +42,8 @@ class MessageManager(object):
def getManager(self):
return self.manager
def addMessage(self, messageName, text):
message = Message(messageName, manager=self)
def addMessage(self, messageName, text, **kw):
message = Message(messageName, manager=self, **kw)
message.text = text
if self.messages is None:
self.messages = self.messagesFactory()
@ -57,8 +57,10 @@ class Message(Template):
name = u''
manager = None
def __init__(self, name, **kw):
def __init__(self, name, text=u'', subject=u'', **kw):
self.name = name
self.text = text
self.subject = subject
for k, v in kw.items():
setattr(self, k, v)

View file

@ -29,6 +29,7 @@ from zope.publisher.browser import TestRequest
from cybertools.composer.instance import Instance
from cybertools.composer.interfaces import IInstance
from cybertools.util.jeep import Jeep
class MessageInstance(Instance):
@ -41,7 +42,9 @@ class MessageInstance(Instance):
def applyTemplate(self, data=None, **kw):
data = DataProvider(self)
return MessageTemplate(self.template.text).safe_substitute(data)
text = MessageTemplate(self.template.text).safe_substitute(data)
subject = self.template.subject
return Jeep((('subject', subject), ('text', text)))
class DataProvider(object):
@ -66,7 +69,7 @@ class DataProvider(object):
#mi = component.getMultiAdapter(
# (client, messageManager.messages[key]), IInstance)
mi = MessageInstance(client, messageManager.messages[key])
return mi.applyTemplate()
return mi.applyTemplate().text
elif '.' in key:
if client is None:
return '$' + key

View file

@ -63,6 +63,11 @@ class IMessage(ITemplate):
title=_(u'Description'),
description=_(u'A brief description of the message.'),
required=False,)
subject = schema.TextLine(
title=_(u'Subject'),
description=_(u'A short text that may be used as the subject '
'line for emails or as a page title for web pages.'),
required=False,)
text = schema.Text(
title=_(u'Text'),
description=_(u"The message text; may contain placeholders "
@ -70,11 +75,18 @@ class IMessage(ITemplate):
"be replaced when the message is rendered."),
required=False,)
format = schema.Choice(
title=_(u'Text format'),
description=_(u'The format of the message.'),
title=_(u'Text input format'),
description=_(u'The format in which the message is entered.'),
required=True,
default='text/plain',
values=('text/plain', 'text/html',))
outputFormat = schema.Choice(
title=_(u'Output format'),
description=_(u'The format in which the message will be '
'rendered when delivered to the recipient.'),
required=False,
default='text/plain',
values=('text/plain', 'text/html',))
media = schema.Choice(
title=_(u'Output media'),
description=_(u'The media type on which the message will '

View file

@ -22,12 +22,39 @@ Action handler for sending emails.
$Id$
"""
from email.MIMEText import MIMEText
from zope import component
from zope.interface import implements
from zope.sendmail.interfaces import IMailDelivery
from cybertools.composer.interfaces import IInstance
from cybertools.composer.rule.base import ActionHandler
class MailActionHandler(ActionHandler):
pass
def __call__(self, data, params={}):
#print 'sending mail...'
#print 'subject:', data.subject
#print 'text:', data.text
#print 'params:', params
sender = params.get('sender', 'unknown')
client = self.context.context
clientData = IInstance(client).applyTemplate()
recipient = clientData['standard.email']
msg = self.prepareMessage(data.subject, data.text, sender, recipient)
data['mailInfo'] = self.sendMail(msg.as_string(), sender, [recipient])
return data
def prepareMessage(self, subject, text, sender, recipient):
text = text.encode('utf-8')
msg = MIMEText(text, 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipient
return msg
def sendMail(self, message, sender, recipients):
mailhost = component.getUtility(IMailDelivery, 'Mail')
mailhost.send(sender, recipients, message)
return 'Mail sent to %s.' % ', '.join(recipients)

View file

@ -1,15 +1,23 @@
# $Id$
import unittest, doctest
from email import message_from_string
from zope.interface import implements
from zope.sendmail.interfaces import IMailDelivery
from zope.testing.doctestunit import DocFileSuite
from cybertools.composer.rule.base import ActionHandler
class TestMailer(object):
class MailActionHandler(ActionHandler):
implements(IMailDelivery)
def __call__(self, data, event, params={}):
pass
def send(self, sender, recipients, message):
print 'sender:', sender
print 'recipients:', recipients
msg = message_from_string(message)
print 'subject:', msg['Subject']
print 'message:'
print msg.get_payload(decode=True)
class Test(unittest.TestCase):

View file

@ -369,8 +369,8 @@ class MessageManagerAdapter(MessageManager):
def __init__(self, context):
self.context = context
def addMessage(self, messageName, text):
super(MessageManagerAdapter, self).addMessage(messageName, text)
def addMessage(self, messageName, text, **kw):
super(MessageManagerAdapter, self).addMessage(messageName, text, **kw)
self.context.messages = self.messages
@Lazy