Esempio n. 1
0
 def get_context_data(self, **kwargs):
     context = super(ShowFilmView, self).get_context_data(**kwargs)
     if self.request.user.is_authenticated():
         from film20.filmbasket.models import BasketItem
         user = self.request.user
         BasketItem.user_basket(user)
         film = self.get_object()
         try:
             basket = user._basket[film.id]
             owned = basket[1]
         except:
             owned = None
         context['user_film_basket'] = owned
     return context
Esempio n. 2
0
 def get_context_data(self, **kwargs):
     context = super(ShowFilmView, self).get_context_data(**kwargs)
     if self.request.user.is_authenticated():
         from film20.filmbasket.models import BasketItem
         user = self.request.user
         BasketItem.user_basket(user)
         film = self.get_object()
         try:
             basket = user._basket[film.id]
             owned = basket[1]
         except:
             owned = None
         context['user_film_basket'] = owned
     return context
Esempio n. 3
0
def user_recommendations(user, films, with_rated=False, order_by=None):
    film_ids = [f.id for f in films]

    if user and user.is_authenticated():
        profile = user.get_profile()
        basket = BasketItem.user_basket(user)
        rated_ids = set(Rating.get_user_ratings(user).keys())
        if profile.recommendations_status in (profile.NORMAL_RECOMMENDATIONS,
                                              profile.FAST_RECOMMENDATIONS):
            recommendations = recommendations_engine.compute_guess_score_for_films(
                user, film_ids)
        else:
            recommendations = {}

        for f in films:
            r = recommendations.get(f.id)
            b = basket.get(f.id)
            f._rated = f.id in rated_ids
            f._guess_rating = r or 0
            f._on_wishlist = b and b[0] and (
                b[0] != BasketItem.NOT_INTERESTED) or False
            f._on_shitlist = b and (b[0] == BasketItem.NOT_INTERESTED) or False
    else:
        for f in films:
            f._rated = False
            f._guess_rating = 0
            f._on_wishlist = False
            f._on_shitlist = False

    test_with_rated = lambda f: with_rated or not f._rated

    films = list(f for f in films if not f._on_shitlist and test_with_rated(f))

    comparator = create_film_comparator(order_by)
    return sorted(films, cmp=comparator)
Esempio n. 4
0
def user_recommendations(user, films, with_rated=False, order_by=None):
    film_ids = [f.id for f in films]

    if user and user.is_authenticated():
        profile = user.get_profile()
        basket = BasketItem.user_basket(user)
        rated_ids = set(Rating.get_user_ratings(user).keys())
        if profile.recommendations_status in (profile.NORMAL_RECOMMENDATIONS, profile.FAST_RECOMMENDATIONS):
            recommendations = recommendations_engine.compute_guess_score_for_films(user, film_ids)
        else:
            recommendations = {}

        for f in films:
            r = recommendations.get(f.id)
            b = basket.get(f.id)
            f._rated = f.id in rated_ids
            f._guess_rating = r or 0
            f._on_wishlist = b and b[0] and (b[0] != BasketItem.NOT_INTERESTED) or False
            f._on_shitlist = b and (b[0] == BasketItem.NOT_INTERESTED) or False
    else:
        for f in films:
            f._rated = False
            f._guess_rating = 0
            f._on_wishlist = False
            f._on_shitlist = False

    test_with_rated = lambda f: with_rated or not f._rated

    films = list(f for f in films if not f._on_shitlist and test_with_rated(f))

    comparator = create_film_comparator(order_by)
    return sorted(films, cmp=comparator)
Esempio n. 5
0
    def testRatingRemoveFromWishlist( self ):

        BasketItem.objects.create( film=self.film, wishlist=BasketItem.DYING_FOR, user=self.user )

        self.assertEqual( len( BasketItem.user_basket( self.user ) ), 1 )

        rating_helper.rate(self.user, 5, film_id=self.film.id)
    
        bi = BasketItem.objects.get( film=self.film, user=self.user )
        self.assertTrue( bi.wishlist is None )
