a few small fixes to ekim's comments

pull/46/head
Christopher Usher 6 years ago
parent 1d09e28b1e
commit d23de10b3e

@ -5,10 +5,11 @@ import signal
import uuid import uuid
import argh import argh
import flask
import gevent import gevent
import gevent.backdoor import gevent.backdoor
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
import flask import prometheus_client
from psycopg2 import sql from psycopg2 import sql
import common import common
@ -33,6 +34,12 @@ def cors(app):
return handle return handle
@app.route('/metrics')
def metrics():
"""Expose Prometheus metrics."""
return prometheus_client.generate_latest()
@app.route('/thrimshim/<ident>', methods=['GET', 'POST']) @app.route('/thrimshim/<ident>', methods=['GET', 'POST'])
def thrimshim(ident): def thrimshim(ident):
"""Comunicate between Thrimbletrimmer and the Wubloader database.""" """Comunicate between Thrimbletrimmer and the Wubloader database."""
@ -100,11 +107,10 @@ def update_row(ident, new_row):
conn = app.db_manager.get_conn() conn = app.db_manager.get_conn()
#check a row with id = ident is in the database #check a row with id = ident is in the database
with database.transaction(conn):
results = database.query(conn, """ results = database.query(conn, """
SELECT id, state SELECT id, state
FROM events FROM events
WHERE id = %s;""", ident) WHERE id = %s""", ident)
old_row = results.fetchone() old_row = results.fetchone()
if old_row is None: if old_row is None:
return 'Row {} not found'.format(ident), 404 return 'Row {} not found'.format(ident), 404
@ -144,19 +150,9 @@ def update_row(ident, new_row):
sql.Identifier(column), sql.Placeholder(column), sql.Identifier(column), sql.Placeholder(column),
) for column in new_row.keys() ) for column in new_row.keys()
)) ))
with database.transaction(conn):
result = database.query(conn, build_query, id=ident, **new_row) result = database.query(conn, build_query, id=ident, **new_row)
if result.rowcount != 1: if result.rowcount != 1:
if result.rowcount == 0.: return 'Video likely already published', 403
with database.transaction(conn):
check_result = database.query(conn, """
SELECT id, state
FROM events
WHERE id = %s;""", ident)
current_row = check_result.fetchone()
if current_row.state not in ['UNEDITED', 'EDITED', 'CLAIMED']:
return 'Video already published', 403
raise Exception('Database consistancy error for id = {}'.format(ident))
logging.info('Row {} updated to state {}'.format(ident, new_row['state'])) logging.info('Row {} updated to state {}'.format(ident, new_row['state']))
return '' return ''

Loading…
Cancel
Save