def add_popular_films(comparators): films = list(Film.objects.filter(filmranking__number_of_votes__gte=300).distinct()) popular_films = [] for film in films: popular_films.append( (film.filmranking_set.all().order_by("-number_of_votes").values("number_of_votes")[0], film) ) popular_films.sort(reverse=True) popular_films = popular_films[:POPULAR_FILMS_NUMBER] films = list(Film.objects.filter(filmranking__number_of_votes__gte=MIN_FILM_RATES).distinct()) film_helper = FilmHelper() film_actors = {} film_ratings = {} for film in films: film_ratings[film.id] = {} ratings = list(Rating.objects.filter(type=Rating.TYPE_FILM, rating__isnull=False).values("user", "film", "rating")) for rat in ratings: if rat["film"] in film_ratings: film_ratings[rat["film"]][rat["user"]] = rat["rating"] for film in films: film_actors[film.id] = set([c["person"] for c in film_helper.get_film_actors(film, limit=20).values("person")]) for film2 in popular_films: for film1 in films: comparators.append((film1.id, film2[1].id, count_comparator(film1, film2[1], film_actors, film_ratings)))
def test_get_all_ratings(self): recom_helper = RecomHelper() film_helper = FilmHelper() film_with_director_and_actor = Film.objects.filter( title__startswith="Added film part 2") related_directors = film_helper.get_film_directors( film_with_director_and_actor[0]) related_characters = film_helper.get_film_actors( film_with_director_and_actor[0]) related_actors = [] for character in related_characters: actor = character.person related_actors.append(actor) non_rated_films = Film.objects.filter(rating__isnull=True, title__startswith="Added") POST = {} #POST['form_id'] = '1' POST['rating'] = '6' POST['object_id'] = str(non_rated_films[1].parent.id) POST['form_type'] = str(Rating.TYPE_FILM) POST['cancel_rating'] = '0' rating_form = RatingForm(POST) if rating_form.is_valid(): rating_helper.add_edit_rating(rating_form, self.u1) else: print "RATING FORM INVALID!" all_ratings = recom_helper.get_all_ratings(self.u1.id) self.assertEquals(len(all_ratings), 31) director_ratings = recom_helper.get_all_ratings( self.u1.id, related_director=related_directors) self.assertEquals(len(director_ratings), 1) actor_ratings = recom_helper.get_all_ratings( self.u1.id, related_actor=related_actors) self.assertEquals(len(actor_ratings), 1) from_ratings = recom_helper.get_all_ratings(self.u1.id, year_from=1990) self.assertEquals(len(from_ratings), 23) to_ratings = recom_helper.get_all_ratings(self.u1.id, year_to=2000) self.assertEquals(len(to_ratings), 22) popularity_ratings = recom_helper.get_all_ratings(self.u1.id, popularity=5) self.assertEquals(len(popularity_ratings), 8) tag_ratings = recom_helper.get_all_ratings(self.u1.id, tags='comedy') self.assertEquals(len(tag_ratings), 5)
def test_get_all_ratings(self): recom_helper = RecomHelper() film_helper = FilmHelper() film_with_director_and_actor = Film.objects.filter(title__startswith="Added film part 2") related_directors = film_helper.get_film_directors(film_with_director_and_actor[0]) related_characters = film_helper.get_film_actors(film_with_director_and_actor[0]) related_actors = [] for character in related_characters: actor = character.person related_actors.append(actor) non_rated_films = Film.objects.filter(rating__isnull=True, title__startswith="Added") POST = {} # POST['form_id'] = '1' POST["rating"] = "6" POST["object_id"] = str(non_rated_films[1].parent.id) POST["form_type"] = str(Rating.TYPE_FILM) POST["cancel_rating"] = "0" rating_form = RatingForm(POST) if rating_form.is_valid(): rating_helper.add_edit_rating(rating_form, self.u1) else: print "RATING FORM INVALID!" all_ratings = recom_helper.get_all_ratings(self.u1.id) self.assertEquals(len(all_ratings), 31) director_ratings = recom_helper.get_all_ratings(self.u1.id, related_director=related_directors) self.assertEquals(len(director_ratings), 1) actor_ratings = recom_helper.get_all_ratings(self.u1.id, related_actor=related_actors) self.assertEquals(len(actor_ratings), 1) from_ratings = recom_helper.get_all_ratings(self.u1.id, year_from=1990) self.assertEquals(len(from_ratings), 23) to_ratings = recom_helper.get_all_ratings(self.u1.id, year_to=2000) self.assertEquals(len(to_ratings), 22) popularity_ratings = recom_helper.get_all_ratings(self.u1.id, popularity=5) self.assertEquals(len(popularity_ratings), 8) tag_ratings = recom_helper.get_all_ratings(self.u1.id, tags="comedy") self.assertEquals(len(tag_ratings), 5)
def add_popular_films(comparators): films = list(Film.objects.filter( filmranking__number_of_votes__gte=300).distinct()) popular_films = [] for film in films: popular_films.append((film.filmranking_set.all().order_by( "-number_of_votes").values('number_of_votes')[0], film)) popular_films.sort(reverse=True) popular_films = popular_films[:POPULAR_FILMS_NUMBER] films = list(Film.objects.filter( filmranking__number_of_votes__gte=MIN_FILM_RATES).distinct()) film_helper = FilmHelper() film_actors = {} film_ratings = {} for film in films: film_ratings[film.id] = {} ratings = list(Rating.objects.filter(type=Rating.TYPE_FILM, rating__isnull=False).values('user', 'film', 'rating')) for rat in ratings: if rat['film'] in film_ratings: film_ratings[rat['film']][rat['user']] = rat['rating'] for film in films: film_actors[film.id] = set([c['person'] for c in film_helper.get_film_actors(film, limit=20).values('person')]) for film2 in popular_films: for film1 in films: comparators.append((film1.id, film2[1].id, count_comparator(film1, film2[1], film_actors, film_ratings)))
def test_get_recently_popular_films_query(self): print "Test get recently popular films query" """ Test for query which gets the most popular films for the rating machine. First it should return all films, then we rate one and in the second call it should only retrieve the other film. """ recom_helper = RecomHelper() film_helper = FilmHelper() u1_result = recom_helper.get_recently_popular_films_query(self.u1.id) self.assertEquals(len(u1_result), 2) u2_result = recom_helper.get_recently_popular_films_query(self.u2.id) self.assertEquals(len(u2_result), 12) film_with_director_and_actor = Film.objects.filter(title__startswith="Added film part 1") related_directors = film_helper.get_film_directors(film_with_director_and_actor[0]) related_characters = film_helper.get_film_actors(film_with_director_and_actor[0]) related_actors = [] for character in related_characters: actor = character.person related_actors.append(actor) POST = {} # TODO check why it starts breaking (form invalid) when form_id is set # POST['form_id'] = '1' POST["rating"] = "6" POST["object_id"] = str(self.f12.parent.id) POST["form_type"] = str(Rating.TYPE_FILM) POST["cancel_rating"] = "0" rating_form = RatingForm(POST) if rating_form.is_valid(): rating_helper.add_edit_rating(rating_form, self.u2) else: print "RATING FORM INVALID!" result = recom_helper.get_recently_popular_films_query(self.u2.id) self.assertEquals(len(result), 11) # test filtering result = recom_helper.get_recently_popular_films_query(self.u5.id) self.assertEquals(len(result), 32) result = recom_helper.get_recently_popular_films_query(self.u5.id, year_from="1990") self.assertEquals(len(result), 24) result = recom_helper.get_recently_popular_films_query(self.u5.id, year_to="2000") self.assertEquals(len(result), 22) result = recom_helper.get_recently_popular_films_query(self.u5.id, year_from="1990", year_to="2000") self.assertEquals(len(result), 14) result = recom_helper.get_recently_popular_films_query(self.u5.id, popularity="10") self.assertEquals(len(result), 8) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from="1990", year_to="2000", popularity="10" ) self.assertEquals(len(result), 4) rated_films = [self.f1.id, self.f3.id, self.f5.id, self.f7.id, self.f9.id] result = recom_helper.get_recently_popular_films_query(self.u5.id, rated_films=rated_films) self.assertEquals(len(result), 27) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from="1990", year_to="2000", rated_films=rated_films, popularity="10" ) self.assertEquals(len(result), 2) result = recom_helper.get_recently_popular_films_query(self.u5.id, related_director=related_directors) self.assertEquals(len(result), 1) result = recom_helper.get_recently_popular_films_query(self.u5.id, related_actor=related_actors) self.assertEquals(len(result), 2) result = recom_helper.get_recently_popular_films_query( self.u5.id, related_director=related_directors, related_actor=related_actors ) self.assertEquals(len(result), 1) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from="1990", year_to="2011", related_director=related_directors, related_actor=related_actors, ) self.assertEquals(len(result), 1)
def test_get_recently_popular_films_query(self): print "Test get recently popular films query" """ Test for query which gets the most popular films for the rating machine. First it should return all films, then we rate one and in the second call it should only retrieve the other film. """ recom_helper = RecomHelper() film_helper = FilmHelper() u1_result = recom_helper.get_recently_popular_films_query(self.u1.id) self.assertEquals(len(u1_result), 2) u2_result = recom_helper.get_recently_popular_films_query(self.u2.id) self.assertEquals(len(u2_result), 12) film_with_director_and_actor = Film.objects.filter( title__startswith="Added film part 1") related_directors = film_helper.get_film_directors( film_with_director_and_actor[0]) related_characters = film_helper.get_film_actors( film_with_director_and_actor[0]) related_actors = [] for character in related_characters: actor = character.person related_actors.append(actor) POST = {} # TODO check why it starts breaking (form invalid) when form_id is set #POST['form_id'] = '1' POST['rating'] = '6' POST['object_id'] = str(self.f12.parent.id) POST['form_type'] = str(Rating.TYPE_FILM) POST['cancel_rating'] = '0' rating_form = RatingForm(POST) if rating_form.is_valid(): rating_helper.add_edit_rating(rating_form, self.u2) else: print "RATING FORM INVALID!" result = recom_helper.get_recently_popular_films_query(self.u2.id) self.assertEquals(len(result), 11) # test filtering result = recom_helper.get_recently_popular_films_query(self.u5.id) self.assertEquals(len(result), 32) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from='1990') self.assertEquals(len(result), 24) result = recom_helper.get_recently_popular_films_query(self.u5.id, year_to='2000') self.assertEquals(len(result), 22) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from='1990', year_to='2000') self.assertEquals(len(result), 14) result = recom_helper.get_recently_popular_films_query(self.u5.id, popularity='10') self.assertEquals(len(result), 8) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from='1990', year_to='2000', popularity='10') self.assertEquals(len(result), 4) rated_films = [ self.f1.id, self.f3.id, self.f5.id, self.f7.id, self.f9.id ] result = recom_helper.get_recently_popular_films_query( self.u5.id, rated_films=rated_films) self.assertEquals(len(result), 27) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from='1990', year_to='2000', rated_films=rated_films, popularity='10') self.assertEquals(len(result), 2) result = recom_helper.get_recently_popular_films_query( self.u5.id, related_director=related_directors) self.assertEquals(len(result), 1) result = recom_helper.get_recently_popular_films_query( self.u5.id, related_actor=related_actors) self.assertEquals(len(result), 2) result = recom_helper.get_recently_popular_films_query( self.u5.id, related_director=related_directors, related_actor=related_actors) self.assertEquals(len(result), 1) result = recom_helper.get_recently_popular_films_query( self.u5.id, year_from='1990', year_to='2011', related_director=related_directors, related_actor=related_actors) self.assertEquals(len(result), 1)