From fd7e3a0628366acd2abeec853087ccaf48d493a6 Mon Sep 17 00:00:00 2001 From: Corder Guy Date: Tue, 17 Apr 2018 15:14:52 -0500 Subject: [PATCH] Add fetch_users --- mocha_server.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mocha_server.py b/mocha_server.py index ddef071..a4dfab0 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -13,7 +13,7 @@ if not path.exists(DATABASE): # TODO: Add fetching of top N users by score # TODO: Add ability to store and retrieve avatars (as image files?) -def get_users(username_list): +def fetch_users(user_ids): ''' Gets a list of users searching by name. This can also easily be done by user_id. @@ -22,8 +22,8 @@ def get_users(username_list): conn.row_factory = sqlite3.Row cursor = conn.cursor() output = [] - for usr in username_list: - cursor.execute('select * from users where username=(?)', (usr, )) + for user_id in user_ids: + cursor.execute('select * from users where user_id=(?)', (user_id, )) output += cursor.fetchall() output = json.dumps([dict(row) for row in output]) @@ -136,7 +136,10 @@ def process_request(uri): if len(parts) < 2: output = None elif parts[1] == 'users' and len(parts) > 2: - output = fetch_user(parts[2]) + if ',' in parts[2]: + output = fetch_users(parts[2].split(',')) + else: + output = fetch_user(parts[2]) elif parts[1] == 'top' and len(parts) > 2: output = fetch_top_n(parts[2]) else: @@ -165,9 +168,10 @@ def application(environ, start_response): return [output] + if __name__ == '__main__': USERNAMES = ['andrew', 'Corder'] - print(get_users(USERNAMES)) + print(fetch_users(USERNAMES)) # vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab