Mercurial > p > roundup > code
changeset 1927:f5e8aeb1382d
Add 'safeget' method to hyperdb, including tests...
...and use it to simplify code in roundupdb.
| author | Johannes Gijsbers <jlgijsbers@users.sourceforge.net> |
|---|---|
| date | Sun, 16 Nov 2003 19:59:10 +0000 |
| parents | 3bdd34547fa7 |
| children | 7c1ddebe7589 |
| files | roundup/hyperdb.py roundup/roundupdb.py test/db_test_base.py |
| diffstat | 3 files changed, 42 insertions(+), 33 deletions(-) [+] |
line wrap: on
line diff
--- a/roundup/hyperdb.py Sun Nov 16 18:41:40 2003 +0000 +++ b/roundup/hyperdb.py Sun Nov 16 19:59:10 2003 +0000 @@ -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 @@ ''' 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
--- a/roundup/roundupdb.py Sun Nov 16 18:41:40 2003 +0000 +++ b/roundup/roundupdb.py Sun Nov 16 19:59:10 2003 +0000 @@ -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 @@ 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 @@ 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') + + 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) + # 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 @@ 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:
--- a/test/db_test_base.py Sun Nov 16 18:41:40 2003 +0000 +++ b/test/db_test_base.py Sun Nov 16 19:59:10 2003 +0000 @@ -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 @@ 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
