[Checkins] SVN: zc.notification/trunk/src/zc/notification/ Make it
possible to pass an explicit context. Needs some explicit tests.
Gary Poster
gary at zope.com
Tue Oct 3 13:43:50 EDT 2006
Log message for revision 70510:
Make it possible to pass an explicit context. Needs some explicit tests.
Changed:
U zc.notification/trunk/src/zc/notification/__init__.py
U zc.notification/trunk/src/zc/notification/email/README.txt
U zc.notification/trunk/src/zc/notification/email/notifier.py
U zc.notification/trunk/src/zc/notification/interfaces.py
U zc.notification/trunk/src/zc/notification/notification.py
U zc.notification/trunk/src/zc/notification/tests.py
-=-
Modified: zc.notification/trunk/src/zc/notification/__init__.py
===================================================================
--- zc.notification/trunk/src/zc/notification/__init__.py 2006-10-03 17:43:07 UTC (rev 70509)
+++ zc.notification/trunk/src/zc/notification/__init__.py 2006-10-03 17:43:50 UTC (rev 70510)
@@ -21,12 +21,13 @@
import interfaces
-def notify(notification):
+def notify(notification, context=None):
"""Dispatch a notification.
This takes care of the dance to get the notification utility and
send the notification.
"""
- utility = zope.component.getUtility(interfaces.INotificationUtility)
- utility.notify(notification)
+ utility = zope.component.getUtility(
+ interfaces.INotificationUtility, context=context)
+ utility.notify(notification, context)
Modified: zc.notification/trunk/src/zc/notification/email/README.txt
===================================================================
--- zc.notification/trunk/src/zc/notification/email/README.txt 2006-10-03 17:43:07 UTC (rev 70509)
+++ zc.notification/trunk/src/zc/notification/email/README.txt 2006-10-03 17:43:50 UTC (rev 70510)
@@ -110,9 +110,9 @@
... zope.app.principalannotation.interfaces.IPrincipalAnnotationUtility)
>>> user1 = annotations.getAnnotationsById("user1")
-As for all notifiers, we can just use the `send()` method::
+As with all notifiers, we can just use the `send()` method::
- >>> notifier.send(n, "user1", user1)
+ >>> notifier.send(n, "user1", user1, None)
Since our test mailer collects information from the calls to its
`send()` method, we can examine what was done::
Modified: zc.notification/trunk/src/zc/notification/email/notifier.py
===================================================================
--- zc.notification/trunk/src/zc/notification/email/notifier.py 2006-10-03 17:43:07 UTC (rev 70509)
+++ zc.notification/trunk/src/zc/notification/email/notifier.py 2006-10-03 17:43:50 UTC (rev 70510)
@@ -25,10 +25,9 @@
import zope.app.container.interfaces
import zope.app.container.contained
+import zope.app.security.interfaces
import zope.sendmail.interfaces
-from zope.app import zapi
-
import zc.notification.email.interfaces
@@ -48,14 +47,18 @@
fromAddress = None
fromName = None
- def send(self, notification, principal_id, annotations):
- address = self.email_lookup.getAddress(principal_id, annotations)
+ def send(self, notification, principal_id, annotations, context):
+ address = self.email_lookup(context).getAddress(
+ principal_id, annotations)
if address:
# send some email
- principal = zapi.principals().getPrincipal(principal_id)
+ principal = zope.component.getUtility(
+ zope.app.security.interfaces.IAuthentication,
+ context=context).getPrincipal(principal_id)
view = zope.component.getMultiAdapter(
(notification, principal),
- zc.notification.email.interfaces.IEmailView)
+ zc.notification.email.interfaces.IEmailView,
+ context=context)
if self.fromName:
response = ("From: %s <%s>\r\n"
% (self.fromName, self.fromAddress))
@@ -63,27 +66,31 @@
response = "From: %s\r\n" % self.fromAddress
response += "To: %s\r\n" % address
response += view.render()
- self.mailer.send(self.fromAddress, [address], response)
+ self.mailer(context).send(self.fromAddress, [address], response)
else:
_log.info("No email address for principal id %r." % principal_id)
_v_email_lookup_utility = None
_v_mailer = None
+ _v_context = None
- @property
- def email_lookup(self):
- if self._v_email_lookup_utility is None:
+ def email_lookup(self, context):
+ if (self._v_email_lookup_utility is None or
+ context is not self._v_context):
utility = zope.component.getUtility(
- zc.notification.email.interfaces.IEmailLookupUtility)
+ zc.notification.email.interfaces.IEmailLookupUtility,
+ context=context)
self._v_email_lookup_utility = utility
+ self._v_context = context
return self._v_email_lookup_utility
- @property
- def mailer(self):
- if self._v_mailer is None:
+ def mailer(self, context):
+ if self._v_mailer is None or context is not self._v_context:
utility = zope.component.getUtility(
- zope.sendmail.interfaces.IMailDelivery)
+ zope.sendmail.interfaces.IMailDelivery,
+ context=context)
self._v_mailer = utility
+ self._v_context = context
return self._v_mailer
Modified: zc.notification/trunk/src/zc/notification/interfaces.py
===================================================================
--- zc.notification/trunk/src/zc/notification/interfaces.py 2006-10-03 17:43:07 UTC (rev 70509)
+++ zc.notification/trunk/src/zc/notification/interfaces.py 2006-10-03 17:43:50 UTC (rev 70510)
@@ -28,7 +28,7 @@
"""
- def notify(notification):
+ def notify(notification, context=None):
"""Process a notification.
`notification` must implement `INotification`.
@@ -116,7 +116,7 @@
"""
- def send(notification, principal_id, annotations):
+ def send(notification, principal_id, annotations, context):
"""Send one notification to one principal.
`notification` must implement `INotification`.
@@ -125,6 +125,10 @@
`annotations` is the annotations object for the principal.
+ `context` is a context in which utilities and other components
+ should be looked up, and may be None (indicating the default
+ look up).
+
"""
Modified: zc.notification/trunk/src/zc/notification/notification.py
===================================================================
--- zc.notification/trunk/src/zc/notification/notification.py 2006-10-03 17:43:07 UTC (rev 70509)
+++ zc.notification/trunk/src/zc/notification/notification.py 2006-10-03 17:43:50 UTC (rev 70510)
@@ -95,11 +95,12 @@
def __init__(self,
name, message, principal_ids, mapping=None, summary=None,
- exclude_ids=frozenset()):
+ exclude_ids=frozenset(), context=None):
super(GroupAwarePrincipalNotification, self).__init__(
name, message, principal_ids, mapping, summary)
self.principals = zope.component.getUtility(
- zope.app.security.interfaces.IAuthentication)
+ zope.app.security.interfaces.IAuthentication,
+ context=context)
self.group_ids = frozenset(
pid for pid in self.principal_ids if
zope.security.interfaces.IGroup.providedBy(
@@ -206,7 +207,7 @@
def getNotificationSubscriptions(self, notification_name):
return self._notifications.get(notification_name, set())
- def notify(self, notification):
+ def notify(self, notification, context=None):
ids = notification.applicablePrincipals(
set(self._notifications.get(notification.name, ())))
@@ -216,8 +217,11 @@
if method:
notifier = zope.component.queryUtility(
zc.notification.interfaces.INotifier,
- name=method)
+ name=method,
+ context=context)
if notifier is None:
notifier = zope.component.getUtility(
- zc.notification.interfaces.INotifier)
- notifier.send(notification, id, self.get_annotations(id))
+ zc.notification.interfaces.INotifier,
+ context=context)
+ notifier.send(
+ notification, id, self.get_annotations(id), context)
Modified: zc.notification/trunk/src/zc/notification/tests.py
===================================================================
--- zc.notification/trunk/src/zc/notification/tests.py 2006-10-03 17:43:07 UTC (rev 70509)
+++ zc.notification/trunk/src/zc/notification/tests.py 2006-10-03 17:43:50 UTC (rev 70510)
@@ -48,7 +48,7 @@
def __init__(self, method=""):
self.method = method
- def send(self, notification, principal_id, annotations):
+ def send(self, notification, principal_id, annotations, context):
print notification.name
print notification.message
print principal_id, "by", repr(self.method)
More information about the Checkins
mailing list