def save(self, show_id, session=None): ''' :param show_id: int :returns: boolean ''' episode = models.Episode( show_id=show_id, number=self.number, title=self.title, air_date=self.air_date, description_text=self.description.text if self.description else None, description_title=self.description.title if self.description else None, description_url=self.description.url if self.description else None, season=self.season, episode=self.episode, runtime=self.runtime, ) session.merge(episode) self.to_elasticsearch(show_id)
def patch_episodes(self, session, show_id, episodes_dict): ''' :param show_id: int :param episodes_dict: dict :returns boolean ''' episodes = [] numbers = [episode['number'] for episode in episodes_dict] ep_g = {episode['number']: episode for episode in episodes_dict} episodes = session.query(models.Episode).filter( models.Episode.show_id == show_id, models.Episode.number.in_(numbers), ).all() for episode in episodes: numbers.remove(episode.number) self.flatten_request(ep_g[episode.number], 'description', 'description') self.update_model( model_ins=episode, new_data=ep_g[episode.number], overwrite=False, ) for number in numbers: episode = models.Episode( show_id=show_id, number=number, ) session.add(episode) self.flatten_request(ep_g[episode.number], 'description', 'description') self.update_model( model_ins=episode, new_data=ep_g[episode.number], overwrite=False, )
def test(self): self.login() with new_session() as session: show = models.Show( title='Test show', runtime=30, total_episodes=3, ) session.add(show) session.flush() show2 = models.Show( title='Test show 2', runtime=30, total_episodes=1, ) session.add(show2) session.flush() episode1 = models.Episode(show_id=show.id, number=1) session.add(episode1) episode2 = models.Episode(show_id=show.id, number=2) session.add(episode2) episode3 = models.Episode(show_id=show.id, number=3, runtime=40) session.add(episode3) episode4 = models.Episode(show_id=show2.id, number=4) session.add(episode4) session.commit() show = show.serialize() show2 = show2.serialize() episode1 = episode1.serialize() episode2 = episode2.serialize() episode3 = episode3.serialize() episode4 = episode4.serialize() response = self.get('/1/users/{}/show-stats'.format( self.current_user.id)) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['fan_of'], 0) self.assertEqual(data['episodes_watched'], 0) self.assertEqual(data['episodes_watched_minutes'], 0) self.assertEqual(data['shows_finished'], 0) # Follow self.put('/1/users/{}/shows-following/{}'.format( self.current_user.id, show['id'], )) response = self.get('/1/users/{}/show-stats'.format( self.current_user.id)) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['fan_of'], 1) # watched time response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 1)) self.assertEqual(response.code, 200) response = self.get('/1/users/{}/show-stats'.format( self.current_user.id)) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 1, data) self.assertEqual(data['episodes_watched_minutes'], 30, data) self.assertEqual(data['shows_finished'], 0) response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 1)) self.assertEqual(response.code, 200) response = self.get('/1/users/{}/show-stats'.format( self.current_user.id)) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 2, data) self.assertEqual(data['episodes_watched_minutes'], 60, data) response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 2)) self.assertEqual(response.code, 200) response = self.get('/1/users/{}/show-stats'.format( self.current_user.id)) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 3, data) self.assertEqual(data['episodes_watched_minutes'], 90, data) self.assertEqual(data['shows_finished'], 0) response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 3)) self.assertEqual(response.code, 200) response = self.get('/1/users/{}/show-stats'.format( self.current_user.id)) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 4, data) self.assertEqual(data['episodes_watched_minutes'], 130, data) self.assertEqual(data['shows_watched'], 1, data) self.assertEqual(data['shows_finished'], 1) response = self.put('/1/shows/{}/episodes/{}/watched'.format( show2['id'], 4)) self.assertEqual(response.code, 200) response = self.get('/1/users/{}/show-stats'.format( self.current_user.id)) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 5, data) self.assertEqual(data['episodes_watched_minutes'], 160, data) self.assertEqual(data['shows_watched'], 2, data) self.assertEqual(data['shows_finished'], 2, data)
def test(self): self.login() with new_session() as session: show = models.Show( title='Test show', runtime=30, ) session.add(show) session.flush() episode1 = models.Episode(show_id=show.id, number=1) session.add(episode1) episode2 = models.Episode(show_id=show.id, number=2) session.add(episode2) episode3 = models.Episode(show_id=show.id, number=3, runtime=40) session.add(episode3) session.commit() show = show.serialize() episode1 = episode1.serialize() episode2 = episode2.serialize() episode3 = episode3.serialize() response = self.get('/1/shows/{}/user-stats'.format(show['id'])) self.assertEqual(response.code, 200, response.body) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 0) self.assertEqual(data['episodes_watched_minutes'], 0) # watched time response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 1)) self.assertEqual(response.code, 200) response = self.get('/1/shows/{}/user-stats'.format(show['id'])) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 1, data) self.assertEqual(data['episodes_watched_minutes'], 30, data) response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 1)) self.assertEqual(response.code, 200) response = self.get('/1/shows/{}/user-stats'.format(show['id'])) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 2, data) self.assertEqual(data['episodes_watched_minutes'], 60, data) response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 2)) self.assertEqual(response.code, 200) response = self.get('/1/shows/{}/user-stats'.format(show['id'])) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 3, data) self.assertEqual(data['episodes_watched_minutes'], 90, data) response = self.put('/1/shows/{}/episodes/{}/watched'.format( show['id'], 3)) self.assertEqual(response.code, 200) response = self.get('/1/shows/{}/user-stats'.format(show['id'])) self.assertEqual(response.code, 200) data = utils.json_loads(response.body) self.assertEqual(data['episodes_watched'], 4, data) self.assertEqual(data['episodes_watched_minutes'], 130, data)
def test(self): self.login(constants.LEVEL_PROGRESS) # Create a show and 2 episodes with new_session() as session: show = models.Show() session.add(show) session.flush() episode1 = models.Episode(show_id=show.id, number=1) session.add(episode1) episode2 = models.Episode(show_id=show.id, number=2) session.add(episode2) session.commit() show_id = show.id # Test no watched episodes response = self.get( '/1/shows/{}/episodes/last-watched'.format(show_id)) self.assertEqual(response.code, 204, response.body) self.assertEqual(response.body, b'') # set episode 1 as watching response = self.put( '/1/shows/{}/episodes/{}/position'.format(show_id, 1), {'position': 200}) self.assertEqual(response.code, 204) # Since we have not completed the first episode # and it's the only episode we have watched the result # should be empty response = self.get( '/1/shows/{}/episodes/last-watched'.format(show_id)) self.assertEqual(response.code, 204, response.body) self.assertEqual(response.body, b'') # Start watching episode 2. # Episode 1 should now be the latest watched even though it # is not completed. response = self.put( '/1/shows/{}/episodes/{}/position'.format(show_id, 2), {'position': 202}) self.assertEqual(response.code, 204) response = self.get( '/1/shows/{}/episodes/last-watched'.format(show_id)) self.assertEqual(response.code, 200, response.body) data = utils.json_loads(response.body) self.assertEqual(data['number'], 1) self.assertEqual(data['user_watched']['position'], 200) self.assertEqual(data['user_watched']['times'], 0) # Set episode 2 as completed. # Episode 2 should now be the last watched response = self.put('/1/shows/{}/episodes/{}/watched'.format( show_id, 2)) self.assertEqual(response.code, 200) response = self.get( '/1/shows/{}/episodes/last-watched'.format(show_id)) self.assertEqual(response.code, 200, response.body) data = utils.json_loads(response.body) self.assertEqual(data['number'], 2) self.assertEqual(data['user_watched']['position'], 0) self.assertEqual(data['user_watched']['times'], 1) # set episode 1 as watched response = self.put('/1/shows/{}/episodes/{}/watched'.format( show_id, 1)) # unwatch episode 2 response = self.put( '/1/shows/{}/episodes/{}/watched'.format(show_id, 2), { 'times': -1, }) self.assertEqual(response.code, 204) # episode 1 should now be the last watched response = self.get( '/1/shows/{}/episodes/last-watched'.format(show_id)) self.assertEqual(response.code, 200, response.body) data = utils.json_loads(response.body) self.assertEqual(data['number'], 1) # watch episode 2 twice response = self.put( '/1/shows/{}/episodes/{}/watched'.format(show_id, 2), { 'times': 2, }) self.assertEqual(response.code, 200) response = self.get( '/1/shows/{}/episodes/last-watched'.format(show_id)) self.assertEqual(response.code, 200, response.body) data = utils.json_loads(response.body) self.assertEqual(data['number'], 2) self.assertEqual(data['user_watched']['position'], 0)