Ejemplo n.º 1
0
class ShowtimesTestCase(TestCase):
    fixtures = ['test_users.json']

    def setUp(self):
        Film.objects.filter(imdb_code__lt=1000).delete()

    def init_films(self):

        self.f1 = Film(type=1, permalink='przypadek', imdb_code=111, status=1, version=1,
            release_year=1999, title='Przypadek', popularity=20, popularity_month=10)
        self.f1.save()
        self.fc1 = FilmOnChannel(title='fc1', film=self.f1)
        self.fc1.save()
        self.fr1 = FilmRanking(film=self.f1, type=Rating.TYPE_FILM,
                average_score=Decimal('1.0'), number_of_votes=100)
        self.fr1.save()

        self.f2 = Film(type=1, permalink='wrestler', imdb_code=112, status=1, version=1,
            release_year=2008, title='Wrestler', popularity=10, popularity_month=1)
        self.f2.save()
        self.fc2 = FilmOnChannel(title='fc2', film=self.f2)
        self.fc2.save()
        self.fr2 = FilmRanking(film=self.f2, type=Rating.TYPE_FILM,
                average_score=Decimal('2.0'), number_of_votes=100)
        self.fr2.save()

        self.f3 = Film(type=1, permalink='american-history-x', imdb_code=113, status=1, version=1,
            release_year=1998, title='American History X', popularity=1, popularity_month=1)
        self.f3.save()
        self.fc3 = FilmOnChannel(title='fc3', film=self.f3)
        self.fc3.save()
        self.fr3 = FilmRanking(film=self.f3, type=Rating.TYPE_FILM,
                average_score=Decimal('3.0'), number_of_votes=100)
        self.fr3.save()

        self.f4 = Film(type=1, permalink='the-big-lebowski', imdb_code=114, status=1, version=1,
            release_year=1998, title='The Big Lebowski', popularity=1, popularity_month=1)
        self.f4.save()
        self.fc4 = FilmOnChannel(title='fc4', film=self.f4)
        self.fc4.save()
        self.fr4 = FilmRanking(film=self.f4, type=Rating.TYPE_FILM,
                average_score=Decimal('4.0'), number_of_votes=100)
        self.fr4.save()

        self.f5 = Film(type=1, permalink='the-lord-of-the-rings-the-fellowship-of-the-ring', imdb_code=115, status=1, version=1,
            release_year=2001, title='The Lord of the Rings: The Fellowship of the Ring', popularity=1, popularity_month=1)
        self.f5.save()
        self.fc5 = FilmOnChannel(title='fc5', film=self.f5)
        self.fc5.save()
        self.fr5 = FilmRanking(film=self.f5, type=Rating.TYPE_FILM,
                average_score=Decimal('5.0'), number_of_votes=100)
        self.fr5.save()

        self.f6 = Film(type=1, permalink='raiders-of-the-lost-ark', imdb_code=116, status=1, version=1,
            release_year=1981, title='Raiders of the Lost Ark', popularity=1, popularity_month=1)
        self.f6.save()
        self.fc6 = FilmOnChannel(title='fc6', film=self.f6)
        self.fc6.save()
        self.fr6 = FilmRanking(film=self.f6, type=Rating.TYPE_FILM,
                average_score=Decimal('6.0'), number_of_votes=100)
        self.fr6.save()

        self.f7 = Film(type=1, permalink='the-alien', imdb_code=117, status=1, version=1,
            release_year=1979, title='The Alien', popularity=1, popularity_month=1)
        self.f7.save()
        self.fc7 = FilmOnChannel(title='fc7', film=self.f7)
        self.fc7.save()
        self.fr7 = FilmRanking(film=self.f7, type=Rating.TYPE_FILM,
                average_score=Decimal('7.0'), number_of_votes=100)
        self.fr7.save()

        self.f8 = Film(type=1, permalink='terminator-2-judgment-day', imdb_code=118, status=1, version=1,
        release_year=1991, title='Terminator 2: Judgment Day', popularity=1, popularity_month=1)
        self.f8.save()
        self.fc8 = FilmOnChannel(title='fc8', film=self.f8)
        self.fc8.save()
        self.fr8 = FilmRanking(film=self.f8, type=Rating.TYPE_FILM,
                average_score=Decimal('8.0'), number_of_votes=100)
        self.fr8.save()
        
        self.f9 = Film(type=1, permalink='pulp-fiction', imdb_code=119, status=1, version=1,
        release_year=1991, title='Pulp Fiction', popularity=1, popularity_month=1)
        self.f9.save()
        
        self.f9.save_tags('pulp')

    def initialize(self):
    
        Film.objects.all().delete()

        self.poland = Country.objects.get(code='PL')

        self.init_films()
        self.usa = Country(name='USA', code='US')
        self.usa.save()

        self.new_york = Town(country=self.usa, name='New York',
                has_cinemas=True)
        self.new_york.save()

        self.cinema_ny = Channel(type=TYPE_CINEMA, town=self.new_york, address='Some Street 3')
        self.cinema_ny.save()

        self.hbo = Channel(type=TYPE_TV_CHANNEL, country=self.usa)
        self.hbo.save()

        self.tvp = Channel(type=TYPE_TV_CHANNEL, country=self.poland)
        self.tvp.save()

        time_now = datetime.datetime.utcnow() + datetime.timedelta(0.1)

        self.screen_ny1 = Screening(channel=self.cinema_ny, film=self.fc1,
                utc_time=time_now)
        self.screen_ny1.save()

        self.screen_ny2 = Screening(channel=self.cinema_ny, film=self.fc2,
                utc_time=time_now)
        self.screen_ny2.save()

        self.screen_ny3 = Screening(channel=self.cinema_ny, film=self.fc3,
                utc_time=time_now)
        self.screen_ny3.save()

        self.screen_ny4 = Screening(channel=self.cinema_ny, film=self.fc4,
                utc_time=time_now)
        self.screen_ny4.save()

        self.screen_ny5 = Screening(channel=self.cinema_ny, film=self.fc5,
                utc_time=time_now)
        self.screen_ny5.save()

        self.screen_hbo1 = Screening(channel=self.hbo, film=self.fc1,
                utc_time=time_now)
        self.screen_hbo1.save()

        self.screen_hbo2 = Screening(channel=self.hbo, film=self.fc2,
                utc_time=time_now)
        self.screen_hbo2.save()

        self.screen_hbo3 = Screening(channel=self.hbo, film=self.fc3,
                utc_time=time_now)
        self.screen_hbo3.save()

        self.screen_hbo4 = Screening(channel=self.hbo, film=self.fc4,
                utc_time=time_now)
        self.screen_hbo4.save()

        self.screen_hbo5 = Screening(channel=self.hbo, film=self.fc5,
                utc_time=time_now + datetime.timedelta(8))
        self.screen_hbo5.save()

    def test_get_films_in_cinemas1(self):
        """ Tests situation when there are no showtimes in the given country.
            Takes USA as a default then.
        """

        self.initialize()

        country_films = set([f.id for f in \
                get_films_in_cinemas_by_country(country_code='CR')])
        self.assertTrue(self.f4.id in country_films)

    def test_get_films_in_cinemas2(self):
        """ Tests situation when there are showtimes in the given country.
        """

        self.initialize()
        time_now = datetime.datetime.utcnow() + datetime.timedelta(0.1)

        self.warsaw = Town(country=self.poland, name='Warsaw',
                has_cinemas=True)
        self.warsaw.save()

        self.cinema_ww = Channel(type=TYPE_CINEMA, town=self.warsaw, address='Marszalkowska 2')
        self.cinema_ww.save()

        self.screen_ww1 = Screening(channel=self.cinema_ww, film=self.fc5,
                utc_time=time_now)
        self.screen_ww1.save()

        self.screen_ww2 = Screening(channel=self.cinema_ww, film=self.fc7,
                utc_time=time_now)
        self.screen_ww2.save()

        country_films = set([f.id for f in \
                get_films_in_cinemas_by_country(country_code='PL')])
        self.assertTrue(self.f7.id in country_films)

    def test_get_films_in_tv1(self):
        """
            Tests situation when there are no showtimes in the given country
        """

        self.initialize()

        country_films = set([f.id for f in \
                get_films_in_tv_by_country(country_code='CR')])
        self.assertTrue(self.f1.id in country_films)
        self.assertTrue(self.f4.id in country_films)
        self.assertTrue(self.f5.id not in country_films)

    def test_get_films_in_tv2(self):
        """
            Tests situation when there are showtimes in the given country
        """

        self.initialize()
        time_now = datetime.datetime.utcnow() + datetime.timedelta(0.1)

        self.screen_tvp1 = Screening(channel=self.tvp, film=self.fc1,
                utc_time=time_now)
        self.screen_tvp1.save()

        self.screen_tvp2 = Screening(channel=self.tvp, film=self.fc2,
                utc_time=time_now)
        self.screen_tvp2.save()

        self.screen_tvp3 = Screening(channel=self.tvp, film=self.fc3,
                utc_time=time_now)
        self.screen_tvp3.save()

        self.screen_tvp4 = Screening(channel=self.tvp, film=self.fc4,
                utc_time=time_now)
        self.screen_tvp4.save()

        self.screen_tvp5 = Screening(channel=self.tvp, film=self.fc5,
                utc_time=datetime.datetime.utcnow() + datetime.timedelta(8))
        self.screen_tvp5.save()

        country_films = set([f.id for f in \
                get_films_in_tv_by_country(country_code='PL')])

        self.assertTrue(self.f1.id in country_films)
        self.assertTrue(self.f4.id in country_films)
        self.assertTrue(self.f5.id not in country_films)
    
    def test_double_checkins(self):
        self.initialize()
        
        alice = User.objects.get(username='******')

        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice, 2)
        self.screen_hbo1.check_in(alice)

        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)

        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice, 2)
        self.screen_hbo1.film.film.check_in(alice)
        
        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)
        
    def test_double_checkins2(self):
        self.initialize()
        
        alice = User.objects.get(username='******')

        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice, 2)
        self.screen_hbo1.film.film.check_in(alice)
        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)
        
        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice, 2)
        self.screen_hbo1.check_in(alice)

        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)

    def test_activity_message_filter( self ):
        from django.template import Context, Template
        from django.utils.translation import gettext as _
        
        template = Template( '{% load showtimes %}{{ d1|to_checkin_activity_message:d2 }}' )

        now = datetime.datetime.now()

        context = Context( { 'd1': now, 'd2': now } )
        self.assertEqual( template.render( context ), _( 'is watching' ) )

        context = Context( { 'd1': now - datetime.timedelta( hours=1 ), 'd2': now } )
        self.assertEqual( template.render( context ), _( 'is watching' ) )

        context = Context( { 'd1': now + datetime.timedelta( hours=1 ), 'd2': now } )
        self.assertEqual( template.render( context ), _( 'is planing to watch' ) )

        context = Context( { 'd1': now - datetime.timedelta( hours=3 ), 'd2': now } )
        self.assertEqual( template.render( context ), _( 'watched' ) )

    def test_matching(self):
        self.initialize()
        
        foc = FilmOnChannel.objects.match({'title':'pulp fiction'})
        self.assertFalse(foc.film)
        
        foc = FilmOnChannel.objects.match({'title':'pulp,  fiction', 'tag':'pulp'})
        self.assertTrue(foc.film)
