diff --git a/__pycache__/mocha_server.cpython-36.pyc b/__pycache__/mocha_server.cpython-36.pyc index 29b16bf..7c2ae9e 100644 Binary files a/__pycache__/mocha_server.cpython-36.pyc and b/__pycache__/mocha_server.cpython-36.pyc differ diff --git a/mocha_server.py b/mocha_server.py index dd84dbe..536f204 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -5,6 +5,7 @@ Handle API requests to the database import json import sqlite3 +# NOTE: closing a database makes the next query in a script not work # DATABASE = '/usr/local/www/mocha-server/mocha.db' DATABASE = 'mocha.db' @@ -14,24 +15,60 @@ conn.row_factory = sqlite3.Row cursor = conn.cursor() - # TODO: Add fetching of list of users # TODO: Add fetching of top N users by score # TODO: Add ability to store and retrieve avatars (as image files?) -def get_top_N(search_parameter, N): +def get_users(username_list): """ - in progress - """ - cursor.execute("select * from users order by p0 desc limit p1", {'p0':search_parameter, 'p1':N}) + Gets a list of users searching by name. + This can also easily be done by user_id. + """ + output = [] + for usr in username_list: + cursor.execute("select * from users where username=(?)", (usr,)) + output += cursor.fetchall() + + output = json.dumps([dict(row) for row in output]) conn.commit() - conn.close() + #conn.close() + + if output == '[]': + output = None + + return output + + +def get_top_N(search_parameter, desc, N): + """ + In progress. + Currently the query seems to work, but returning a dict does not preserve order. + + Store an orderd id list and put that in the json? + """ + if desc == True: + cursor.execute("select * from users order by ? desc limit ?", (search_parameter, N)) + else: + cursor.execute("select * from users order by ? asc limit ?", (search_parameter, N)) + + output = cursor.fetchall() + #print(output) + + output = json.dumps([dict(row) for row in output]) + conn.commit() + #conn.close() + + if output == '[]': + output = None + + return output # add new parameters as needed def update_row(user_id, updated_username): print() + def insert_row(user_id, username): """ Inserts a row for a NEW user with given parameters @@ -40,7 +77,7 @@ def insert_row(user_id, username): cursor.execute("insert into users values (?,?)", (user_id, username)) conn.commit() - conn.close() + #conn.close() def fetch_user(user_id): @@ -56,7 +93,8 @@ def fetch_user(user_id): output = cursor.fetchall() output = json.dumps([dict(row) for row in output]) - conn.close() + conn.commit() + #conn.close() if output == '[]': output = None @@ -82,7 +120,6 @@ def process_request(uri): return output - def application(environ, start_response): """ mod_wsgi entry point diff --git a/test.py b/test.py index b65663c..9cf54e4 100644 --- a/test.py +++ b/test.py @@ -2,7 +2,11 @@ import mocha_server #mocha_server.insert_row(4, 'andrew') -print(mocha_server.fetch_user(3)) +#print(mocha_server.fetch_user(3)) -print(mocha_server.get_top_N('username', 2)) +#print(mocha_server.get_top_N('user_id', False, 3)) + +username_list = ['andrew', 'Corder'] + +print(mocha_server.get_users(username_list))