def get_details(movie_id): """ Get movie details. Will retrieve from the db if it has been previously queried, otherwise will get from the TMDB web service. """ # Check if movie already exists in the database matching_movies = Movie.objects.filter(m_id=movie_id) if len(matching_movies) > 0: movie = matching_movies[0] logger.info("Found movie %s in db.", movie_id) else: # If movie does not exist in the database, retrieve details from TMDB tmdb_item = tmdb.get_details_from_tmdb(movie_id) movie = Movie.convert_to_movie(tmdb_item) movie.save() # get it from the DB again, since the format of dates is different # in the API JSON compared to the DB movie = Movie.objects.get(m_id=movie_id) logger.info("Retrieved movie #%s from tmdb.", movie_id) # Populate calculated fields avg = models.Avg("rating") ratings = Rating.objects.exclude(rating=-1).filter(movie=movie) movie.avg_rating = ratings.aggregate(avg)["rating__avg"] return movie
def get_details(movie_id): """ Get movie details. Will retrieve from the db if it has been previously queried, otherwise will get from the TMDB web service. """ # Check if movie already exists in the database matching_movies = Movie.objects.filter(m_id=movie_id) if len(matching_movies) > 0: movie = matching_movies[0] logger.info('Found movie %s in db.', movie_id) else: # If movie does not exist in the database, retrieve details from TMDB tmdb_item = tmdb.get_details_from_tmdb(movie_id) movie = Movie.convert_to_movie(tmdb_item) movie.save() # get it from the DB again, since the format of dates is different # in the API JSON compared to the DB movie = Movie.objects.get(m_id=movie_id) logger.info('Retrieved movie #%s from tmdb.', movie_id) # Populate calculated fields avg = models.Avg('rating') ratings = Rating.objects.exclude(rating=-1).filter(movie=movie) movie.avg_rating = ratings.aggregate(avg)['rating__avg'] return movie
def test_get_movie_details(self): ret = tmdb.get_details_from_tmdb(513) with patch.object(tmdb, "get_details_from_tmdb", return_value=ret) as m_method: Movie.get_details(513) # this should get it from TMDB api m_method.assert_called_with(513) m_method.reset_mock() Movie.get_details(513) # this should get it from the SQL database self.assertFalse(m_method.called)
def test_get_single_movie(self): movie = tmdb.get_details_from_tmdb(513) self.assertTrue(movie['original_title'] == 'Fire')