Ejemplo n.º 2
0
class FilmHelperTestCase(TestCase):
    def initialize(self):
        self.clean_data()

        tags = "sciencie-fiction comedy"

        # set up test user
        self.user1 = User.objects.create_user("user1", "*****@*****.**",
                                              "secret")
        self.user1.save()

        # set up films
        self.film1 = Film()
        self.film1.title = "Battlefield Earth II"
        self.film1.type = Object.TYPE_FILM
        self.film1.permalink = "battlefirld-earth-ii"
        self.film1.release_year = 2010
        self.film1.production_country_list = "USA"
        self.film1.save()
        self.film1.save_tags(tags, LANG="pl", saved_by=2)

        self.film2 = Film()
        self.film2.title = "Battlefield Earth III"
        self.film2.type = Object.TYPE_FILM
        self.film2.permalink = "battlefirld-earth-iii"
        self.film2.release_year = 2011
        self.film2.production_country_list = "USA"
        self.film2.save()
        self.film2.save_tags(tags, LANG="pl", saved_by=2)

        self.film3 = Film()
        self.film3.title = "Battlefield Earth IV"
        self.film3.type = Object.TYPE_FILM
        self.film3.permalink = "battlefirld-earth-iv"
        self.film3.release_year = 2012
        self.film3.production_country_list = "Italy"
        self.film3.save()
        self.film3.save_tags(tags, LANG="pl", saved_by=2)

        self.film4 = Film()
        self.film4.title = "Battlefield Earth V"
        self.film4.type = Object.TYPE_FILM
        self.film4.permalink = "battlefirld-earth-v"
        self.film4.release_year = 2013
        self.film4.production_country_list = "UK"
        self.film4.save()
        self.film4.save_tags(tags, LANG="pl", saved_by=2)

        # set up filmrankings
        self.filmranking1 = FilmRanking()
        self.filmranking1.film = self.film1
        self.filmranking1.type = Rating.TYPE_FILM
        self.filmranking1.average_score = Decimal('8.0')
        self.filmranking1.number_of_votes = 80
        self.filmranking1.save()

        self.filmranking2 = FilmRanking()
        self.filmranking2.film = self.film2
        self.filmranking2.type = Rating.TYPE_FILM
        self.filmranking2.average_score = Decimal('7.0')
        self.filmranking2.number_of_votes = 70
        self.filmranking2.save()

        self.filmranking3 = FilmRanking()
        self.filmranking3.film = self.film3
        self.filmranking3.type = Rating.TYPE_FILM
        self.filmranking3.average_score = Decimal('6.0')
        self.filmranking3.number_of_votes = 60
        self.filmranking3.save()

        self.filmranking4 = FilmRanking()
        self.filmranking4.film = self.film4
        self.filmranking4.type = Rating.TYPE_FILM
        self.filmranking4.average_score = Decimal('5.0')
        self.filmranking4.number_of_votes = 2
        self.filmranking4.save()

        # save compared objects in database
        self.compared_film1 = FilmComparator()
        self.compared_film1.main_film = self.film1
        self.compared_film1.compared_film = self.film1
        self.compared_film1.score = 10
        self.compared_film1.save()

        self.compared_film2 = FilmComparator()
        self.compared_film2.main_film = self.film1
        self.compared_film2.compared_film = self.film2
        self.compared_film2.score = 9
        self.compared_film2.save()

        self.compared_film3 = FilmComparator()
        self.compared_film3.main_film = self.film1
        self.compared_film3.compared_film = self.film3
        self.compared_film3.score = 8
        self.compared_film3.save()

        self.compared_film4 = FilmComparator()
        self.compared_film4.main_film = self.film1
        self.compared_film4.compared_film = self.film4
        self.compared_film4.score = 7
        self.compared_film4.save()

        # set up test user ratings
        self.user1rating1 = Rating()
        self.user1rating1.type = Rating.TYPE_FILM
        self.user1rating1.user = self.user1
        self.user1rating1.parent = self.film1
        self.user1rating1.rating = 10
        self.user1rating1.save()

        self.user1rating2 = Rating()
        self.user1rating2.type = Rating.TYPE_FILM
        self.user1rating2.user = self.user1
        self.user1rating2.parent = self.film2
        self.user1rating2.rating = 9
        self.user1rating2.save()

        self.user1rating3 = Rating()
        self.user1rating3.type = Rating.TYPE_FILM
        self.user1rating3.user = self.user1
        self.user1rating3.parent = self.film3
        self.user1rating3.rating = 8
        self.user1rating3.save()

        self.user1rating4 = Rating()
        self.user1rating4.type = Rating.TYPE_FILM
        self.user1rating4.user = self.user1
        self.user1rating4.parent = self.film4
        self.user1rating4.rating = 7
        self.user1rating4.save()

    def clean_data(self):
        Film.objects.all().delete()
        FilmComparator.objects.all().delete()
        FilmRanking.objects.all().delete()

    def test_get_users_best_films(self):
        """
            Test get_users_best_films method
        """

        self.initialize()
        helper = FilmHelper()
        best_films = helper.get_users_best_films(self.user1, 3)
        self.assertEquals(len(best_films), 3)
        self.assertEquals(self.film4 in best_films, False)
        self.assertEquals(self.film1 in best_films, True)

    def test_get_related_localized_objects(self):
        """
            Test get_related_localized_objects() method 
        """

        self.initialize()
        helper = FilmHelper()
        related = helper.get_related_localized_objects(self.film1, 3)
        self.assertEquals(len(related), 3)

    def test_get_popular_films(self):
        """
            Test get_popular_films method
        """

        self.initialize()
        helper = FilmHelper()

        films_english = set(helper.get_popular_films(exclude_nonEnglish=True))
        self.assertEquals(self.film1 in films_english, True)
        self.assertEquals(self.film3 in films_english, False)
        self.assertEquals(self.film4 in films_english, False)

        films_english = set(helper.get_popular_films())
        self.assertEquals(self.film1 in films_english, True)
        self.assertEquals(self.film3 in films_english, True)
Ejemplo n.º 3
0
class FilmHelperTestCase(TestCase):

    def initialize(self):
        self.clean_data()

        tags = "sciencie-fiction comedy"

        # set up test user
        self.user1 = User.objects.create_user("user1", "*****@*****.**", "secret")
        self.user1.save()


        # set up films
        self.film1 = Film()
        self.film1.title = "Battlefield Earth II"
        self.film1.type = Object.TYPE_FILM
        self.film1.permalink = "battlefirld-earth-ii"
        self.film1.release_year = 2010
        self.film1.production_country_list = "USA"
        self.film1.save()
        self.film1.save_tags(tags, LANG="pl", saved_by=2)

        self.film2 = Film()
        self.film2.title = "Battlefield Earth III"
        self.film2.type = Object.TYPE_FILM
        self.film2.permalink = "battlefirld-earth-iii"
        self.film2.release_year = 2011
        self.film2.production_country_list = "USA"
        self.film2.save()
        self.film2.save_tags(tags, LANG="pl", saved_by=2)

        self.film3 = Film()
        self.film3.title = "Battlefield Earth IV"
        self.film3.type = Object.TYPE_FILM
        self.film3.permalink = "battlefirld-earth-iv"
        self.film3.release_year = 2012
        self.film3.production_country_list = "Italy"
        self.film3.save()
        self.film3.save_tags(tags, LANG="pl", saved_by=2)

        self.film4 = Film()
        self.film4.title = "Battlefield Earth V"
        self.film4.type = Object.TYPE_FILM
        self.film4.permalink = "battlefirld-earth-v"
        self.film4.release_year = 2013
        self.film4.production_country_list = "UK"
        self.film4.save()
        self.film4.save_tags(tags, LANG="pl", saved_by=2)

        # set up filmrankings
        self.filmranking1 = FilmRanking()
        self.filmranking1.film = self.film1
        self.filmranking1.type = Rating.TYPE_FILM
        self.filmranking1.average_score = Decimal('8.0')
        self.filmranking1.number_of_votes = 80
        self.filmranking1.save()
        
        self.filmranking2 = FilmRanking()
        self.filmranking2.film = self.film2
        self.filmranking2.type = Rating.TYPE_FILM
        self.filmranking2.average_score = Decimal('7.0')
        self.filmranking2.number_of_votes = 70
        self.filmranking2.save()

        self.filmranking3 = FilmRanking()
        self.filmranking3.film = self.film3
        self.filmranking3.type = Rating.TYPE_FILM
        self.filmranking3.average_score = Decimal('6.0')
        self.filmranking3.number_of_votes = 60
        self.filmranking3.save()

        self.filmranking4 = FilmRanking()
        self.filmranking4.film = self.film4
        self.filmranking4.type = Rating.TYPE_FILM
        self.filmranking4.average_score = Decimal('5.0')
        self.filmranking4.number_of_votes = 2
        self.filmranking4.save()

        # save compared objects in database
        self.compared_film1 = FilmComparator()
        self.compared_film1.main_film = self.film1
        self.compared_film1.compared_film = self.film1
        self.compared_film1.score = 10
        self.compared_film1.save()

        self.compared_film2 = FilmComparator()
        self.compared_film2.main_film = self.film1
        self.compared_film2.compared_film = self.film2
        self.compared_film2.score = 9
        self.compared_film2.save()

        self.compared_film3 = FilmComparator()
        self.compared_film3.main_film = self.film1
        self.compared_film3.compared_film = self.film3
        self.compared_film3.score = 8
        self.compared_film3.save()

        self.compared_film4 = FilmComparator()
        self.compared_film4.main_film = self.film1
        self.compared_film4.compared_film = self.film4
        self.compared_film4.score = 7
        self.compared_film4.save()

        # set up test user ratings
        self.user1rating1 = Rating()
        self.user1rating1.type = Rating.TYPE_FILM
        self.user1rating1.user = self.user1
        self.user1rating1.film = self.film1
        self.user1rating1.parent = self.film1
        self.user1rating1.rating = 10
        self.user1rating1.save()

        self.user1rating2 = Rating()
        self.user1rating2.type = Rating.TYPE_FILM
        self.user1rating2.user = self.user1
        self.user1rating2.film = self.film2
        self.user1rating2.parent = self.film2
        self.user1rating2.rating = 9
        self.user1rating2.save()

        self.user1rating3 = Rating()
        self.user1rating3.type = Rating.TYPE_FILM
        self.user1rating3.user = self.user1
        self.user1rating3.film = self.film3
        self.user1rating3.parent = self.film3
        self.user1rating3.rating = 8
        self.user1rating3.save()

        self.user1rating4 = Rating()
        self.user1rating4.type = Rating.TYPE_FILM
        self.user1rating4.user = self.user1
        self.user1rating4.film = self.film4
        self.user1rating4.parent = self.film4
        self.user1rating4.rating = 7
        self.user1rating4.save()

    def clean_data(self):
        Film.objects.all().delete()
        FilmComparator.objects.all().delete()
        FilmRanking.objects.all().delete()

    def test_get_users_best_films(self):
        """
            Test get_users_best_films method
        """

        self.initialize()
        helper = FilmHelper()
        best_films = helper.get_users_best_films(self.user1, 3)
        self.assertEquals(len(best_films), 3)
        self.assertEquals(self.film4 in best_films, False)
        self.assertEquals(self.film1 in best_films, True)

    def test_get_related_localized_objects(self):
        """
            Test get_related_localized_objects() method 
        """

        self.initialize()
        helper = FilmHelper()
        related = helper.get_related_localized_objects(self.film1, 3)
        self.assertEquals(len(related), 3)

    def test_get_popular_films(self):
        """
            Test get_popular_films method
        """

        self.initialize()
        helper = FilmHelper()

        films_english = set(helper.get_popular_films(exclude_nonEnglish=True))
        self.assertEquals(self.film1 in films_english, True)
        self.assertEquals(self.film3 in films_english, False)
        self.assertEquals(self.film4 in films_english, False)

        films_english = set(helper.get_popular_films())
        self.assertEquals(self.film1 in films_english, True)
        self.assertEquals(self.film3 in films_english, True)
Ejemplo n.º 4
0
class ShowtimesTestCase(TestCase):
    fixtures = ['test_users.json']

    def setUp(self):
        Film.objects.filter(imdb_code__lt=1000).delete()

    def init_films(self):

        self.f1 = Film(type=1,
                       permalink='przypadek',
                       imdb_code=111,
                       status=1,
                       version=1,
                       release_year=1999,
                       title='Przypadek',
                       popularity=20,
                       popularity_month=10)
        self.f1.save()
        self.fc1 = FilmOnChannel(title='fc1', film=self.f1)
        self.fc1.save()
        self.fr1 = FilmRanking(film=self.f1,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('1.0'),
                               number_of_votes=100)
        self.fr1.save()

        self.f2 = Film(type=1,
                       permalink='wrestler',
                       imdb_code=112,
                       status=1,
                       version=1,
                       release_year=2008,
                       title='Wrestler',
                       popularity=10,
                       popularity_month=1)
        self.f2.save()
        self.fc2 = FilmOnChannel(title='fc2', film=self.f2)
        self.fc2.save()
        self.fr2 = FilmRanking(film=self.f2,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('2.0'),
                               number_of_votes=100)
        self.fr2.save()

        self.f3 = Film(type=1,
                       permalink='american-history-x',
                       imdb_code=113,
                       status=1,
                       version=1,
                       release_year=1998,
                       title='American History X',
                       popularity=1,
                       popularity_month=1)
        self.f3.save()
        self.fc3 = FilmOnChannel(title='fc3', film=self.f3)
        self.fc3.save()
        self.fr3 = FilmRanking(film=self.f3,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('3.0'),
                               number_of_votes=100)
        self.fr3.save()

        self.f4 = Film(type=1,
                       permalink='the-big-lebowski',
                       imdb_code=114,
                       status=1,
                       version=1,
                       release_year=1998,
                       title='The Big Lebowski',
                       popularity=1,
                       popularity_month=1)
        self.f4.save()
        self.fc4 = FilmOnChannel(title='fc4', film=self.f4)
        self.fc4.save()
        self.fr4 = FilmRanking(film=self.f4,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('4.0'),
                               number_of_votes=100)
        self.fr4.save()

        self.f5 = Film(
            type=1,
            permalink='the-lord-of-the-rings-the-fellowship-of-the-ring',
            imdb_code=115,
            status=1,
            version=1,
            release_year=2001,
            title='The Lord of the Rings: The Fellowship of the Ring',
            popularity=1,
            popularity_month=1)
        self.f5.save()
        self.fc5 = FilmOnChannel(title='fc5', film=self.f5)
        self.fc5.save()
        self.fr5 = FilmRanking(film=self.f5,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('5.0'),
                               number_of_votes=100)
        self.fr5.save()

        self.f6 = Film(type=1,
                       permalink='raiders-of-the-lost-ark',
                       imdb_code=116,
                       status=1,
                       version=1,
                       release_year=1981,
                       title='Raiders of the Lost Ark',
                       popularity=1,
                       popularity_month=1)
        self.f6.save()
        self.fc6 = FilmOnChannel(title='fc6', film=self.f6)
        self.fc6.save()
        self.fr6 = FilmRanking(film=self.f6,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('6.0'),
                               number_of_votes=100)
        self.fr6.save()

        self.f7 = Film(type=1,
                       permalink='the-alien',
                       imdb_code=117,
                       status=1,
                       version=1,
                       release_year=1979,
                       title='The Alien',
                       popularity=1,
                       popularity_month=1)
        self.f7.save()
        self.fc7 = FilmOnChannel(title='fc7', film=self.f7)
        self.fc7.save()
        self.fr7 = FilmRanking(film=self.f7,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('7.0'),
                               number_of_votes=100)
        self.fr7.save()

        self.f8 = Film(type=1,
                       permalink='terminator-2-judgment-day',
                       imdb_code=118,
                       status=1,
                       version=1,
                       release_year=1991,
                       title='Terminator 2: Judgment Day',
                       popularity=1,
                       popularity_month=1)
        self.f8.save()
        self.fc8 = FilmOnChannel(title='fc8', film=self.f8)
        self.fc8.save()
        self.fr8 = FilmRanking(film=self.f8,
                               type=Rating.TYPE_FILM,
                               average_score=Decimal('8.0'),
                               number_of_votes=100)
        self.fr8.save()

        self.f9 = Film(type=1,
                       permalink='pulp-fiction',
                       imdb_code=119,
                       status=1,
                       version=1,
                       release_year=1991,
                       title='Pulp Fiction',
                       popularity=1,
                       popularity_month=1)
        self.f9.save()

        self.f9.save_tags('pulp')

    def initialize(self):

        Film.objects.all().delete()

        self.poland = Country.objects.get(code='PL')

        self.init_films()
        self.usa = Country(name='USA', code='US')
        self.usa.save()

        self.new_york = Town(country=self.usa,
                             name='New York',
                             has_cinemas=True)
        self.new_york.save()

        self.cinema_ny = Channel(type=TYPE_CINEMA,
                                 town=self.new_york,
                                 address='Some Street 3')
        self.cinema_ny.save()

        self.hbo = Channel(type=TYPE_TV_CHANNEL, country=self.usa)
        self.hbo.save()

        self.tvp = Channel(type=TYPE_TV_CHANNEL, country=self.poland)
        self.tvp.save()

        time_now = datetime.datetime.utcnow() + datetime.timedelta(0.1)

        self.screen_ny1 = Screening(channel=self.cinema_ny,
                                    film=self.fc1,
                                    utc_time=time_now)
        self.screen_ny1.save()

        self.screen_ny2 = Screening(channel=self.cinema_ny,
                                    film=self.fc2,
                                    utc_time=time_now)
        self.screen_ny2.save()

        self.screen_ny3 = Screening(channel=self.cinema_ny,
                                    film=self.fc3,
                                    utc_time=time_now)
        self.screen_ny3.save()

        self.screen_ny4 = Screening(channel=self.cinema_ny,
                                    film=self.fc4,
                                    utc_time=time_now)
        self.screen_ny4.save()

        self.screen_ny5 = Screening(channel=self.cinema_ny,
                                    film=self.fc5,
                                    utc_time=time_now)
        self.screen_ny5.save()

        self.screen_hbo1 = Screening(channel=self.hbo,
                                     film=self.fc1,
                                     utc_time=time_now)
        self.screen_hbo1.save()

        self.screen_hbo2 = Screening(channel=self.hbo,
                                     film=self.fc2,
                                     utc_time=time_now)
        self.screen_hbo2.save()

        self.screen_hbo3 = Screening(channel=self.hbo,
                                     film=self.fc3,
                                     utc_time=time_now)
        self.screen_hbo3.save()

        self.screen_hbo4 = Screening(channel=self.hbo,
                                     film=self.fc4,
                                     utc_time=time_now)
        self.screen_hbo4.save()

        self.screen_hbo5 = Screening(channel=self.hbo,
                                     film=self.fc5,
                                     utc_time=time_now + datetime.timedelta(8))
        self.screen_hbo5.save()

    def test_get_films_in_cinemas1(self):
        """ Tests situation when there are no showtimes in the given country.
            Takes USA as a default then.
        """

        self.initialize()

        country_films = set([f.id for f in \
                get_films_in_cinemas_by_country(country_code='CR')])
        self.assertTrue(self.f4.id in country_films)

    def test_get_films_in_cinemas2(self):
        """ Tests situation when there are showtimes in the given country.
        """

        self.initialize()
        time_now = datetime.datetime.utcnow() + datetime.timedelta(0.1)

        self.warsaw = Town(country=self.poland,
                           name='Warsaw',
                           has_cinemas=True)
        self.warsaw.save()

        self.cinema_ww = Channel(type=TYPE_CINEMA,
                                 town=self.warsaw,
                                 address='Marszalkowska 2')
        self.cinema_ww.save()

        self.screen_ww1 = Screening(channel=self.cinema_ww,
                                    film=self.fc5,
                                    utc_time=time_now)
        self.screen_ww1.save()

        self.screen_ww2 = Screening(channel=self.cinema_ww,
                                    film=self.fc7,
                                    utc_time=time_now)
        self.screen_ww2.save()

        country_films = set([f.id for f in \
                get_films_in_cinemas_by_country(country_code='PL')])
        self.assertTrue(self.f7.id in country_films)

    def test_get_films_in_tv1(self):
        """
            Tests situation when there are no showtimes in the given country
        """

        self.initialize()

        country_films = set([f.id for f in \
                get_films_in_tv_by_country(country_code='CR')])
        self.assertTrue(self.f1.id in country_films)
        self.assertTrue(self.f4.id in country_films)
        self.assertTrue(self.f5.id not in country_films)

    def test_get_films_in_tv2(self):
        """
            Tests situation when there are showtimes in the given country
        """

        self.initialize()
        time_now = datetime.datetime.utcnow() + datetime.timedelta(0.1)

        self.screen_tvp1 = Screening(channel=self.tvp,
                                     film=self.fc1,
                                     utc_time=time_now)
        self.screen_tvp1.save()

        self.screen_tvp2 = Screening(channel=self.tvp,
                                     film=self.fc2,
                                     utc_time=time_now)
        self.screen_tvp2.save()

        self.screen_tvp3 = Screening(channel=self.tvp,
                                     film=self.fc3,
                                     utc_time=time_now)
        self.screen_tvp3.save()

        self.screen_tvp4 = Screening(channel=self.tvp,
                                     film=self.fc4,
                                     utc_time=time_now)
        self.screen_tvp4.save()

        self.screen_tvp5 = Screening(channel=self.tvp,
                                     film=self.fc5,
                                     utc_time=datetime.datetime.utcnow() +
                                     datetime.timedelta(8))
        self.screen_tvp5.save()

        country_films = set([f.id for f in \
                get_films_in_tv_by_country(country_code='PL')])

        self.assertTrue(self.f1.id in country_films)
        self.assertTrue(self.f4.id in country_films)
        self.assertTrue(self.f5.id not in country_films)

    def test_double_checkins(self):
        self.initialize()

        alice = User.objects.get(username='******')

        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice, 2)
        self.screen_hbo1.check_in(alice)

        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)

        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice, 2)
        self.screen_hbo1.film.film.check_in(alice)

        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)

    def test_double_checkins2(self):
        self.initialize()

        alice = User.objects.get(username='******')

        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice)
        self.screen_hbo1.film.film.check_in(alice, 2)
        self.screen_hbo1.film.film.check_in(alice)
        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)

        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice)
        self.screen_hbo1.check_in(alice, 2)
        self.screen_hbo1.check_in(alice)

        self.assertEquals(UserActivity.objects.filter(user=alice).count(), 1)

    def test_activity_message_filter(self):
        from django.template import Context, Template
        from django.utils.translation import gettext as _

        template = Template(
            '{% load showtimes %}{{ d1|to_checkin_activity_message:d2 }}')

        now = datetime.datetime.now()

        context = Context({'d1': now, 'd2': now})
        self.assertEqual(template.render(context), _('is watching'))

        context = Context({'d1': now - datetime.timedelta(hours=1), 'd2': now})
        self.assertEqual(template.render(context), _('is watching'))

        context = Context({'d1': now + datetime.timedelta(hours=1), 'd2': now})
        self.assertEqual(template.render(context), _('is planing to watch'))

        context = Context({'d1': now - datetime.timedelta(hours=3), 'd2': now})
        self.assertEqual(template.render(context), _('watched'))

    def test_matching(self):
        self.initialize()

        foc = FilmOnChannel.objects.match({'title': 'pulp fiction'})
        self.assertFalse(foc.film)

        foc = FilmOnChannel.objects.match({
            'title': 'pulp,  fiction',
            'tag': 'pulp'
        })
        self.assertTrue(foc.film)
