Skip to content

Commit

Permalink
api: CMD_EpisodeSetStatus now reports the episodes status on failure …
Browse files Browse the repository at this point in the history
…and is now much easier to read and backlog is only started once !
  • Loading branch information
lad1337 committed Jun 13, 2012
1 parent da411c4 commit 148308b
Showing 1 changed file with 30 additions and 46 deletions.
76 changes: 30 additions & 46 deletions sickbeard/webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,15 @@ def run(self):
# get all episode numbers frome self,season
ep_list = showObj.getAllEpisodes(season=self.s)

results = []
def _epResult(result_code, ep, msg=""):
return {'season': ep.season, 'episode': ep.episode, 'status': _get_status_Strings(ep.status), 'result': result_type_map[result_code], 'message': msg}

ep_results = []
failure = False
start_backlog = False
ep_segment = None
for epObj in ep_list:
if self.status == WANTED:
if ep_segment == None and self.status == WANTED:
# figure out what segment the episode is in and remember it so we can backlog it
if showObj.air_by_date:
ep_segment = str(epObj.airdate)[:7]
Expand All @@ -929,57 +935,35 @@ def run(self):
with epObj.lock:
# don't let them mess up UNAIRED episodes
if epObj.status == UNAIRED:
if self.e != None: # setting the status of a unaired is only considert a failure if we directly wanted this episode, but is ignored on a season request
ep_results.append(_epResult(RESULT_FAILURE, epObj, "Refusing to change status because it is UNAIRED"))
failure = True
continue
results.append('unaired')
#return _responds(RESULT_FAILURE, msg="Refusing to change status because it is UNAIRED")

if self.status in Quality.DOWNLOADED and epObj.status not in Quality.SNATCHED + Quality.SNATCHED_PROPER + Quality.DOWNLOADED + [IGNORED] and not ek.ek(os.path.isfile, epObj.location):
# allow the user to force setting the status for an already downloaded episode
if epObj.status in Quality.DOWNLOADED and not self.force:
ep_results.append(_epResult(RESULT_FAILURE, epObj, "Refusing to change status to Wanted because it's already marked as DOWNLOADED"))
failure = True
continue
results.append('notSnatchedDownloaded')
#return _responds(RESULT_FAILURE, msg="Refusing to change status to DOWNLOADED because it's not SNATCHED/DOWNLOADED")

# allow the user to force setting the status for an already downloaded episode
if epObj.status in Quality.DOWNLOADED:
if self.force == True:
epObj.status = self.status
epObj.saveToDB()
else:
results.append('alreadySnatchedDownloaded')
continue
#return _responds(RESULT_FAILURE, msg="Refusing to change status to Wanted because it's already marked as SNATCHED/DOWNLOADED")
else:
epObj.status = self.status
epObj.saveToDB()
epObj.status = self.status
epObj.saveToDB()

if self.status == WANTED:
cur_backlog_queue_item = search_queue.BacklogQueueItem(showObj, ep_segment)
sickbeard.searchQueueScheduler.action.add_item(cur_backlog_queue_item) #@UndefinedVariable
logger.log(u"API :: Starting backlog for " + showObj.name + " season " + str(ep_segment) + " because some eps were set to Wanted")
results.append('successWanted')
continue
#return _responds(RESULT_SUCCESS, msg="Episode status changed to Wanted, and backlog started")
results.append('success')
#return _responds(RESULT_SUCCESS, msg="Episode status successfully changed to " + statusStrings[epObj.status])

if 'successWanted' in results:
# if we have set any episode to wanted we set them to wanted.
# only other option is that others might gave been skiped because they are already downloaded or snatched
return _responds(RESULT_SUCCESS, msg="Episode status changed to Wanted, and backlog started")
elif 'alreadySnatchedDownloaded' in results:
# if we never had a successWanted but alreadySnatchedDownloaded all episodes where snatched to dowloaded already
# we do this before the generic succes because the success for a wanted was already checked with successWanted
return _responds(RESULT_FAILURE, msg="Refusing to change status because it's already marked as SNATCHED/DOWNLOADED")
elif 'success' in results:
# this covers basically all other statuses besides wanted
return _responds(RESULT_SUCCESS, msg="Episode status successfully changed to " + statusStrings[epObj.status])
elif 'unaired' in results:
# no succes because the unaired error
return _responds(RESULT_FAILURE, msg="Refusing to change status because it is UNAIRED")
elif 'notSnatchedDownloaded' in results:
return _responds(RESULT_FAILURE, msg="Refusing to change status to DOWNLOADED because it's not SNATCHED/DOWNLOADED")
start_backlog = True
ep_results.append(_epResult(RESULT_SUCCESS, epObj))

extra_msg = ""
if start_backlog:
cur_backlog_queue_item = search_queue.BacklogQueueItem(showObj, ep_segment)
sickbeard.searchQueueScheduler.action.add_item(cur_backlog_queue_item) #@UndefinedVariable
logger.log(u"API :: Starting backlog for " + showObj.name + " season " + str(ep_segment) + " because some eps were set to Wanted")
extra_msg = " Backlog started"

if failure:
return _responds(RESULT_FAILURE, ep_results, 'Failed to set some or all status. Check data.' + extra_msg)
else:
# if everything else fails ... we fail
return _responds(RESULT_FAILURE, msg='Failed to set status')
return _responds(RESULT_SUCCESS, msg='All status set.' + extra_msg)


class CMD_Exceptions(ApiCall):
Expand Down

0 comments on commit 148308b

Please sign in to comment.