Esempio n. 6
0
    def get_best_psi_films_queryset(self,
                                    user,
                                    tags=None,
                                    year_from=None,
                                    year_to=None,
                                    related_director=None,
                                    related_actor=None,
                                    popularity=None,
                                    include_features=(),
                                    exclude_features=(),
                                    netflix=None,
                                    exclude_production_country=None):
        from film20.recommendations import engine
        is_filtered = tags or year_from or year_to or related_director or related_actor or popularity or netflix

        if exclude_production_country:
            kw = dict(exclude_production_country=exclude_production_country)
        else:
            kw = {}
        all_scores = engine.compute_guess_score_for_all_films(user, **kw)
        all_scores = dict((key, val) for key, val in all_scores.items() if val)
        ratings = Rating.get_user_ratings(user)
        shitlisted = set(k for k, v in BasketItem.user_basket(user).items()
                         if v[0] == 9)

        film_ids = set(all_scores.keys())
        film_ids.difference_update(ratings.keys())
        film_ids.difference_update(shitlisted)

        if is_filtered:
            filtered = self._filter_films(tags, year_from, year_to,
                                          related_director, related_actor,
                                          popularity, netflix)
            filtered = filtered.filter(id__in=film_ids)
            film_ids = filtered.values_list('id', flat=True)

        if include_features:
            from film20.film_features.models import FilmFeature
            featured_ids = FilmFeature.matching_film_ids(
                include_features, exclude_features)
            film_ids = [id for id in film_ids if id in featured_ids]

        film_ids = sorted(film_ids, key=lambda f: -all_scores[f])

        from film20.utils.misc import ListWrapper

        class FilmWrapper(ListWrapper):
            def wrap(self, items):
                films = list(Film.objects.filter(id__in=items))
                films.sort(key=lambda f: -all_scores[f.id])
                for film in films:
                    film.guess_rating = all_scores.get(film.id)
                    yield film

        return FilmWrapper(film_ids)
Esempio n. 7
0
    def testRatingRemoveFromWishlist( self ):

        BasketItem.objects.create( film=self.film, wishlist=BasketItem.DYING_FOR, user=self.user )

        self.assertEqual( len( BasketItem.user_basket( self.user ) ), 1 )

        Rating.objects.create( type=Rating.TYPE_FILM, film=self.film, rating=5, 
                                parent=self.film, user=self.user)
    
        bi = BasketItem.objects.get( film=self.film, user=self.user )
        self.assertTrue( bi.wishlist is None )
Esempio n. 8
0
    def testRatingRemoveFromWishlist(self):

        BasketItem.objects.create(film=self.film,
                                  wishlist=BasketItem.DYING_FOR,
                                  user=self.user)

        self.assertEqual(len(BasketItem.user_basket(self.user)), 1)

        rating_helper.rate(self.user, 5, film_id=self.film.id)

        bi = BasketItem.objects.get(film=self.film, user=self.user)
        self.assertTrue(bi.wishlist is None)
Esempio n. 9
0
    def _get_recommendations(self):
        self.films = set()
        film_ids = []

        for film_set in self.film_sets:
            for f in film_set.values():
                if not f in self.films and f.has_screenings(
                        self.channel_ids, self.since):
                    self.films.add(f)
                    if f.film_id:
                        film_ids.append(f.film_id)

        if self.user and self.user.is_authenticated():
            profile = self.user.get_profile()
            basket = BasketItem.user_basket(self.user)
            rated_ids = Film._rated_film_ids(self.user)
            if profile.recommendations_status in (
                    profile.NORMAL_RECOMMENDATIONS,
                    profile.FAST_RECOMMENDATIONS):
                recommendations = recommendations_engine.compute_guess_score_for_films(
                    self.user, film_ids)
            else:
                recommendations = {}

            for f in self.films:
                if not f.film_id:
                    continue
                r = recommendations.get(f.film_id)
                b = basket.get(f.film_id)
                f._rated = f.film_id in rated_ids
                f._guess_rating = r or 0
                f._on_wishlist = b and b[0] and (
                    b[0] != BasketItem.NOT_INTERESTED) or False
                f._on_shitlist = b and (b[0]
                                        == BasketItem.NOT_INTERESTED) or False

        test_with_rated = lambda f: self.with_rated or not f._rated
        test_unmatched = lambda f: not self.without_unmatched or f.film_id

        films = list(
            f for f in self.films
            if not f._on_shitlist and test_with_rated(f) and test_unmatched(f))

        comparator = create_film_comparator(self.order_by)
        return sorted(films, cmp=comparator)
Esempio n. 10
0
    def get_best_psi_films_queryset(self, user, tags=None, year_from=None, year_to = None, related_director = None, related_actor = None, popularity = None, include_features=(), exclude_features=(), netflix=None, exclude_production_country=None):
        from film20.recommendations import engine
        is_filtered = tags or year_from or year_to or related_director or related_actor or popularity or netflix
        
        if exclude_production_country:
            kw = dict(exclude_production_country=exclude_production_country)
        else:
            kw = {}
        all_scores = engine.compute_guess_score_for_all_films(user, **kw)
        all_scores = dict((key, val) for key, val in all_scores.items() if val)
        ratings = Rating.get_user_ratings(user)
        shitlisted = set(k for k,v in BasketItem.user_basket(user).items() if v[0]==9)

        film_ids = set(all_scores.keys())
        film_ids.difference_update(ratings.keys())
        film_ids.difference_update(shitlisted)

        if is_filtered:
            filtered = self._filter_films(tags, year_from, year_to, related_director, related_actor, popularity, netflix)
            filtered = filtered.filter(id__in=film_ids)
            film_ids = filtered.values_list('id', flat=True)

        if include_features:
            from film20.film_features.models import FilmFeature
            featured_ids = FilmFeature.matching_film_ids(include_features, exclude_features)
            film_ids = [id for id in film_ids if id in featured_ids]

        film_ids = sorted(film_ids, key=lambda f: -all_scores[f])
        
        from film20.utils.misc import ListWrapper
        class FilmWrapper(ListWrapper):
            def wrap(self, items):
                films = list(Film.objects.filter(id__in=items))
                films.sort(key=lambda f:-all_scores[f.id])
                for film in films:
                    film.guess_rating = all_scores.get(film.id)
                    yield film
        return FilmWrapper(film_ids)