Ejemplo n.º 5
0
def do_update_films_popularity():
    """
        Goes through all movies ever rated and checks their ratings, 
        updating the popularity and popularity_month columns in core_film
        as well as computing average ratings and counts for each type
        in core_filmranking 
    """
    
    today = date.today()
    last_month = today - timedelta(days=30)
    yesterday = today - timedelta(days=1)
    
    debug("Film monthly popularity to be computed for time between " + unicode(last_month) + " and " + unicode(today))
        
    q_only_rated = (
            Q(rating__last_rated__gt=yesterday) &
            Q(rating__rating__isnull=False)
        )    
    all_films = Film.objects.filter(q_only_rated)    
    all_films = all_films.distinct()
    all_count = all_films.count()
    
    debug("Popularity to be computed for " + unicode(all_count) + " films")
    
    for film in all_films:
        
        # compute overall popularity
        qset = (
            Q(film=film) &
            Q(type=Rating.TYPE_FILM) &
            Q(rating__isnull=False)
        )
        popularity = Rating.objects.filter(qset).count()

        # compute monthlypopularity
        qset_month = (
            Q(film=film) &
            Q(type=Rating.TYPE_FILM) &
            Q(last_rated__gt=last_month) &
            Q(rating__isnull=False)
        )
        popularity_month = Rating.objects.filter(qset_month).count()
                
        # compute average ratings for each type
        for type_obj in Rating.ALL_RATING_TYPES:            
            type = type_obj[0]                       
            overall_film_rating = get_average_rating_for_film(film, type)      
            avg = overall_film_rating.avg
            cnt = overall_film_rating.count
                       
            try:
                qset_ranking = (
                    Q(film=film) &
                    Q(type=type)                
                )
                ranking = FilmRanking.objects.get(qset_ranking)
                ddebug("Ranking for film " + unicode(film.title) + " type " + unicode(type) + " retrieved.")            
            except FilmRanking.DoesNotExist:
                ranking = FilmRanking()
                ranking.film = film
                ranking.type = type
                ddebug("Ranking for film " + unicode(film.title) + " type " + unicode(type) + " created.")
                       
            if (avg!=None):
                if (float(avg)>0):
                    ranking.average_score = avg
                    ranking.number_of_votes = cnt
                    ddebug("Ranking for film " + unicode(film.title) + " type " + str(type) + " saved (" + str(ranking.average_score) + ").")
                    ranking.save()
                else:   
                    ddebug("Ranking for film " + unicode(film.title) + " type " + unicode(type) + " not saved (" + unicode(avg) + ").")
                    return      
            else:
                ddebug("Ranking for film " + unicode(film.title) + " type " + unicode(type) + " not saved (" + unicode(avg) + ").")        
        
        do_update = False
        if not (film.popularity == popularity):
            ddebug(unicode(film) + " popularity set to " + unicode(popularity))
            film.popularity = popularity
            do_update = True
        if not (film.popularity_month == popularity_month):            
            ddebug(unicode(film) + " popularity_month set to " + unicode(popularity_month))               
            film.popularity_month = popularity_month
            do_update = True            
        if do_update:
            film.save()
        
    debug("Film monthly popularity computed for time between " + unicode(last_month) + " and " + unicode(today))
    
    return