import click from ffx.model.show import Show class ShowController(): def __init__(self, context): self.context = context self.Session = self.context['database']['session'] # convenience def getShowDesciptor(self, showId): try: s = self.Session() q = s.query(Show).filter(Show.id == showId) if q.count(): show = q.first() return show.getDesciptor() except Exception as ex: raise click.ClickException(f"ShowController.getShowDesciptor(): {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): try: s = self.Session() q = s.query(Show).filter(Show.id == showDescriptor['id']) if not q.count(): show = Show(id = int(showDescriptor['id']), name = str(showDescriptor['name']), year = int(showDescriptor['year']), index_season_digits = showDescriptor['index_season_digits'], index_episode_digits = showDescriptor['index_episode_digits'], indicator_season_digits = showDescriptor['indicator_season_digits'], indicator_episode_digits = showDescriptor['indicator_episode_digits']) s.add(show) s.commit() return True else: currentShow = q.first() changed = False if currentShow.name != str(showDescriptor['name']): currentShow.name = str(showDescriptor['name']) changed = True if currentShow.year != int(showDescriptor['year']): currentShow.year = int(showDescriptor['year']) changed = True if currentShow.index_season_digits != int(showDescriptor['index_season_digits']): currentShow.index_season_digits = int(showDescriptor['index_season_digits']) changed = True if currentShow.index_episode_digits != int(showDescriptor['index_episode_digits']): currentShow.index_episode_digits = int(showDescriptor['index_episode_digits']) changed = True if currentShow.indicator_season_digits != int(showDescriptor['indicator_season_digits']): currentShow.indicator_season_digits = int(showDescriptor['indicator_season_digits']) changed = True if currentShow.indicator_episode_digits != int(showDescriptor['indicator_episode_digits']): currentShow.indicator_episode_digits = int(showDescriptor['indicator_episode_digits']) 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()