From 736f0e0fe488b0baf2df564788a189e90953bc09 Mon Sep 17 00:00:00 2001 From: MasterGunner Date: Fri, 28 Jun 2019 19:00:46 -0300 Subject: [PATCH 1/3] Adding get_all_row and auth function stubs --- thrimshim/thrimshim/main.py | 44 +++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/thrimshim/thrimshim/main.py b/thrimshim/thrimshim/main.py index 85fe116..2c9965c 100644 --- a/thrimshim/thrimshim/main.py +++ b/thrimshim/thrimshim/main.py @@ -48,8 +48,10 @@ def thrimshim(ident): row = flask.request.json return update_row(ident, row) else: - return get_row(ident) - + if ident: + return get_row(ident) + else: + return get_all_rows() def get_row(ident): """Gets the row from the database with id == ident.""" @@ -73,11 +75,40 @@ def get_row(ident): } return json.dumps(response) +def get_all_rows(): + """Gets all rows from the events table from the database""" + conn = app.db_manager.get_conn() + results = database.query(conn, """ + SELECT * + FROM events""") + + rows = [] + row = results.fetchone() + while row: + row = row._asdict() + row['id'] = str(row['id']) + row = { + key: ( + value.isoformat() if isinstance(value, datetime.datetime) + else value + ) for key, value in row.items() + } + rows.append(row) + row = results.fetchone() + if not rows: + return 'No rows found', 404 + + response = rows + 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 not authenticate_user(new_row['Auth_Token']): + return 'User not authenticated.', 403 + state_columns = ['state', 'uploader', 'error', 'video_link'] #these have to be set before a video can be set as 'EDITED' non_null_columns = ['upload_location', 'video_start', 'video_end', @@ -94,6 +125,13 @@ def update_row(ident, new_row): for extra in extras: del new_row[extra] + #validate title length - YouTube titles are limited to 100 characters, 9 reserved for "DBXXXX - ". + if(len(new_row['video_title']) > 91): + return 'Title must be 91 characters or less', 400 + #validate start time is less than end time + if(new_row['video_start'] > new_row['video_end']): + return 'Video Start must be less than Video End.', 400 + conn = app.db_manager.get_conn() #check a row with id = ident is in the database results = database.query(conn, """ @@ -140,6 +178,8 @@ def update_row(ident, new_row): logging.info('Row {} updated to state {}'.format(ident, new_row['state'])) return '' +def authenticate_user(authToken): + return True @app.route('/thrimshim/manual-link/', methods=['POST']) def manual_link(ident): From 6a171130e836b374d241da6a6d80fa95869c77c0 Mon Sep 17 00:00:00 2001 From: MasterGunner Date: Fri, 28 Jun 2019 22:07:44 -0300 Subject: [PATCH 2/3] Updated Get All Rows route. --- thrimshim/thrimshim/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/thrimshim/thrimshim/main.py b/thrimshim/thrimshim/main.py index 2c9965c..71df644 100644 --- a/thrimshim/thrimshim/main.py +++ b/thrimshim/thrimshim/main.py @@ -40,6 +40,9 @@ def metrics(): """Expose Prometheus metrics.""" return prometheus_client.generate_latest() +@app.route('/thrimshim') +def thrimshim_all_rows(): + return get_all_rows() @app.route('/thrimshim/', methods=['GET', 'POST']) def thrimshim(ident): @@ -48,10 +51,7 @@ def thrimshim(ident): row = flask.request.json return update_row(ident, row) else: - if ident: - return get_row(ident) - else: - return get_all_rows() + return get_row(ident) def get_row(ident): """Gets the row from the database with id == ident.""" From 2e953edddede4e89c81614adc2f987e879279598 Mon Sep 17 00:00:00 2001 From: MasterGunner Date: Fri, 28 Jun 2019 23:31:49 -0300 Subject: [PATCH 3/3] Cleanup from Ekim's comments, removed auth placeholder until I know what I'm doing. --- thrimshim/thrimshim/main.py | 61 +++++++++++++------------------------ 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/thrimshim/thrimshim/main.py b/thrimshim/thrimshim/main.py index 71df644..26b8c31 100644 --- a/thrimshim/thrimshim/main.py +++ b/thrimshim/thrimshim/main.py @@ -41,8 +41,24 @@ def metrics(): return prometheus_client.generate_latest() @app.route('/thrimshim') -def thrimshim_all_rows(): - return get_all_rows() +def get_all_rows(): + """Gets all rows from the events table from the database""" + conn = app.db_manager.get_conn() + results = database.query(conn, """ + SELECT * + FROM events""") + rows = [] + for row in results: + row = row._asdict() + row['id'] = str(row['id']) + row = { + key: ( + value.isoformat() if isinstance(value, datetime.datetime) + else value + ) for key, value in row.items() + } + rows.append(row) + return json.dumps(rows) @app.route('/thrimshim/', methods=['GET', 'POST']) def thrimshim(ident): @@ -75,40 +91,10 @@ def get_row(ident): } return json.dumps(response) -def get_all_rows(): - """Gets all rows from the events table from the database""" - conn = app.db_manager.get_conn() - results = database.query(conn, """ - SELECT * - FROM events""") - - rows = [] - row = results.fetchone() - while row: - row = row._asdict() - row['id'] = str(row['id']) - row = { - key: ( - value.isoformat() if isinstance(value, datetime.datetime) - else value - ) for key, value in row.items() - } - rows.append(row) - row = results.fetchone() - if not rows: - return 'No rows found', 404 - - response = rows - 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 not authenticate_user(new_row['Auth_Token']): - return 'User not authenticated.', 403 - state_columns = ['state', 'uploader', 'error', 'video_link'] #these have to be set before a video can be set as 'EDITED' non_null_columns = ['upload_location', 'video_start', 'video_end', @@ -125,11 +111,11 @@ def update_row(ident, new_row): for extra in extras: del new_row[extra] - #validate title length - YouTube titles are limited to 100 characters, 9 reserved for "DBXXXX - ". - if(len(new_row['video_title']) > 91): - return 'Title must be 91 characters or less', 400 + #validate title length - YouTube titles are limited to 100 characters. + if len(new_row['video_title']) > 100: + return 'Title must be 100 characters or less', 400 #validate start time is less than end time - if(new_row['video_start'] > new_row['video_end']): + if new_row['video_start'] > new_row['video_end']: return 'Video Start must be less than Video End.', 400 conn = app.db_manager.get_conn() @@ -178,9 +164,6 @@ def update_row(ident, new_row): logging.info('Row {} updated to state {}'.format(ident, new_row['state'])) return '' -def authenticate_user(authToken): - return True - @app.route('/thrimshim/manual-link/', methods=['POST']) def manual_link(ident): """Manually set a video_link if the state is 'UNEDITED' or 'DONE' and the