You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
234 lines
8.8 KiB
Python
234 lines
8.8 KiB
Python
import click
|
|
|
|
from ffx.model.shifted_season import ShiftedSeason
|
|
|
|
|
|
class EpisodeOrderException(Exception):
|
|
pass
|
|
|
|
class RangeOverlapException(Exception):
|
|
pass
|
|
|
|
|
|
class ShiftedSeasonController():
|
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
self.Session = self.context['database']['session'] # convenience
|
|
|
|
def checkShiftedSeason(self, showId: int, shiftedSeasonObj: dict, shiftedSeasonId: int = 0):
|
|
"""
|
|
Check if for a particula season
|
|
|
|
shiftedSeasonId
|
|
"""
|
|
|
|
try:
|
|
s = self.Session()
|
|
|
|
originalSeason = shiftedSeasonObj['original_season']
|
|
firstEpisode = int(shiftedSeasonObj['first_episode'])
|
|
lastEpisode = int(shiftedSeasonObj['last_episode'])
|
|
|
|
q = s.query(ShiftedSeason).filter(ShiftedSeason.show_id == int(showId))
|
|
if shiftedSeasonId:
|
|
q = q.filter(ShiftedSeason.id != int(shiftedSeasonId))
|
|
|
|
siblingShiftedSeason: ShiftedSeason
|
|
for siblingShiftedSeason in q.all():
|
|
|
|
siblingOriginalSeason = siblingShiftedSeason.getOriginalSeason
|
|
siblingFirstEpisode = siblingShiftedSeason.getFirstEpisode()
|
|
siblingLastEpisode = siblingShiftedSeason.getLastEpisode()
|
|
|
|
if (originalSeason == siblingOriginalSeason
|
|
and lastEpisode >= siblingFirstEpisode
|
|
and siblingLastEpisode >= firstEpisode):
|
|
|
|
return False
|
|
return True
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShiftedSeasonController.addShiftedSeason(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def addShiftedSeason(self, showId: int, shiftedSeasonObj: dict):
|
|
|
|
if type(showId) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.addShiftedSeason(): Argument showId is required to be of type int")
|
|
|
|
if type(shiftedSeasonObj) is not dict:
|
|
raise ValueError(f"ShiftedSeasonController.addShiftedSeason(): Argument shiftedSeasonObj is required to be of type dict")
|
|
|
|
try:
|
|
s = self.Session()
|
|
|
|
firstEpisode = int(shiftedSeasonObj['first_episode'])
|
|
lastEpisode = int(shiftedSeasonObj['last_episode'])
|
|
|
|
if lastEpisode < firstEpisode:
|
|
raise EpisodeOrderException()
|
|
|
|
q = s.query(ShiftedSeason).filter(ShiftedSeason.show_id == int(showId))
|
|
|
|
shiftedSeason = ShiftedSeason(show_id = int(showId),
|
|
original_season = int(shiftedSeasonObj['original_season']),
|
|
first_episode = firstEpisode,
|
|
last_episode = lastEpisode,
|
|
season_offset = int(shiftedSeasonObj['season_offset']),
|
|
episode_offset = int(shiftedSeasonObj['episode_offset']))
|
|
s.add(shiftedSeason)
|
|
s.commit()
|
|
return shiftedSeason.getId()
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShiftedSeasonController.addShiftedSeason(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def updateShiftedSeason(self, shiftedSeasonId: int, shiftedSeasonObj: dict):
|
|
|
|
if type(shiftedSeasonId) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.updateShiftedSeason(): Argument shiftedSeasonId is required to be of type int")
|
|
|
|
if type(shiftedSeasonObj) is not dict:
|
|
raise ValueError(f"ShiftedSeasonController.updateShiftedSeason(): Argument shiftedSeasonObj is required to be of type dict")
|
|
|
|
try:
|
|
s = self.Session()
|
|
|
|
q = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId))
|
|
|
|
if q.count():
|
|
|
|
shiftedSeason = q.first()
|
|
|
|
shiftedSeason.original_season = int(shiftedSeasonObj['original_season'])
|
|
shiftedSeason.first_episode = int(shiftedSeasonObj['first_episode'])
|
|
shiftedSeason.last_episode = int(shiftedSeasonObj['last_episode'])
|
|
shiftedSeason.season_offset = int(shiftedSeasonObj['season_offset'])
|
|
shiftedSeason.episode_offset = int(shiftedSeasonObj['episode_offset'])
|
|
|
|
s.commit()
|
|
return True
|
|
|
|
else:
|
|
return False
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShiftedSeasonController.updateShiftedSeason(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def findShiftedSeason(self, showId: int, originalSeason: int, firstEpisode: int, lastEpisode: int):
|
|
|
|
if type(showId) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.findShiftedSeason(): Argument shiftedSeasonId is required to be of type int")
|
|
|
|
if type(originalSeason) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.findShiftedSeason(): Argument originalSeason is required to be of type int")
|
|
|
|
if type(firstEpisode) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.findShiftedSeason(): Argument firstEpisode is required to be of type int")
|
|
|
|
if type(lastEpisode) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.findShiftedSeason(): Argument lastEpisode is required to be of type int")
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(ShiftedSeason).filter(ShiftedSeason.show_id == int(showId),
|
|
ShiftedSeason.original_season == int(originalSeason),
|
|
ShiftedSeason.first_episode == int(firstEpisode),
|
|
ShiftedSeason.last_episode == int(lastEpisode))
|
|
|
|
return q.first().getId() if q.count() else None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.findShiftedSeason(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
def getShiftedSeasonSiblings(self, showId: int):
|
|
|
|
if type(showId) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.getShiftedSeasonSiblings(): Argument shiftedSeasonId is required to be of type int")
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(ShiftedSeason).filter(ShiftedSeason.show_id == int(showId))
|
|
|
|
return q.all()
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.getShiftedSeasonSiblings(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def getShiftedSeason(self, shiftedSeasonId: int):
|
|
|
|
if type(shiftedSeasonId) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.getShiftedSeason(): Argument shiftedSeasonId is required to be of type int")
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId))
|
|
|
|
return q.first() if q.count() else None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShiftedSeasonController.getShiftedSeason(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def deleteShiftedSeason(self, shiftedSeasonId):
|
|
|
|
if type(shiftedSeasonId) is not int:
|
|
raise ValueError(f"ShiftedSeasonController.deleteShiftedSeason(): Argument shiftedSeasonId is required to be of type int")
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(ShiftedSeason).filter(ShiftedSeason.id == int(shiftedSeasonId))
|
|
|
|
if q.count():
|
|
|
|
#DAFUQ: https://stackoverflow.com/a/19245058
|
|
# q.delete()
|
|
shiftedSeason = q.first()
|
|
s.delete(shiftedSeason)
|
|
|
|
s.commit()
|
|
return True
|
|
return False
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShiftedSeasonController.deleteShiftedSeason(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def shiftSeason(self, showId, season, episode):
|
|
|
|
shiftedSeasonEntry: ShiftedSeason
|
|
for shiftedSeasonEntry in self.getShiftedSeasonSiblings(showId):
|
|
|
|
if (season == shiftedSeasonEntry.getOriginalSeason()
|
|
and (shiftedSeasonEntry.getFirstEpisode() == -1 or episode >= shiftedSeasonEntry.getFirstEpisode())
|
|
and (shiftedSeasonEntry.getLastEpisode() == -1 or episode <= shiftedSeasonEntry.getLastEpisode())):
|
|
|
|
shiftedSeason = season + shiftedSeasonEntry.getSeasonOffset()
|
|
shiftedEpisode = episode + shiftedSeasonEntry.getEpisodeOffset()
|
|
|
|
self.context['logger'].info(f"Shifting season: {season} episode: {episode} "
|
|
+f"-> season: {shiftedSeason} episode: {shiftedEpisode}")
|
|
|
|
return shiftedSeason, shiftedEpisode
|
|
|
|
return season, episode
|