Add avatar column and getter/setter

This commit is contained in:
Corder Guy 2018-04-28 20:26:48 -05:00
parent 23b48ec7fc
commit ef8c86710b
2 changed files with 34 additions and 5 deletions

Binary file not shown.

View file

@ -161,6 +161,37 @@ def update(user_id, score):
return '[]'
def set_avatar(user_id, new_avatar):
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('UPDATE users SET avatar = (?) WHERE user_id = (?)',
(new_avatar, user_id))
conn.commit()
conn.close()
return '[]'
def get_avatar(user_id):
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute(
'SELECT avatar FROM users WHERE user_id = (?)',
(user_id))
output = cursor.fetchall()
output = json.dumps([dict(row) for row in output])
conn.commit()
conn.close()
if output == '[]':
output = None
return output
def process_request(uri):
'''
Handles the API endpoint.
@ -215,10 +246,8 @@ def application(environ, start_response):
if __name__ == '__main__':
new_score = random.randint(1, 100)
print(fetch_user(1))
print(new_score)
update(1, new_score)
print(fetch_user(1))
print(get_avatar('1'))
set_avatar('1', 'newavatar')
print(get_avatar('1'))
# vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab