From: jlgijsbers Date: Sun, 16 Nov 2003 19:59:10 +0000 (+0000) Subject: Add 'safeget' method to hyperdb, including tests, and use it to simplify code X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=1809b0ed8359e07e32aee490730728d8f7e04271;p=roundup.git Add 'safeget' method to hyperdb, including tests, and use it to simplify code in roundupdb. git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1996 57a73879-2fb5-44c3-a270-3262357dd7e2 --- diff --git a/roundup/hyperdb.py b/roundup/hyperdb.py index 2933a99..26a6c90 100644 --- a/roundup/hyperdb.py +++ b/roundup/hyperdb.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: hyperdb.py,v 1.92 2003-11-16 18:41:40 jlgijsbers Exp $ +# $Id: hyperdb.py,v 1.93 2003-11-16 19:59:10 jlgijsbers Exp $ """ Hyperdatabase implementation, especially field types. @@ -572,6 +572,12 @@ class Class: ''' raise NotImplementedError + def safeget(self, nodeid, propname, default=None): + try: + return self.get(nodeid, propname) + except (KeyError, IndexError): + return default + class HyperdbValueError(ValueError): ''' Error converting a raw value into a Hyperdb value ''' pass diff --git a/roundup/roundupdb.py b/roundup/roundupdb.py index f69e1fe..60c8fe4 100644 --- a/roundup/roundupdb.py +++ b/roundup/roundupdb.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: roundupdb.py,v 1.93 2003-11-06 19:01:57 jlgijsbers Exp $ +# $Id: roundupdb.py,v 1.94 2003-11-16 19:59:09 jlgijsbers Exp $ __doc__ = """ Extending hyperdb with types specific to issue-tracking. @@ -133,10 +133,8 @@ class IssueClass: If 'msgid' is None, the message gets sent only to the nosy list, and it's called a 'System Message'. """ - authid, recipients = None, [] - if msgid: - authid = self.db.msg.get(msgid, 'author') - recipients = self.db.msg.get(msgid, 'recipients') + authid = self.db.msg.safeget(msgid, 'author') + recipients = self.db.msg.safeget(msgid, 'recipients', []) sendto = [] seen_message = dict([(recipient, 1) for recipient in recipients]) @@ -192,36 +190,30 @@ class IssueClass: users = self.db.user messages = self.db.msg files = self.db.file - - inreplyto, messageid = None, None - if msgid: - inreplyto = messages.get(msgid, 'inreplyto') - messageid = messages.get(msgid, 'messageid') - - # make up a messageid if there isn't one (web edit) - if not messageid: - # this is an old message that didn't get a messageid, so - # create one - messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(), - self.classname, nodeid, - self.db.config.MAIL_DOMAIN) - messages.set(msgid, messageid=messageid) + + inreplyto = messages.safeget(msgid, 'inreplyto') + messageid = messages.safeget(msgid, 'messageid') + + # make up a messageid if there isn't one (web edit) + if not messageid: + # this is an old message that didn't get a messageid, so + # create one + messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(), + self.classname, nodeid, + self.db.config.MAIL_DOMAIN) + messages.set(msgid, messageid=messageid) # send an email to the people who missed out cn = self.classname title = self.get(nodeid, 'title') or '%s message copy'%cn - authid, authname, authaddr = None, '', '' - if msgid: - authid = messages.get(msgid, 'author') - authname = users.get(authid, 'realname') - if not authname: - authname = users.get(authid, 'username') - authaddr = users.get(authid, 'address') - if authaddr: - authaddr = " <%s>" % straddr( ('',authaddr) ) - else: - authaddr = '' + authid = messages.safeget(msgid, 'author') + authname = users.safeget(authid, 'realname') + if not authname: + authname = users.safeget(authid, 'username', '') + authaddr = users.safeget(authid, 'address', '') + if authaddr: + authaddr = " <%s>" % straddr( ('',authaddr) ) # make the message body m = [''] @@ -241,8 +233,7 @@ class IssueClass: m.append('') # add the content - if msgid: - m.append(messages.get(msgid, 'content')) + m.append(messages.safeget(msgid, 'content', '')) # add the change note if note: diff --git a/test/db_test_base.py b/test/db_test_base.py index 57d6e2b..2435134 100644 --- a/test/db_test_base.py +++ b/test/db_test_base.py @@ -15,7 +15,7 @@ # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. # -# $Id: db_test_base.py,v 1.8 2003-11-14 00:11:19 richard Exp $ +# $Id: db_test_base.py,v 1.9 2003-11-16 19:59:06 jlgijsbers Exp $ import unittest, os, shutil, errno, imp, sys, time, pprint @@ -798,6 +798,18 @@ class DBTest(MyTestCase): ae(self.db.user.get('3', 'username'), 'blop') ae(self.db.issue.get('2', 'title'), 'issue two') + def testSafeGet(self): + # existent nodeid, existent property + self.assertEqual(self.db.user.safeget('1', 'username'), 'admin') + # existent nodeid, nonexistent property + self.assertEqual(self.db.user.safeget('1', 'nonexistent'), None) + # nonexistent nodeid, existent property + self.assertEqual(self.db.user.safeget('999', 'username'), None) + # nonexistent nodeid, nonexistent property + self.assertEqual(self.db.user.safeget('999', 'nonexistent'), None) + # different default + self.assertEqual(self.db.issue.safeget('999', 'nosy', []), []) + class ROTest(MyTestCase): def setUp(self): # remove previous test, ignore errors