def show_my_profile(request): """ Shows the profile of the logged in user """ # object to return favorite_films = None friend_invitations = None profile = None # get favorite movies film_helper = FilmHelper() favorite_films = film_helper.get_favorite_films(request.user) # get favorite actors # get favorite directors # get friend invitations # friend_helper = FriendHelper() # pending_received_invitations = friend_helper.get_pending_received_invitations(request.user) # pending_sent_invitations = friend_helper.get_pending_sent_invitations(request.user) # get profile data profile = request.user.get_profile() logger.debug("Profile description: " + unicode(profile.description)) context = { "favorite_films": favorite_films, "pending_received_invitations": pending_received_invitations, "pending_sent_invitations": pending_sent_invitations, "profile": profile, "user": request.user, } return render_to_response(templates["ACCOUNT"], context, context_instance=RequestContext(request))
def random_film_to_rate(request): """ Widget for main page (for not logged in users) displaying a film to rate, selected from the list of 10 most popular films. """ user = request.user if user.is_authenticated(): film = Film.get_next_film_to_rate(user) else: key = cache.Key("popular_films_list") popular_films = cache.get(key) if popular_films is None: fhelper = FilmHelper() lang = getattr(settings, 'LANGUAGE_CODE', 'en') if lang == 'en': popular_films = fhelper.get_popular_films( exclude_nonEnglish=True) else: popular_films = fhelper.get_popular_films() cache.set(key, popular_films) film = popular_films and random.choice(popular_films) or None return { 'film': film, }
def random_film_to_rate(request): """ Widget for main page (for not logged in users) displaying a film to rate, selected from the list of 10 most popular films. """ user = request.user if user.is_authenticated(): film = Film.get_next_film_to_rate(user) else: key = cache.Key("popular_films_list") popular_films = cache.get(key) if popular_films is None: fhelper = FilmHelper() lang = getattr(settings, 'LANGUAGE_CODE', 'en') if lang == 'en': popular_films = fhelper.get_popular_films(exclude_nonEnglish=True) else: popular_films = fhelper.get_popular_films() cache.set(key, popular_films) film = popular_films and random.choice(popular_films) or None return { 'film': film, }
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_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_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_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 get_queryset(self): tags = None year_from = None year_to = None related_director = None related_actor = None popularity = None tags = self.get_genre() is_valid, fake_film_specs_form, film_specs_form = \ self.get_specs_form() if is_valid: tags = tags year_from = fake_film_specs_form.year_from year_to = fake_film_specs_form.year_to related_director = \ fake_film_specs_form.related_director_as_object related_actor = fake_film_specs_form.related_actor_as_object popularity = fake_film_specs_form.popularity films = None if self.request.user.is_authenticated(): recom_status = self.request.user.get_profile().recommendations_status else: recom_status = False if recom_status: recom_helper = RecomHelper() films = recom_helper.get_best_psi_films_queryset( user_id = self.request.user.id, tags = tags, year_from = year_from, year_to = year_to, related_director = related_director, related_actor = related_actor, popularity = popularity, ) else: film_helper = FilmHelper() films = film_helper.get_best_films( tags = tags, year_from = year_from, year_to = year_to, related_director = related_director, related_actor = related_actor, popularity = popularity ) films = list(films) return films
def get_queryset(self): tags = None year_from = None year_to = None related_director = None related_actor = None popularity = None tags = self.get_genre() is_valid, fake_film_specs_form, film_specs_form = \ self.get_specs_form() if is_valid: tags = tags year_from = fake_film_specs_form.year_from year_to = fake_film_specs_form.year_to related_director = \ fake_film_specs_form.related_director_as_object related_actor = fake_film_specs_form.related_actor_as_object popularity = fake_film_specs_form.popularity films = None if self.request.user.is_authenticated(): recom_status = self.request.user.get_profile( ).recommendations_status else: recom_status = False if recom_status: recom_helper = RecomHelper() films = recom_helper.get_best_psi_films_queryset( self.request.user, tags=tags, year_from=year_from, year_to=year_to, related_director=related_director, related_actor=related_actor, popularity=popularity, ) else: film_helper = FilmHelper() films = film_helper.get_best_films( tags=tags, year_from=year_from, year_to=year_to, related_director=related_director, related_actor=related_actor, popularity=popularity) return films
def random_top_movie(): """Displays some random popular movie on Filmaster.""" key = cache.Key("popular_films") popular_films = cache.get(key) if popular_films is None: # Get most popular films fhelper = FilmHelper() popular_films = list( fhelper.get_popular_films(exclude_nonEnglish=True)) cache.set(key, popular_films) random.shuffle(popular_films) if len(popular_films) > 0: return {'movie': popular_films[0]} else: return {'movie': None}
def random_top_movie(): """Displays some random popular movie on Filmaster.""" key = cache.Key("popular_films") popular_films = cache.get(key) if popular_films is None: # Get most popular films fhelper = FilmHelper() popular_films = list(fhelper.get_popular_films( exclude_nonEnglish=True)) cache.set(key, popular_films) random.shuffle(popular_films) if len(popular_films) > 0: return {'movie': popular_films[0]} else: return {'movie': None}
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)
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 users_best_rated(user): """Displays user's top rated movies.""" cache_key = cache.Key("profile_page_best_films", user) best_movies = cache.get(cache_key) NUMBER_OF_USER_BEST_FILMS = \ getattr(settings, 'NUMBER_OF_USER_BEST_FILMS') if not best_movies: fhelper = FilmHelper() best_movies = fhelper.get_users_best_films(user, NUMBER_OF_USER_BEST_FILMS + 1) cache.set(cache_key, best_movies) show_more = len( best_movies ) == NUMBER_OF_USER_BEST_FILMS + 1 best_movies = best_movies[:NUMBER_OF_USER_BEST_FILMS] return {'movies': best_movies, 'show_more': show_more, 'act_user': user }
def users_best_rated(user): """Displays user's top rated movies.""" cache_key = cache.Key("profile_page_best_films", user) best_movies = cache.get(cache_key) NUMBER_OF_USER_BEST_FILMS = \ getattr(settings, 'NUMBER_OF_USER_BEST_FILMS') if not best_movies: fhelper = FilmHelper() best_movies = fhelper.get_users_best_films( user, NUMBER_OF_USER_BEST_FILMS + 1) cache.set(cache_key, best_movies) show_more = len(best_movies) == NUMBER_OF_USER_BEST_FILMS + 1 best_movies = best_movies[:NUMBER_OF_USER_BEST_FILMS] return {'movies': best_movies, 'show_more': show_more, 'act_user': user}
def top_recommendations_all(context, films_number=4): """ Displays user's top recommended movies (most popular for not logged in). """ POPULAR_FILMS_NUMBER_ALL = getattr(settings, 'POPULAR_FILMS_MAIN_PAGE_NUMBER_ALL') user = context.get('recommendations_user', context['request'].user) key = cache.Key("popular_films_main_page", user) films = cache.get(key) if (not films) or len(films) < films_number: key_all = cache.Key("popular_films_main_page_all", user) all_films = cache.get(key_all) if not all_films: has_recommendations = user.is_authenticated() and \ user.get_profile().recommendations_status if has_recommendations: recom_helper = RecomHelper() all_films = list( recom_helper.get_best_psi_films_queryset(user) [:POPULAR_FILMS_NUMBER_ALL]) else: fhelper = FilmHelper() all_films = list( fhelper.get_popular_films( number_of_films=POPULAR_FILMS_NUMBER_ALL, exclude_nonEnglish=True)) cache.set(key_all, all_films, cache.A_MONTH) # Select random few films random.shuffle(all_films) films = all_films[:films_number] cache.set(key, films, cache.A_QUARTER) return films[:films_number]
def show_my_profile(request): """ Shows the profile of the logged in user """ # object to return favorite_films = None friend_invitations = None profile = None # get favorite movies film_helper = FilmHelper() favorite_films = film_helper.get_favorite_films(request.user) # get favorite actors # get favorite directors # get friend invitations # friend_helper = FriendHelper() # pending_received_invitations = friend_helper.get_pending_received_invitations(request.user) # pending_sent_invitations = friend_helper.get_pending_sent_invitations(request.user) # get profile data profile = request.user.get_profile() logger.debug("Profile description: " + unicode(profile.description)) context = { 'favorite_films': favorite_films, 'pending_received_invitations': pending_received_invitations, 'pending_sent_invitations': pending_sent_invitations, 'profile': profile, 'user': request.user, } return render_to_response( templates['ACCOUNT'], context, context_instance=RequestContext(request), )
def top_recommendations_all(context, films_number=4): """ Displays user's top recommended movies (most popular for not logged in). """ POPULAR_FILMS_NUMBER_ALL = getattr(settings, 'POPULAR_FILMS_MAIN_PAGE_NUMBER_ALL') user = context['request'].user key = cache.Key("popular_films_main_page", user) films = cache.get(key) if (not films) or len(films) < films_number: key_all = cache.Key("popular_films_main_page_all", user) all_films = cache.get(key_all) if not all_films: has_recommendations = user.is_authenticated() and \ user.get_profile().recommendations_status if has_recommendations: recom_helper = RecomHelper() all_films = list(recom_helper.get_best_psi_films_queryset( user_id = user.id, offset=0, limit=POPULAR_FILMS_NUMBER_ALL)) else: fhelper = FilmHelper() all_films = list(fhelper.get_popular_films( number_of_films=POPULAR_FILMS_NUMBER_ALL, exclude_nonEnglish=True)) cache.set(key_all, all_films, cache.A_MONTH) # Select random few films random.shuffle(all_films) films = all_films[:films_number] cache.set(key, films, cache.A_QUARTER) return films[:films_number]
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_enrich_query_with_tags(self): print "Test enrich query with tags" recom_helper = RecomHelper() film_helper = FilmHelper() comedies = recom_helper.get_films_for_tag('comedy') scifis = recom_helper.get_films_for_tag('sci-fi') horrors = recom_helper.get_films_for_tag('horror') thrillers = recom_helper.get_films_for_tag('thriller') dramas = recom_helper.get_films_for_tag('drama') fantasies = recom_helper.get_films_for_tag('fantasy') mysteries = recom_helper.get_films_for_tag('mystery') animations = recom_helper.get_films_for_tag('animation') series = recom_helper.get_films_for_tag('series') self.assertEquals(len(comedies), 5) self.assertEquals(len(scifis), 6) self.assertEquals(len(horrors), 3) self.assertEquals(len(thrillers), 4) self.assertEquals(len(dramas), 4) self.assertEquals(len(fantasies), 3) self.assertEquals(len(mysteries), 2) self.assertEquals(len(animations), 3) self.assertEquals(len(series), 4)
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)
description = unicode(truncate_words(descr, words)) short = True else: description = descr return { 'film': film, 'edit': edit, 'can_edit': context['request'].user.is_authenticated(), 'description': description, 'short': short } from film20.core.film_helper import FilmHelper film_helper = FilmHelper() from film20.core.models import FilmLocalized @register.inclusion_tag('movies/movie/related.html', takes_context=True) def film_related(context, film): NUMBER_RELATED = getattr(settings, 'NUMBER_OF_FILMS_RELATED', 6) related = cache_query(film.get_related_films()[:NUMBER_RELATED], "film_related_films", film) return { 'film': film, 'perms': context.get('perms'), 'related': related, }
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)