summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarian Sigler <m@qjym.de>2012-09-26 03:42:24 +0200
committerMarian Sigler <m@qjym.de>2012-09-26 03:42:24 +0200
commitfc2b6762d5c8c2ac9c5003ddbaa53bdb13a64ed4 (patch)
treedf9e7f53c2307740b6c97209b4419c23b7311fa9
parent7216ec0a7fe4051611990c237652eaaa50e26e42 (diff)
downloadweb-fc2b6762d5c8c2ac9c5003ddbaa53bdb13a64ed4.tar.gz
web-fc2b6762d5c8c2ac9c5003ddbaa53bdb13a64ed4.tar.bz2
web-fc2b6762d5c8c2ac9c5003ddbaa53bdb13a64ed4.zip
account.py: Fix find(); Add get_by_uid()
-rw-r--r--account.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/account.py b/account.py
index 123b07b..e78dc93 100644
--- a/account.py
+++ b/account.py
@@ -52,6 +52,7 @@ class AccountService:
>> all_accounts = service.find() # find all accounts
>> print([x.uid for x in all_accounts])
>> service.find_by_uid('test') # find users by uid
+ >> service.get_by_uid('test') # same, raise NoSuchUserError if no match
>> service.find_by_mail('test@test.de') # find users by mail
>> service.find_by_uid('test*', wildcard=True) # find with wildcards
@@ -91,6 +92,17 @@ class AccountService:
return acc
+ def get_by_uid(self, uid):
+ """
+ Find a single user by uid. Unlike find_by_uid, don't return a list but
+ raise ValueError if there is no such user.
+ """
+ users = self.find_by_uid(uid)
+ if len(users) != 1:
+ raise NoSuchUserError('No such user')
+
+ return users[0]
+
def find_by_uid(self, uid, wildcard=False):
return self.find({'uid':uid}, wildcard)
@@ -121,7 +133,7 @@ class AccountService:
accounts = []
for a in data:
- accounts.append(Account(a[1]['uid'],a[1]['mail']))
+ accounts.append(Account(a[1]['uid'][0], a[1]['mail'][0]))
self._unbind()
@@ -282,3 +294,7 @@ class Account:
new_mail = new_mail.encode('utf8')
self.mail = new_mail
+
+
+class NoSuchUserError(ValueError):
+ pass