adding database functionality
This commit is contained in:
parent
3c7887adbd
commit
9e2ff9936b
3 changed files with 52 additions and 11 deletions
Binary file not shown.
|
@ -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
|
||||
Gets a list of users searching by name.
|
||||
This can also easily be done by user_id.
|
||||
"""
|
||||
cursor.execute("select * from users order by p0 desc limit p1", {'p0':search_parameter, 'p1':N})
|
||||
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
|
||||
|
|
8
test.py
8
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))
|
||||
|
||||
|
|
Reference in a new issue