Split get_user into _by_id, _by_username.

This commit is contained in:
voussoir 2020-09-17 21:11:42 -07:00
parent 22d16693f3
commit 4bcfd9958a

View file

@ -1242,18 +1242,27 @@ class PDBUserMixin:
return user_id return user_id
def get_user(self, username=None, id=None): def get_user(self, username=None, id=None):
'''
Redirect to get_user_by_id or get_user_by_username.
'''
if not helpers.is_xor(id, username): if not helpers.is_xor(id, username):
raise exceptions.NotExclusive(['id', 'username']) raise exceptions.NotExclusive(['id', 'username'])
if username is not None: if id:
user_row = self.sql_select_one('SELECT * FROM users WHERE username == ?', [username]) return self.get_user_by_id(id)
else: else:
user_row = self.sql_select_one('SELECT * FROM users WHERE id == ?', [id]) return self.get_user_by_username(username)
def get_user_by_id(self, id):
return self.get_thing_by_id('user', id)
def get_user_by_username(self, username):
user_row = self.sql_select_one('SELECT * FROM users WHERE username == ?', [username])
if user_row is None:
raise exceptions.NoSuchUser(username)
if user_row is not None:
return self.get_cached_instance('user', user_row) return self.get_cached_instance('user', user_row)
else:
raise exceptions.NoSuchUser(username or id)
def get_user_count(self): def get_user_count(self):
return self.sql_select_one('SELECT COUNT(id) FROM users')[0] return self.sql_select_one('SELECT COUNT(id) FROM users')[0]