def test_get_watched_categories_with_no_smart_category(self): """ categories of media watched are returned from most recent watched to less recent watched :return: """ member = Member.objects.get(pk='56eb6d04b37b3379b531e011') watched = get_watched(member) watched_categories = get_watched_categories(watched) expected_categories = [Category.objects.get(pk=pk) for pk in ('56eb6d04b37b3379b531e092', '56eb6d04b37b3379b531e091', '56eb6d04b37b3379b531e093')] self.assertListEqual(watched_categories, expected_categories)
def test_get_watched_categories_with_one_smart_category(self): """ smart categories should not be taken into account in get_watched_categories :return: """ category = Category.objects.get(pk='56eb6d04b37b3379b531e091') category.smart = True category.save() member = Member.objects.get(pk='56eb6d04b37b3379b531e011') watched = get_watched(member) watched_categories = [category.slug for category in get_watched_categories(watched)] expected_categories = [Category.objects.get(pk=pk).slug for pk in ('56eb6d04b37b3379b531e092', '56eb6d04b37b3379b531e093')] self.assertListEqual(watched_categories, expected_categories)
def test_get_watched_categories_with_no_smart_category(self): """ categories of media watched are returned from most recent watched to less recent watched :return: """ member = Member.objects.get(pk='56eb6d04b37b3379b531e011') watched = get_watched(member) watched_categories = get_watched_categories(watched) expected_categories = [ Category.objects.get(pk=pk) for pk in ('56eb6d04b37b3379b531e092', '56eb6d04b37b3379b531e091', '56eb6d04b37b3379b531e093') ] self.assertListEqual(watched_categories, expected_categories)
def test_get_watched_categories_with_one_smart_category(self): """ smart categories should not be taken into account in get_watched_categories :return: """ category = Category.objects.get(pk='56eb6d04b37b3379b531e091') category.smart = True category.save() member = Member.objects.get(pk='56eb6d04b37b3379b531e011') watched = get_watched(member) watched_categories = [ category.slug for category in get_watched_categories(watched) ] expected_categories = [ Category.objects.get(pk=pk).slug for pk in ('56eb6d04b37b3379b531e092', '56eb6d04b37b3379b531e093') ] self.assertListEqual(watched_categories, expected_categories)
def get_all_recommended(member, count): """ Gets Media recommended for a Member based on the recently watched media categories :param member: :param count: Total number of media to grab :return: list of Movies and/or Series """ if StreamLogEntry.objects.filter(member=member).count() == 0: recommended = [] else: exclude_list_keys = set() cache_key_recommended = member.email + ':recommended' recommended = cache.get(cache_key_recommended) if recommended: return recommended cache_key_watched = member.email + ':already_watched' already_watched = cache.get(cache_key_watched) if not already_watched: already_watched = get_watched(member) if len(already_watched) > 0: cache.set(cache_key_watched, already_watched) exclude_list_keys.add(cache_key_watched) exclude_list = already_watched categories = get_watched_categories(already_watched) if len(categories) > 0: # Main category is the category of the media most recently watched main_category = categories[0] cnt = count - (len(categories) - 1) media = get_recommended_for_category(main_category, cnt, exclude_list) recommended = media exclude_list.extend(media) categories = categories[1:] for category in categories: # Grab one item for each category other than the main media = get_recommended_for_category(category, 1, exclude_list) recommended.extend(media) exclude_list.extend(media) cache.set(cache_key_recommended, recommended) exclude_list_keys.add(cache_key_recommended) cache.set(member.email + ':' + EXCLUDE_LIST_KEYS_KEY, exclude_list_keys) return recommended
def get_all_recommended(member, count): """ Gets Media recommended for a Member based on the recently watched media categories :param member: :param count: Total number of media to grab :return: list of Movies and/or Series """ if StreamLogEntry.objects.filter(member=member).count() == 0: recommended = [] else: exclude_list_keys = set() cache_key_recommended = member.email + ":recommended" recommended = cache.get(cache_key_recommended) if recommended: return recommended cache_key_watched = member.email + ":already_watched" already_watched = cache.get(cache_key_watched) if not already_watched: already_watched = get_watched(member) if len(already_watched) > 0: cache.set(cache_key_watched, already_watched) exclude_list_keys.add(cache_key_watched) exclude_list = already_watched categories = get_watched_categories(already_watched) if len(categories) > 0: # Main category is the category of the media most recently watched main_category = categories[0] cnt = count - (len(categories) - 1) media = get_recommended_for_category(main_category, cnt, exclude_list) recommended = media exclude_list.extend(media) categories = categories[1:] for category in categories: # Grab one item for each category other than the main media = get_recommended_for_category(category, 1, exclude_list) recommended.extend(media) exclude_list.extend(media) cache.set(cache_key_recommended, recommended) exclude_list_keys.add(cache_key_recommended) cache.set(member.email + ":" + EXCLUDE_LIST_KEYS_KEY, exclude_list_keys) return recommended
def test_get_watched(self): member = Member.objects.get(pk='56eb6d04b37b3379b531e011') watched = get_watched(member) expected = [Movie.objects.get(pk=pk) for pk in ['56eb6d04b37b3379b531e082', '56eb6d04b37b3379b531e081', '56eb6d04b37b3379b531e083']] self.assertListEqual(watched, expected)