changeset 540:2cc5457e6610

Find a user's email by only fetching their session instead of all known users
author mgood
date Mon, 23 Mar 2009 00:37:55 +0000
parents 01f1ee3c8a4f
children 2479e2a75afa
files bitten/notify.py bitten/tests/notify.py
diffstat 2 files changed, 14 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/bitten/notify.py
+++ b/bitten/notify.py
@@ -8,6 +8,7 @@
 
 from trac.core import *
 from trac.web.chrome import ITemplateProvider
+from trac.web.session import DetachedSession
 from trac.config import BoolOption
 from trac.notification import NotifyEmail
 from bitten.api import IBuildListener
@@ -162,9 +163,7 @@
 
     def get_recipients(self, resid):
         author = self.build_info.author
-        user_emails = dict([(username, email) for username, name, email
-                            in self.env.get_known_users(None)])
-        author = user_emails.get(author) or author
+        author = DetachedSession(self.env, author).get('email') or author
         torecipients = [author]
         ccrecipients = []
         return (torecipients, ccrecipients)
@@ -175,4 +174,3 @@
             'X-Trac-Build-URL': self.build_info.link,
         }
         NotifyEmail.send(self, torcpts, ccrcpts, mime_headers)
-
--- a/bitten/tests/notify.py
+++ b/bitten/tests/notify.py
@@ -13,6 +13,7 @@
 
 from trac.db import DatabaseManager
 from trac.test import EnvironmentStub, Mock
+from trac.web.session import DetachedSession
 from bitten.model import *
 from bitten.notify import *
 
@@ -142,21 +143,25 @@
                 'recipient list should contain plain author')
 
     def test_notification_uses_custom_address(self):
-        self.env.get_known_users = lambda cnx = None : [('author',
-                'Author\'s Name',
-                'author@email.com')]
+        self.add_known_user('author', "Author's Name", 'author@email.com')
         self.email.notify(self.build_info)
         self.assertTrue('author@email.com' in self.notifications_sent_to,
-                'recipient list should contain custom author\'s email')
+                "recipient list should contain custom author's email")
 
     def test_notification_discards_invalid_address(self):
-        self.env.get_known_users = lambda cnx = None : [('author',
-                'Author\'s Name',
-                '')]
+        self.add_known_user('author', "Author's Name", email=None)
         self.email.notify(self.build_info)
         self.assertTrue('author' in self.notifications_sent_to,
                 'recipient list should only use valid custom address')
 
+    def add_known_user(self, username, name, email):
+        session = DetachedSession(self.env, username)
+        if name is not None:
+            session['name'] = name
+        if email is not None:
+            session['email'] = email
+        session.save()
+
 
 def suite():
     suite = unittest.TestSuite()
Copyright (C) 2012-2017 Edgewall Software