From fc2b6762d5c8c2ac9c5003ddbaa53bdb13a64ed4 Mon Sep 17 00:00:00 2001 From: Marian Sigler Date: Wed, 26 Sep 2012 03:42:24 +0200 Subject: account.py: Fix find(); Add get_by_uid() --- account.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'account.py') 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 -- cgit v1.2.3-1-g7c22