|
|
@ -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
|
|
|
|
else:
|
|
|
|
return update_row(ident, row)
|
|
|
|
conn = app.db_manager.get_conn()
|
|
|
|
|
|
|
|
with database.transaction(conn):
|
|
|
|
|
|
|
|
results = database.query(conn, 'SELECT * FROM events WHERE id = %s;', str(ident))
|
|
|
|
|
|
|
|
row = results.fetchone()
|
|
|
|
|
|
|
|
if row is None:
|
|
|
|
|
|
|
|
flask.abort(404)
|
|
|
|
|
|
|
|
response = row._asdict()
|
|
|
|
|
|
|
|
response = {key:(response[key].isoformat() if isinstance(response[key], datetime.datetime) else response[key]) for key in response.keys()}
|
|
|
|
|
|
|
|
return json.dumps(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return get_row(ident)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def 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, """
|
|
|
|
row = results[0]
|
|
|
|
SELECT *
|
|
|
|
|
|
|
|
FROM events
|
|
|
|
|
|
|
|
WHERE id = %s;""", ident)
|
|
|
|
|
|
|
|
row = results.fetchone()
|
|
|
|
|
|
|
|
if row is None:
|
|
|
|
|
|
|
|
return 'Row {} not found'.format(ident), 404
|
|
|
|
assert row.id == ident
|
|
|
|
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.dump(response)
|
|
|
|
return json.dumps(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#def query_database(ident):
|
|
|
|
|
|
|
|
#
|
|
|
|
def update_row(ident, new_row):
|
|
|
|
# select start, end, catagory, description, notes, video_title, video_description, video_start, video_end, state, error
|
|
|
|
"""Updates row of database with id == ident with the edit columns in
|
|
|
|
# from database where id is ident
|
|
|
|
new_row.
|
|
|
|
#
|
|
|
|
|
|
|
|
#def set_row(data):
|
|
|
|
If a 'video_link' is provided in uodate, interperate this as a manual video
|
|
|
|
# to_update = unjson(data)
|
|
|
|
upload and set state to 'DONE'"""
|
|
|
|
#
|
|
|
|
|
|
|
|
# update_database(to_update)
|
|
|
|
edit_columns = ['allow_holes', 'uploader_whitelist', 'upload_location',
|
|
|
|
#
|
|
|
|
'video_start', 'video_end', 'video_title', 'video_description',
|
|
|
|
#def update_database(ident, to_update):
|
|
|
|
'video_channel', 'video_quality']
|
|
|
|
#
|
|
|
|
|
|
|
|
# if state not in ['UNEDITED, EDITED, CLAIMED']:
|
|
|
|
row_keys = new_row.keys()
|
|
|
|
# return 'Video already published'
|
|
|
|
for column in edit_columns + ['state']:
|
|
|
|
#
|
|
|
|
if column not in row_keys:
|
|
|
|
# insert video_title, video_description, video_start, video_end
|
|
|
|
return 'Missing {} from JSON'.format(column), 400
|
|
|
|
# #allow_holes, uploader_whitelist, upload_location
|
|
|
|
|
|
|
|
# into database where id == indent
|
|
|
|
state_columns = ['state', 'uploader', 'error', 'video_link']
|
|
|
|
#
|
|
|
|
|
|
|
|
# set error to NULL
|
|
|
|
conn = app.db_manager.get_conn()
|
|
|
|
# set uploader to NULL
|
|
|
|
with database.transaction(conn):
|
|
|
|
#
|
|
|
|
results = database.query(conn, """
|
|
|
|
# if upload_location:
|
|
|
|
SELECT id, state
|
|
|
|
# set state to 'DONE'
|
|
|
|
FROM events
|
|
|
|
#
|
|
|
|
WHERE id = %s;""", ident)
|
|
|
|
# if publish:
|
|
|
|
old_row = results.fetchone()
|
|
|
|
# set state to 'EDITED'
|
|
|
|
if old_row is None:
|
|
|
|
# else:
|
|
|
|
return 'Row {} not found'.format(ident), 404
|
|
|
|
# set state to 'UNEDITED'
|
|
|
|
assert old_row.id == ident
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if old_row.state not in ['UNEDITED', 'EDITED', 'CLAIMED']:
|
|
|
|
|
|
|
|
return 'Video already published', 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'video_link' in new_row and new_row['video_link']:
|
|
|
|
|
|
|
|
new_row['state'] = 'DONE'
|
|
|
|
|
|
|
|
new_row['upload_location'] = 'manual'
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
new_row['video_link'] = None
|
|
|
|
|
|
|
|
if new_row['state'] not in ['EDITED']:
|
|
|
|
|
|
|
|
new_row['state'] = 'UNEDITED'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new_row['uploader'] = None
|
|
|
|
|
|
|
|
new_row['error'] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
columns = edit_columns + state_columns
|
|
|
|
|
|
|
|
build_query = sql.SQL("""
|
|
|
|
|
|
|
|
UPDATE events
|
|
|
|
|
|
|
|
set {}
|
|
|
|
|
|
|
|
WHERE id = %(id)s""").format(sql.SQL(", ").join(
|
|
|
|
|
|
|
|
sql.SQL("{} = {}").format(
|
|
|
|
|
|
|
|
sql.Identifier(column), sql.Placeholder(column),
|
|
|
|
|
|
|
|
) for column in columns
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
kwargs = {column:new_row[column] for column in columns}
|
|
|
|
|
|
|
|
kwargs['id'] = ident
|
|
|
|
|
|
|
|
with database.transaction(conn):
|
|
|
|
|
|
|
|
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):
|
|
|
|