Add fetch_users
This commit is contained in:
parent
3230aba345
commit
fd7e3a0628
1 changed files with 9 additions and 5 deletions
|
@ -13,7 +13,7 @@ if not path.exists(DATABASE):
|
||||||
# TODO: Add fetching of top N users by score
|
# TODO: Add fetching of top N users by score
|
||||||
# TODO: Add ability to store and retrieve avatars (as image files?)
|
# 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.
|
Gets a list of users searching by name.
|
||||||
This can also easily be done by user_id.
|
This can also easily be done by user_id.
|
||||||
|
@ -22,8 +22,8 @@ def get_users(username_list):
|
||||||
conn.row_factory = sqlite3.Row
|
conn.row_factory = sqlite3.Row
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
output = []
|
output = []
|
||||||
for usr in username_list:
|
for user_id in user_ids:
|
||||||
cursor.execute('select * from users where username=(?)', (usr, ))
|
cursor.execute('select * from users where user_id=(?)', (user_id, ))
|
||||||
output += cursor.fetchall()
|
output += cursor.fetchall()
|
||||||
|
|
||||||
output = json.dumps([dict(row) for row in output])
|
output = json.dumps([dict(row) for row in output])
|
||||||
|
@ -136,6 +136,9 @@ def process_request(uri):
|
||||||
if len(parts) < 2:
|
if len(parts) < 2:
|
||||||
output = None
|
output = None
|
||||||
elif parts[1] == 'users' and len(parts) > 2:
|
elif parts[1] == 'users' and len(parts) > 2:
|
||||||
|
if ',' in parts[2]:
|
||||||
|
output = fetch_users(parts[2].split(','))
|
||||||
|
else:
|
||||||
output = fetch_user(parts[2])
|
output = fetch_user(parts[2])
|
||||||
elif parts[1] == 'top' and len(parts) > 2:
|
elif parts[1] == 'top' and len(parts) > 2:
|
||||||
output = fetch_top_n(parts[2])
|
output = fetch_top_n(parts[2])
|
||||||
|
@ -165,9 +168,10 @@ def application(environ, start_response):
|
||||||
|
|
||||||
return [output]
|
return [output]
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
USERNAMES = ['andrew', 'Corder']
|
USERNAMES = ['andrew', 'Corder']
|
||||||
print(get_users(USERNAMES))
|
print(fetch_users(USERNAMES))
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
|
||||||
|
|
Reference in a new issue