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):