Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    def test_get_best_psi_films_queryset_alg2(self):
        """
           Test for query that retrieves top recommendations.
           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()

        # no recommendations yet, the result should be 0
        result = recom_helper.get_best_psi_films_queryset_alg2(self.u4.id)
        self.assertEquals(len(result), 0)

        # simulate generating recommendations (for all films) and test again, it should retrieve all films
        r1 = Recommendation()
        r1.guess_rating_alg2 = 7
        r1.film = self.f19
        r1.user = self.u4
        r1.save()
        r2 = Recommendation()
        r2.guess_rating_alg2 = 8
        r2.film = self.f20
        r2.user = self.u4
        r2.save()
        result = recom_helper.get_best_psi_films_queryset_alg2(self.u4.id)
        self.assertEquals(len(result), 2)

        # rate one film, it should be removed from the list
        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.f20.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.u4)
        else:
            print "RATING FORM INVALID!"

        result = recom_helper.get_recently_popular_films_query(self.u4.id)
        self.assertEquals(len(result), 21)
Beispiel #4
0
def rate_films_fast_forward(request, ajax=None):
    if request.POST:
        if not request.user.is_authenticated():
            if ajax:
                return json_error('LOGIN')
            else:
                return HttpResponseRedirect(
                    full_url('LOGIN') + '?next=%s&reason=vote' % request.path)
        form = rating_helper.handle_rating(RatingForm(request.POST),
                                           request.user)
        valid = form.is_valid()
        print "valid=" + str(valid)
        if ajax:
            print "ajax"
            return json_success() if valid else json_error(
                "Hmm serwer nawalil?")

    films1 = []
    films2 = []
    for i in range(1, 6):
        film = rating_helper.get_next_film(request)
        films1.append(film)
        print "got film: " + film.title

    for j in range(1, 6):
        film = rating_helper.get_next_film(request)
        films2.append(film)
        print "got film: " + film.title

    context = {'films1': films1, 'films2': films2}

    return render_to_response(
        templates['RATE_FILMS_FAST_FORWARD'],
        context,
        context_instance=RequestContext(request),
    )
Beispiel #5
0
    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)
Beispiel #6
0
    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)