logic of the post

pull/46/head
Christopher Usher 6 years ago
parent 4c5b6e4cda
commit e4fc878577

@ -7,6 +7,7 @@ import gevent
import gevent.backdoor import gevent.backdoor
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
import flask import flask
from psycopg2 import sql
import common import common
from common import database from common import database
@ -32,63 +33,96 @@ def cors(app):
@app.route('/thrimshim/<uuid:ident>', methods=['GET', 'POST']) @app.route('/thrimshim/<uuid:ident>', methods=['GET', 'POST'])
def thrimshim(ident): def thrimshim(ident):
"""Comunicate between Thrimbletrimmer and the Wubloader database."""
ident = str(ident)
if flask.request.method == 'POST': if flask.request.method == 'POST':
row = flask.request.json row = flask.request.json
return update_row(ident, row)
else: else:
return get_row(ident)
def get_row(ident):
"""Gets the row from the database with id == ident."""
conn = app.db_manager.get_conn() conn = app.db_manager.get_conn()
with database.transaction(conn): with database.transaction(conn):
results = database.query(conn, 'SELECT * FROM events WHERE id = %s;', str(ident)) results = database.query(conn, """
SELECT *
FROM events
WHERE id = %s;""", ident)
row = results.fetchone() row = results.fetchone()
if row is None: if row is None:
flask.abort(404) return 'Row {} not found'.format(ident), 404
assert row.id == ident
response = row._asdict() response = row._asdict()
response = {key:(response[key].isoformat() if isinstance(response[key], datetime.datetime) else response[key]) for key in response.keys()} response = {key:(response[key].isoformat() if isinstance(response[key], datetime.datetime) else response[key]) for key in response.keys()}
return json.dumps(response) return json.dumps(response)
def update_row(ident, new_row):
"""Updates row of database with id == ident with the edit columns in
new_row.
If a 'video_link' is provided in uodate, interperate this as a manual video
upload and set state to 'DONE'"""
edit_columns = ['allow_holes', 'uploader_whitelist', 'upload_location',
'video_start', 'video_end', 'video_title', 'video_description',
'video_channel', 'video_quality']
row_keys = new_row.keys()
for column in edit_columns + ['state']:
if column not in row_keys:
return 'Missing {} from JSON'.format(column), 400
state_columns = ['state', 'uploader', 'error', 'video_link']
def get_row(ident):
conn = app.db_manager.get_conn() conn = app.db_manager.get_conn()
with database.transaction(conn): with database.transaction(conn):
results = database.query(conn, 'SELECT * FROM events WHERE id = %s;', str(ident)) results = database.query(conn, """
row = results[0] SELECT id, state
assert row.id == ident FROM events
response = row._asdict() WHERE id = %s;""", ident)
response = {key:(response[key].isoformat() if isinstance(response[key], datetime.datetime) else response[key]) for key in response.keys()} old_row = results.fetchone()
return json.dump(response) if old_row is None:
return 'Row {} not found'.format(ident), 404
assert old_row.id == ident
#def query_database(ident):
# if old_row.state not in ['UNEDITED', 'EDITED', 'CLAIMED']:
# select start, end, catagory, description, notes, video_title, video_description, video_start, video_end, state, error return 'Video already published', 400
# from database where id is ident
#
#def set_row(data): if 'video_link' in new_row and new_row['video_link']:
# to_update = unjson(data) new_row['state'] = 'DONE'
# new_row['upload_location'] = 'manual'
# update_database(to_update) else:
# new_row['video_link'] = None
#def update_database(ident, to_update): if new_row['state'] not in ['EDITED']:
# new_row['state'] = 'UNEDITED'
# if state not in ['UNEDITED, EDITED, CLAIMED']:
# return 'Video already published' new_row['uploader'] = None
# new_row['error'] = None
# insert video_title, video_description, video_start, video_end
# #allow_holes, uploader_whitelist, upload_location columns = edit_columns + state_columns
# into database where id == indent build_query = sql.SQL("""
# UPDATE events
# set error to NULL set {}
# set uploader to NULL WHERE id = %(id)s""").format(sql.SQL(", ").join(
# sql.SQL("{} = {}").format(
# if upload_location: sql.Identifier(column), sql.Placeholder(column),
# set state to 'DONE' ) for column in columns
# ))
# if publish: kwargs = {column:new_row[column] for column in columns}
# set state to 'EDITED' kwargs['id'] = ident
# else: with database.transaction(conn):
# set state to 'UNEDITED' result = database.query(conn, build_query, **kwargs)
if result.rowcount != 1:
raise Exception('No row with id {} to update'.format(ident))
logging.info('Row {} updated to state {}'.format(ident, new_row['state']))
return 'Row updated'
def main(host='0.0.0.0', port=8004, connection_string='', backdoor_port=0): def main(host='0.0.0.0', port=8004, connection_string='', backdoor_port=0):

Loading…
Cancel
Save