Beispiel #1
0
 def get_context_data(self, **kwargs):
     context = super(Home, self).get_context_data(**kwargs)
     member = self.request.user
     recommended_items = []
     if member.is_authenticated():
         for item in get_all_recommended(member, 12):
             if isinstance(item, Movie):
                 item.type = 'movie'
             else:
                 item.type = 'series'
                 size = 0
                 episodes = SeriesEpisode.objects.filter(series=item)
                 for episode in episodes:
                     size += episode.size
                 item.size = size
             recommended_items.append(item)
         if len(recommended_items) < Movie.MIN_RECOMMENDED:
             additional = Movie.MIN_RECOMMENDED - len(recommended_items)
             additional_items = Movie.objects.all().order_by('-release')[:additional]
             recommended_items.append(additional_items)
     context['items'] = recommended_items
     context['recommended_items'] = as_matrix(recommended_items, 4)
     recent_releases = list(Movie.objects.all().order_by('-release', '-id')[:Movie.MAX_RECENT])
     shuffle(recent_releases)
     sample_media = recent_releases[0]
     context['fb_share_item'] = sample_media
     return context
Beispiel #2
0
 def get_context_data(self, **kwargs):
     context = super(Home, self).get_context_data(**kwargs)
     member = self.request.user
     recommended_items = []
     if member.is_authenticated():
         for item in get_all_recommended(member, 12):
             if isinstance(item, Movie):
                 item.type = 'movie'
             else:
                 item.type = 'series'
                 size = 0
                 episodes = SeriesEpisode.objects.filter(series=item)
                 for episode in episodes:
                     size += episode.size
                 item.size = size
             recommended_items.append(item)
         if len(recommended_items) < Movie.MIN_RECOMMENDED:
             additional = Movie.MIN_RECOMMENDED - len(recommended_items)
             additional_items = Movie.objects.all().order_by('-release')[:additional]
             recommended_items.append(additional_items)
     context['items'] = recommended_items
     context['recommended_items'] = as_matrix(recommended_items, 4)
     recent_releases = list(Movie.objects.all().order_by('-release', '-id')[:Movie.MAX_RECENT])
     shuffle(recent_releases)
     sample_media = recent_releases[0]
     context['fb_share_item'] = sample_media
     return context
Beispiel #3
0
 def test_get_all_recommended(self):
     """
     Pulls recommended media based on the ones previously watched. The so recommended items must be put in cache
     :return:
     """
     member = Member.objects.get(pk='56eb6d04b37b3379b531e011')
     category_western = Category.objects.get(pk='56eb6d04b37b3379b531e092')
     for series in Series.objects.all():
         series.categories.append(category_western)
         series.save()
     exclude_list_keys_key = '[email protected]:' + EXCLUDE_LIST_KEYS_KEY
     cache_key_watched = '[email protected]:already_watched'
     cache_key_recommended = '[email protected]:recommended'
     cache.delete(exclude_list_keys_key)
     cache.delete(cache_key_watched)
     cache.delete(cache_key_recommended)
     self.client.login(email='*****@*****.**', password='******')
     # exclude_list_keys = {cache_key_watched}
     # watched_media = [Movie.objects.get(pk='56eb6d04b37b3379b531e085')]
     # cache.set(exclude_list_keys_key, exclude_list_keys)
     # cache.set(cache_key_watched, watched_media)
     other_adult_movie = Movie.objects.get(pk='56eb6d04b37b3379b531e083')
     other_adult_movie.id = None
     other_adult_movie.slug = 'other-adult-movie'
     other_adult_movie.filename = 'other-adult-movie.mp4'
     other_adult_movie.title = 'Other adult movie'
     other_adult_movie.save()
     all_recommended_media = get_all_recommended(member, 6)
     expected_media = [
         Movie.objects.get(pk=pk)
         for pk in ('56eb6d04b37b3379b531e086', '56eb6d04b37b3379b531e085')
     ]
     expected_media.extend([
         Series.objects.get(pk=pk)
         for pk in ('56eb6d04b37b3379b531e074', '56eb6d04b37b3379b531e073')
     ])
     expected_media.append(other_adult_movie)
     cached_recommended = cache.get(cache_key_recommended)
     cached_ex_list_keys = cache.get(exclude_list_keys_key)
     self.assertListEqual(all_recommended_media, expected_media)
     self.assertListEqual(all_recommended_media, cached_recommended)
     for key in cached_ex_list_keys:
         self.assertIn(key, [cache_key_watched, cache_key_recommended])
     cache.delete(exclude_list_keys_key)
     cache.delete(cache_key_watched)
     cache.delete(cache_key_recommended)
Beispiel #4
0
 def test_get_all_recommended(self):
     """
     Pulls recommended media based on the ones previously watched. The so recommended items must be put in cache
     :return:
     """
     member = Member.objects.get(pk='56eb6d04b37b3379b531e011')
     category_western = Category.objects.get(pk='56eb6d04b37b3379b531e092')
     for series in Series.objects.all():
         series.categories.append(category_western)
         series.save()
     exclude_list_keys_key = '[email protected]:' + EXCLUDE_LIST_KEYS_KEY
     cache_key_watched = '[email protected]:already_watched'
     cache_key_recommended = '[email protected]:recommended'
     cache.delete(exclude_list_keys_key)
     cache.delete(cache_key_watched)
     cache.delete(cache_key_recommended)
     self.client.login(email='*****@*****.**', password='******')
     # exclude_list_keys = {cache_key_watched}
     # watched_media = [Movie.objects.get(pk='56eb6d04b37b3379b531e085')]
     # cache.set(exclude_list_keys_key, exclude_list_keys)
     # cache.set(cache_key_watched, watched_media)
     other_adult_movie = Movie.objects.get(pk='56eb6d04b37b3379b531e083')
     other_adult_movie.id = None
     other_adult_movie.slug = 'other-adult-movie'
     other_adult_movie.filename = 'other-adult-movie.mp4'
     other_adult_movie.title = 'Other adult movie'
     other_adult_movie.save()
     all_recommended_media = get_all_recommended(member, 6)
     expected_media = [Movie.objects.get(pk=pk) for pk in ('56eb6d04b37b3379b531e086', '56eb6d04b37b3379b531e085')]
     expected_media.extend([Series.objects.get(pk=pk) for pk in ('56eb6d04b37b3379b531e074', '56eb6d04b37b3379b531e073')])
     expected_media.append(other_adult_movie)
     cached_recommended = cache.get(cache_key_recommended)
     cached_ex_list_keys = cache.get(exclude_list_keys_key)
     self.assertListEqual(all_recommended_media, expected_media)
     self.assertListEqual(all_recommended_media, cached_recommended)
     for key in cached_ex_list_keys:
         self.assertIn(key, [cache_key_watched, cache_key_recommended])
     cache.delete(exclude_list_keys_key)
     cache.delete(cache_key_watched)
     cache.delete(cache_key_recommended)