Commits

Anonymous committed 80d0c8a

adding events and transactions

  • Participants
  • Parent commits ddde524

Comments (0)

Files changed (5)

pyramid_lamson/connection.py

 import smtplib
 import socket
+import transaction
+
+from transaction.interfaces import IDataManager
 
 from pyramid_lamson.mail.message import Message
 from pyramid_lamson.mail.events import MessageDispatched
 
+class MessageDataManager(object):
+    implements(IDataManager)
+    """
+    This code stolen from repoze.sendmail
+    """
+
+    def __init__(self, callable, args=(), onAbort=None):
+        self.callable = callable
+        self.args = args
+        self.onAbort = onAbort
+        # Use the default thread transaction manager.
+        self.transaction_manager = transaction.manager
+
+    def commit(self, transaction):
+        pass
+
+    def abort(self, transaction):
+        if self.onAbort:
+            self.onAbort()
+
+    def sortKey(self):
+        return id(self)
+
+    # No subtransaction support.
+    def abort_sub(self, transaction):
+        pass  #pragma NO COVERAGE
+
+    commit_sub = abort_sub
+
+    def beforeCompletion(self, transaction):
+        pass  #pragma NO COVERAGE
+
+    afterCompletion = beforeCompletion
+
+    def tpc_begin(self, transaction, subtransaction=False):
+        assert not subtransaction
+
+    def tpc_vote(self, transaction):
+        pass
+
+    def tpc_finish(self, transaction):
+        self.callable(*self.args)
+
+    tpc_abort = abort
+
+
 class Connection(object):
 
     """Handles connection to host."""
         Sends message.
         
         :param message: Message instance.
+
         """
 
+        transaction.get().join(
+            MailDataManager(self._send, args=(message,))
+    
+    def _send(self, message):
+
         if self.host:
             self.host.sendmail(message.sender,
                                message.send_to,

pyramid_lamson/events.py

+from zope.interfaces import implements
+
+from pyramid_lamson.interfaces import IMessageDispatched
+
+class MessageDispatched(object):
+    implements(IMessageDispatched)
+
+    def __init__(self, message):
+        self.message = message
+
+
+

pyramid_lamson/interfaces.py

+from zope.interfaces import Interface
+from zope.interfaces import Attribute
+
+class IDispatchedMessage(Interface):
+
+    message = Attribute("The sent message")

pyramid_lamson/message.py

+import transaction
 
 from lamson.mail import MailResponse
 
     :param cc: CC list
     :param bcc: BCC list
     :param attachments: list of Attachment instances
+
+    
     """
 
     def __init__(self, subject, 

pyramid_lamson/subscribers.py

-
-try:
-    
-    import blinker
-    signals = blinker.Namespace()
-    
-    email_dispatched = signals.signal("email-dispatched", doc="""
-Signal sent when an email is dispatched. This signal will also be sent
-in testing mode, even though the email will not actually be sent.
-    """)
-
-except ImportError:
-    email_dispatched = None
-