More fixes

pull/55/head
Christopher Usher 6 years ago
parent c3c75b1e4f
commit afdad26a51

@ -9,11 +9,13 @@ import gevent
import gevent.backdoor import gevent.backdoor
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
import prometheus_client import prometheus_client
import psycopg2
from psycopg2 import sql from psycopg2 import sql
import common import common
from common import database from common import database
psycopg2.extras.register_uuid()
app = flask.Flask('thrimshim') app = flask.Flask('thrimshim')
def cors(app): def cors(app):
@ -42,7 +44,6 @@ def metrics():
@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.""" """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) return update_row(ident, row)
@ -63,6 +64,7 @@ def get_row(ident):
assert row.id == ident assert row.id == ident
response = row._asdict() response = row._asdict()
response['id'] = str(response['id'])
response = { response = {
key: ( key: (
value.isoformat() if isinstance(value, datetime.datetime) value.isoformat() if isinstance(value, datetime.datetime)
@ -141,21 +143,30 @@ def update_row(ident, new_row):
@app.route('/thrimshim/manual-link/<uuid:ident>', methods=['POST']) @app.route('/thrimshim/manual-link/<uuid:ident>', methods=['POST'])
def manual_link(ident): def manual_link(ident):
"""Manually set a video_link""" """Manually set a video_link if the state is 'UNEDITED' or 'DONE' and the
upload_location is 'manual'."""
link = flask.request.json link = flask.request.json
conn = app.db_manager.get_conn() conn = app.db_manager.get_conn()
results = database.query(conn, """
SELECT id, state, upload_location
FROM events
WHERE id = %s""", ident)
old_row = results.fetchone()
if old_row is None:
return 'Row {} not found'.format(ident), 404
if old_row.state != 'UNEDITED' and not (old_row.state == 'DONE' and old_row.upload_location == 'manual'):
return 'Invalid state {} for manual video link'.format(old_row.state), 403
results = database.query(conn, """ results = database.query(conn, """
UPDATE events UPDATE events
SET state='DONE', upload_location = 'Manual', video_link = %s SET state='DONE', upload_location = 'manual', video_link = %s
WHERE id = %s""", link, str(ident)) WHERE id = %s AND (state = 'UNEDITED' OR (state = 'DONE' AND
if results.rowcount != 1: upload_location = 'manual'))""", link, ident)
return 'Row id = {} not found'.format(ident), 404
logging.info("Row {} video_link set to {}".format(ident, link)) logging.info("Row {} video_link set to {}".format(ident, link))
return '' return ''
@app.route('/thrimshim/reset/<uuid:ident>', methods=['POST'])
@app.route('/thrimshim/reset/<uuid:ident>')
def reset_row(ident): def reset_row(ident):
"""Clear state and video_link columns and reset state to 'UNEDITED'.""" """Clear state and video_link columns and reset state to 'UNEDITED'."""
conn = app.db_manager.get_conn() conn = app.db_manager.get_conn()
@ -163,7 +174,7 @@ def reset_row(ident):
UPDATE events UPDATE events
SET state='UNEDITED', error = NULL, video_id = NULL, video_link = NULL, SET state='UNEDITED', error = NULL, video_id = NULL, video_link = NULL,
uploader = NULL uploader = NULL
WHERE id = %s""", str(ident)) WHERE id = %s""", ident)
if results.rowcount != 1: if results.rowcount != 1:
return 'Row id = {} not found'.format(ident), 404 return 'Row id = {} not found'.format(ident), 404
logging.info("Row {} reset to 'UNEDITED'".format(ident)) logging.info("Row {} reset to 'UNEDITED'".format(ident))

Loading…
Cancel
Save