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.
134 lines
4.3 KiB
Python
134 lines
4.3 KiB
Python
import click
|
|
|
|
from ffx.model.show import Show
|
|
from ffx.show_descriptor import ShowDescriptor
|
|
|
|
|
|
class ShowController():
|
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
self.Session = self.context['database']['session'] # convenience
|
|
|
|
|
|
def getShowDescriptor(self, showId):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Show).filter(Show.id == showId)
|
|
|
|
if q.count():
|
|
show: Show = q.first()
|
|
return show.getDescriptor(self.context)
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShowController.getShowDescriptor(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
def getShow(self, showId):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Show).filter(Show.id == showId)
|
|
|
|
return q.first() if q.count() else None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShowController.getShow(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
def getAllShows(self):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Show)
|
|
|
|
if q.count():
|
|
return q.all()
|
|
else:
|
|
return []
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShowController.getAllShows(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def updateShow(self, showDescriptor: ShowDescriptor):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Show).filter(Show.id == showDescriptor.getId())
|
|
|
|
if not q.count():
|
|
show = Show(id = int(showDescriptor.getId()),
|
|
name = str(showDescriptor.getName()),
|
|
year = int(showDescriptor.getYear()),
|
|
index_season_digits = showDescriptor.getIndexSeasonDigits(),
|
|
index_episode_digits = showDescriptor.getIndexEpisodeDigits(),
|
|
indicator_season_digits = showDescriptor.getIndicatorSeasonDigits(),
|
|
indicator_episode_digits = showDescriptor.getIndicatorEpisodeDigits())
|
|
|
|
s.add(show)
|
|
s.commit()
|
|
return True
|
|
else:
|
|
|
|
currentShow = q.first()
|
|
|
|
changed = False
|
|
if currentShow.name != str(showDescriptor.getName()):
|
|
currentShow.name = str(showDescriptor.getName())
|
|
changed = True
|
|
if currentShow.year != int(showDescriptor.getYear()):
|
|
currentShow.year = int(showDescriptor.getYear())
|
|
changed = True
|
|
|
|
if currentShow.index_season_digits != int(showDescriptor.getIndexSeasonDigits()):
|
|
currentShow.index_season_digits = int(showDescriptor.getIndexSeasonDigits())
|
|
changed = True
|
|
if currentShow.index_episode_digits != int(showDescriptor.getIndexEpisodeDigits()):
|
|
currentShow.index_episode_digits = int(showDescriptor.getIndexEpisodeDigits())
|
|
changed = True
|
|
if currentShow.indicator_season_digits != int(showDescriptor.getIndicatorSeasonDigits()):
|
|
currentShow.indicator_season_digits = int(showDescriptor.getIndicatorSeasonDigits())
|
|
changed = True
|
|
if currentShow.indicator_episode_digits != int(showDescriptor.getIndicatorEpisodeDigits()):
|
|
currentShow.indicator_episode_digits = int(showDescriptor.getIndicatorEpisodeDigits())
|
|
changed = True
|
|
|
|
if changed:
|
|
s.commit()
|
|
return changed
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShowController.updateShow(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def deleteShow(self, show_id):
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Show).filter(Show.id == int(show_id))
|
|
|
|
|
|
if q.count():
|
|
|
|
#DAFUQ: https://stackoverflow.com/a/19245058
|
|
# q.delete()
|
|
show = q.first()
|
|
s.delete(show)
|
|
|
|
s.commit()
|
|
return True
|
|
return False
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"ShowController.deleteShow(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|