Esempio n. 11
0
    def __init__(self, data, request, film, initial=None):
        initial = initial or {}
        self.film = film
        self.user = request.unique_user
        initial['film_id'] = film.id

        if not 'rating_1' in initial:
            ratings = Rating.get_user_ratings(self.user)
            r = ratings.get(self.film.id)
            if r:
                initial['rating_1'] = r
        
        if self.user.is_authenticated():
            # collections only for registered users
            basket = BasketItem.user_basket(self.user)
            item = basket.get(film.id)
            if item:
                initial['dying_for'] = item[0] == BasketItem.DYING_FOR
                initial['not_interested'] = item[0] == BasketItem.NOT_INTERESTED
                initial['owned'] = item[1] == BasketItem.OWNED

        prefix = "flm-%d" % film.id
        super(forms.Form, self).__init__(data, initial=initial, prefix=prefix)
Esempio n. 12
0
 def __init__(self, data, user=None, film=None, mark_as_seen=False, initial=None):
     initial = initial or {}
     self.film = film
     self.user = user
     if user and user.is_authenticated():
         if not 'rating_1' in initial:
             ratings = Rating.get_user_ratings(user)
             r = ratings.get(self.film.id)
             if r:
                 initial['rating_1'] = r
         
         basket = BasketItem.user_basket(user)
         item = basket.get(film.id)
         if item:
             initial['dying_for'] = item[0] == BasketItem.DYING_FOR
             initial['not_interested'] = item[0] == BasketItem.NOT_INTERESTED
             initial['owned'] = item[1] == BasketItem.OWNED
         
         if mark_as_seen:
             film.mark_as_seen(user)
     
     prefix = "flm-%d" % film.id
     super(forms.Form, self).__init__(data, initial=initial, prefix=prefix)
Esempio n. 13
0
    def _get_recommendations(self):
        self.films = set()
        film_ids = []

        for film_set in self.film_sets:
            for f in film_set.values():
                if not f in self.films and f.has_screenings(self.channel_ids, self.since):
                    self.films.add(f)
                    if f.film_id:
                        film_ids.append(f.film_id)

        if self.user and self.user.is_authenticated():
            profile = self.user.get_profile()
            basket = BasketItem.user_basket(self.user)
            rated_ids = Film._rated_film_ids(self.user)
            if profile.recommendations_status in (profile.NORMAL_RECOMMENDATIONS, profile.FAST_RECOMMENDATIONS):
                recommendations = recommendations_engine.compute_guess_score_for_films(self.user, film_ids)
            else:
                recommendations = {}

            for f in self.films:
                if not f.film_id:
                    continue
                r = recommendations.get(f.film_id)
                b = basket.get(f.film_id)
                f._rated = f.film_id in rated_ids
                f._guess_rating = r or 0
                f._on_wishlist = b and b[0] and (b[0] != BasketItem.NOT_INTERESTED) or False
                f._on_shitlist = b and (b[0] == BasketItem.NOT_INTERESTED) or False

        test_with_rated = lambda f: self.with_rated or not f._rated
        test_unmatched = lambda f: not self.without_unmatched or f.film_id

        films = list(f for f in self.films if not f._on_shitlist and test_with_rated(f) and test_unmatched(f))

        comparator = create_film_comparator(self.order_by)
        return sorted(films, cmp=comparator)
Esempio n. 14
0
    def __init__(self, data, request, film, initial=None):
        initial = initial or {}
        self.film = film
        self.user = request.unique_user
        initial['film_id'] = film.id

        if not 'rating_1' in initial:
            ratings = Rating.get_user_ratings(self.user)
            r = ratings.get(self.film.id)
            if r:
                initial['rating_1'] = r

        if self.user.is_authenticated():
            # collections only for registered users
            basket = BasketItem.user_basket(self.user)
            item = basket.get(film.id)
            if item:
                initial['dying_for'] = item[0] == BasketItem.DYING_FOR
                initial['not_interested'] = item[
                    0] == BasketItem.NOT_INTERESTED
                initial['owned'] = item[1] == BasketItem.OWNED

        prefix = "flm-%d" % film.id
        super(forms.Form, self).__init__(data, initial=initial, prefix=prefix)