From 3c7887adbde8c01af7e17b4a2d69c604346335de Mon Sep 17 00:00:00 2001 From: asleal2 Date: Tue, 17 Apr 2018 10:46:32 -0500 Subject: [PATCH] adding database functionality --- "\\" | 104 ++++++++++++++++++++++++ __pycache__/mocha_server.cpython-36.pyc | Bin 2126 -> 2502 bytes mocha_server.py | 12 ++- test.py | 3 + 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 "\\" diff --git "a/\\" "b/\\" new file mode 100644 index 0000000..fd0ee7e --- /dev/null +++ "b/\\" @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +""" +Handle API requests to the database +""" +import json +import sqlite3 + + +# DATABASE = '/usr/local/www/mocha-server/mocha.db' +DATABASE = 'mocha.db' + +conn = sqlite3.connect(DATABASE) +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): + """ + + """ + cursor.execute("select * from users order by p0 desc limit p1", {'p0':search_parameter, 'p1':N}) + conn.commit() + conn.close() + + +# 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 + This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates + """ + + cursor.execute("insert into users values (?,?)", (user_id, username)) + conn.commit() + conn.close() + + +def fetch_user(user_id): + """ + Returns a JSON object containing the requested user + Also can return a list of all users if user_id == "*" + """ + + if user_id != '*': # must use (?), (item,) format + cursor.execute("SELECT * FROM users WHERE user_id=(?)", (user_id,)) + else: + cursor.execute("SELECT * FROM users") + + output = cursor.fetchall() + output = json.dumps([dict(row) for row in output]) + conn.close() + + if output == '[]': + output = None + + return output + + +def process_request(uri): + """ + Handles the API endpoint. + Currently supports: + - /mocha/users/"user_id" Returns JSON of the specified user + - /mocha/users/* Returns JSON list of all users + """ + parts = uri.split('/')[1:] + assert parts[0] == 'mocha' + + output = None + + if parts[1] == 'users': + output = fetch_user(parts[2]) + + return output + + + +def application(environ, start_response): + """ + mod_wsgi entry point + """ + status = '200 OK' + output = process_request(environ['REQUEST_URI']) + + if output is None: + status = '404 Not Found' + output = '' + + output = output.encode('UTF-8') + + response_headers = [('Content-type', 'application/json'), + ('Content-Length', str(len(output)))] + + start_response(status, response_headers) + + return [output] diff --git a/__pycache__/mocha_server.cpython-36.pyc b/__pycache__/mocha_server.cpython-36.pyc index 1091d27bd2dc73960be4c144908a84fe04441576..29b16bfd18d02e28972a817a8aac34b0b79b28ea 100644 GIT binary patch delta 1103 zcmZuwy>HYo6px+UT`ukQj(!0{i=_f;I#ey4K&1johYk%A6c7;*rHQYXk~;_6Y1LEF z4Fy(IWo1TU?n+lCgqRpIu(0z7F!7udP#swE&(D7L`}n=*&y9~`GfTCa^=|U3yHVA& z@7k$>LB0V~G>>MU)<(kmOU)e#@jcCM9KF8XD}icNsV@UaZ3l(uKs!Joev1z=(j?x| z54B=7)EesW(CAKB1V+>$31{1!3Q_F4vx3GnkYtWDIct+tP%a4LA?0LipCk(;j@pqV z$)b&2oGie^i%#|houA9g0c(RUF9k6Z6!Z;2eIB&D#OHpSO3HKOyw|vyji6i8)om(0 z$r8_*RI!%YFmu&SG_NRYO_ zPxcspMz*MgEc0cz7rip63UybT+2S`S9*U!a?K`{LCcTh+}IJU zlb6D@orr#RzU`ws0 NX_>k?WKNi4zW|Oz_+$V8 delta 746 zcmZ8fzi-qq6t*4b;)J{Fk1K~%wNgbFvYZeVLP({mfT=?n*doyoIj+bliG%G3hO0Vs zM=C)`{0Yqb15At^8N9Oh4`AXsAXHr>fA8hX`|&-${Fq#=?(U67(b@QCLK#B8(2a+H ze+WmPT<)Iu%#9FXc!(KRL-09v z3_NZE4}JUY9pOD!B*vEJ1vfFVWOhICpu$q|-_d?qXalmEgIV{)e{dc^o^mY`b}9^i zla-~Vb1iG@=WW$!6}o`PRVTfCJCLnuTC-HyMtyqfX1fc~dK~}_7+xh2h~K~rkP@{4 z?Ee8k{I&lIA3HMk60cQ7;y0NxT1|jjZn4rR$%WRb5^r0f%_E4>?nY3s4t*#Y;vrF+ z-Iw5cwB_=*0b&ZwC-xXmx+^x_TDt68S<{pS&rBifqmSYCn$sSSZIGt9>k` z!|p{mP5QfX|4zS#eiY?a)DL_^Y~Q2bCEKrL-j?FPQF;nMVT+7X5;+?(k1=+eP5uBU CXp-^( diff --git a/mocha_server.py b/mocha_server.py index 33af5cd..dd84dbe 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -19,6 +19,15 @@ cursor = conn.cursor() # 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): + """ + in progress + """ + cursor.execute("select * from users order by p0 desc limit p1", {'p0':search_parameter, 'p1':N}) + conn.commit() + conn.close() + + # add new parameters as needed def update_row(user_id, updated_username): print() @@ -29,8 +38,7 @@ def insert_row(user_id, username): This may work better with AUTOINCREMENT to avoid arbitrary ids and duplicates """ - item = (user_id, username) - cursor.execute('insert into users values (?,?)', item) + cursor.execute("insert into users values (?,?)", (user_id, username)) conn.commit() conn.close() diff --git a/test.py b/test.py index 2950d04..b65663c 100644 --- a/test.py +++ b/test.py @@ -3,3 +3,6 @@ import mocha_server #mocha_server.insert_row(4, 'andrew') print(mocha_server.fetch_user(3)) + +print(mocha_server.get_top_N('username', 2)) +