RegistrationResource: return any phone/email from phones/emails or None.

This commit is contained in:
Jakub Warmuz
2015-07-08 19:23:06 +00:00
parent 97b09ea1c6
commit 7470bc8db6
2 changed files with 25 additions and 6 deletions
+18 -6
View File
@@ -182,15 +182,27 @@ class Registration(ResourceBody):
@property @property
def phone(self): def phone(self):
"""Phone.""" """Phone.
assert len(self.phones) == 1
return self.phones[0] Picks any phone from `phones` or ``None`` if not available.
"""
try:
return self.phones[0]
except IndexError:
return None
@property @property
def email(self): def email(self):
"""Email.""" """Email.
assert len(self.emails) == 1
return self.emails[0] Picks any email from `emails` or ``None`` if not available.
"""
try:
return self.emails[0]
except IndexError:
return None
class RegistrationResource(ResourceWithURI): class RegistrationResource(ResourceWithURI):
+7
View File
@@ -122,6 +122,7 @@ class RegistrationTest(unittest.TestCase):
self.reg = Registration( self.reg = Registration(
key=key, contact=contact, recovery_token=recovery_token, key=key, contact=contact, recovery_token=recovery_token,
agreement=agreement) agreement=agreement)
self.reg_none = Registration()
self.jobj_to = { self.jobj_to = {
'contact': contact, 'contact': contact,
@@ -149,9 +150,15 @@ class RegistrationTest(unittest.TestCase):
def test_phone(self): def test_phone(self):
self.assertEqual('1234', self.reg.phone) self.assertEqual('1234', self.reg.phone)
def test_phone_none(self):
self.assertTrue(self.reg_none.phone is None)
def test_email(self): def test_email(self):
self.assertEqual('admin@foo.com', self.reg.email) self.assertEqual('admin@foo.com', self.reg.email)
def test_email_none(self):
self.assertTrue(self.reg_none.email is None)
def test_to_partial_json(self): def test_to_partial_json(self):
self.assertEqual(self.jobj_to, self.reg.to_partial_json()) self.assertEqual(self.jobj_to, self.reg.to_partial_json())