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.
This commit is contained in:
parent
413f9af568
commit
c0235901c6
1 changed files with 17 additions and 15 deletions
|
@ -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']
|
||||
video_ids = stringtools.comma_space_split(video_ids)
|
||||
|
||||
try:
|
||||
video_ids = video_ids.split(',')
|
||||
for video_id in video_ids:
|
||||
common.ycdldb.download_video(video_id, commit=False)
|
||||
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()
|
||||
|
||||
except ycdl.ytapi.VideoNotFound:
|
||||
common.ycdldb.rollback()
|
||||
flask.abort(404)
|
||||
|
||||
return flasktools.json_response({'video_ids': video_ids, 'state': 'downloaded'})
|
||||
|
|
Loading…
Reference in a new issue