Esempio n. 1
0
    def get_context_data(self, **kwargs):
        context = super(MovieList, self).get_context_data(**kwargs)

        # check cache for movies
        movies = cache.get('movies', None)
        sort_by = self.request.GET.get('sort_by', 'recent')

        if not movies:
            try:
                # get data from BBC API
                bbc_movies = get_bbc_data()
            except Exception as e:
                messages.error(
                    self.request,
                    'Error getting data from BBC'
                )
                logger.debug(e)
                return context
            movies = [custom_movie_object(movie) for movie in bbc_movies]
            try:
                # search The Movie DB for each movie
                movies = [get_movie_db_data(movie) for movie in movies]
                # save results to cache for 5 minutes
                cache.set('movies', movies, 300)
            except Exception as e:
                messages.error(
                    self.request,
                    'Error getting data from The Movie DB'
                )
                logger.debug(e)
                return context

        movies = sort_movies(movies, sort_by)
        context['movies'] = movies

        return context
Esempio n. 2
0
 def test_sort_rating_low(self):
     """test sorting by lowest rating"""
     self.assertEqual(sort_movies(self.movies, 'low')[0], self.movie_1)
Esempio n. 3
0
 def test_sort_rating_high(self):
     """test sorting by highest rating"""
     self.assertEqual(sort_movies(self.movies, 'high')[0], self.movie_3)
Esempio n. 4
0
 def test_sort_old(self):
     """test sorting by oldest"""
     self.assertEqual(sort_movies(self.movies, 'old')[0], self.movie_2)
Esempio n. 5
0
 def test_sort_recent(self):
     """test sorting by most recent"""
     self.assertEqual(sort_movies(self.movies, 'recent')[0], self.movie_1)