From c0235901c640e9705e79cfe39289c950d56f3d7e Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 26 Oct 2021 12:23:45 -0700 Subject: [PATCH] Separate try-except for the get_video loop and mark loop. This will help prevent inconsistent state where some videos get marked, then one raises, and the db rolls back but the other objects still have their state attribute set. --- .../backend/endpoints/video_endpoints.py | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/frontends/ycdl_flask/backend/endpoints/video_endpoints.py b/frontends/ycdl_flask/backend/endpoints/video_endpoints.py index 225ac67..3eb5675 100644 --- a/frontends/ycdl_flask/backend/endpoints/video_endpoints.py +++ b/frontends/ycdl_flask/backend/endpoints/video_endpoints.py @@ -1,6 +1,7 @@ import flask; from flask import request from voussoirkit import flasktools +from voussoirkit import stringtools import ycdl @@ -12,18 +13,18 @@ site = common.site @site.route('/mark_video_state', methods=['POST']) def post_mark_video_state(): video_ids = request.form['video_ids'] + video_ids = stringtools.comma_space_split(video_ids) state = request.form['state'] - try: - video_ids = video_ids.split(',') - for video_id in video_ids: - video = common.ycdldb.get_video(video_id) - video.mark_state(state, commit=False) - common.ycdldb.commit() + try: + videos = [common.ycdldb.get_video(id) for id in video_ids] except ycdl.exceptions.NoSuchVideo as exc: - common.ycdldb.rollback() return flasktools.json_response(exc.jsonify(), status=404) + try: + for video in videos: + video.mark_state(state, commit=False) + common.ycdldb.commit() except ycdl.exceptions.InvalidVideoState as exc: common.ycdldb.rollback() return flasktools.json_response(exc.jsonify(), status=400) @@ -34,14 +35,15 @@ def post_mark_video_state(): @site.route('/start_download', methods=['POST']) def post_start_download(): video_ids = request.form['video_ids'] - try: - video_ids = video_ids.split(',') - for video_id in video_ids: - common.ycdldb.download_video(video_id, commit=False) - common.ycdldb.commit() + video_ids = stringtools.comma_space_split(video_ids) - except ycdl.ytapi.VideoNotFound: - common.ycdldb.rollback() - flask.abort(404) + try: + videos = [common.ycdldb.get_video(id) for id in video_ids] + except ycdl.exceptions.NoSuchVideo as exc: + return flasktools.json_response(exc.jsonify(), status=404) + + for video in videos: + common.ycdldb.download_video(video, commit=False) + common.ycdldb.commit() return flasktools.json_response({'video_ids': video_ids, 'state': 'downloaded'})