Пример #1
0
def rate(request, song_id):
    is_logged_in = request.user.is_authenticated()
    if is_logged_in:
        username = request.user.username
    else:
        username = None
    
    try:
        song = Song.objects.get(id=song_id)
    except ValueError:
        return Http404()
    ratings = Rating.objects.filter(username=request.user)
    
    rating = 0
    if 'rate_1' in request.POST:
        rating = 1
    elif 'rate_2' in request.POST:
        rating = 2
    elif 'rate_3' in request.POST:
        rating = 3
    elif 'rate_4' in request.POST:
        rating = 4
    elif 'rate_5' in request.POST:
        rating = 5

    if rating != 0:
        # Check to see if the user has already rated the song.
        try: 
            r = Rating.objects.get(username=request.user, song=song)
        # If he hasn't, make a new rating tuple.
        except Rating.DoesNotExist:
            r = Rating(username=request.user, 
                       song=song, 
                       rating=rating)
        # If he has, update the existing rating.
        else:
            r.rating = rating
        r.save()
        return HttpResponseRedirect('success/')

    else:
        # Loads another random song for "rate next."
        songs = Song.objects.exclude(rating__username__exact=request.user)
        try:
            next_song = random_song(songs)
        except ValueError:
            return Http404()
        
        youtube_link = get_embedded_link(get_video(song.title, song.artist.name))
        return render_to_response('userpages/rate.html', 
                                  {'is_logged_in': is_logged_in, 
                                  'username': username, 
                                  'song': song,
                                  'ratings': ratings,
                                  'youtube_link': youtube_link,
                                  'next_song': next_song})
Пример #2
0
def listen(request, playlist_id):
    is_logged_in = request.user.is_authenticated()
    if is_logged_in:
        username = request.user.username
    else:
        username = None

    try:
        playlist = Playlist.objects.get(id=playlist_id)
        if request.user != playlist.username:
            return render_to_response("permission_denied.html")
    except ValueError:
        return Http404()

    ratings = Rating.objects.filter(username=request.user)
    song = playlist.song
    playlist_songs = SimilarSong.objects.filter(song=song).order_by("score")[:100]
    playlist_song = random_song(playlist_songs).similar_song

    youtube_link = get_embedded_link(get_video(playlist_song.title, playlist_song.artist.name))

    rating = 0
    if "rate_1" in request.POST:
        rating = 1
    elif "rate_2" in request.POST:
        rating = 2
    elif "rate_3" in request.POST:
        rating = 3
    elif "rate_4" in request.POST:
        rating = 4
    elif "rate_5" in request.POST:
        rating = 5

    if rating != 0:
        # Check to see if the user has already rated the song.
        try:
            r = Rating.objects.get(username=request.user, song=playlist_song)
        # If he hasn't, make a new rating tuple.
        except Rating.DoesNotExist:
            r = Rating(username=request.user, song=playlist_song, rating=rating)
        # If he has, update the existing rating.
        else:
            r.rating = rating
        r.save()
        rated = True
        return render_to_response(
            "userpages/listen.html",
            {
                "is_logged_in": is_logged_in,
                "username": username,
                "song": playlist_song,
                "ratings": ratings,
                "youtube_link": youtube_link,
                "rated": rated,
                "playlist_id": playlist_id,
            },
        )

    else:
        return render_to_response(
            "userpages/listen.html",
            {
                "is_logged_in": is_logged_in,
                "username": username,
                "song": playlist_song,
                "ratings": ratings,
                "youtube_link": youtube_link,
                "playlist_id": playlist_id,
            },
        )
Пример #3
0
def profile(request):
    is_logged_in = request.user.is_authenticated()
    if is_logged_in:
        username = request.user.username
    else:
        username = None

    songs = Song.objects.all()
    ratings = Rating.objects.filter(username=request.user)
    num_ratings = len(ratings)
    
    # Code that handles search filtering
    # Song search
    errors = []
    if 'q_songs' in request.GET or 'q_artists' in request.GET:
        if 'q_songs' in request.GET:
            q = request.GET['q_songs']
        elif 'q_artists' in request.GET:
            q = request.GET['q_artists']
        else:
            raise Exception('Error: Unhandled GET request.')
        if not q:
            errors.append('Enter a search term.')
        # If there were errors in the search query, return them.
        if len(errors) != 0:
            return render_to_response('userpages/profile.html',
                                      {'is_logged_in': is_logged_in,
                                      'username': username,
                                      'songs': songs,
                                      'ratings': ratings,
                                      'errors': errors})
        # Otherwise, execute the search query.
        else:
            queried = True
            if 'q_songs' in request.GET:
                songs = Song.objects.filter(title__icontains=q)
            elif 'q_artists' in request.GET:
                songs = Song.objects.filter(artist__name__icontains=q)
            else:
                raise Exception('Error: Unhandled GET request.')
            return render_to_response('userpages/profile.html',
                                      {'is_logged_in': is_logged_in,
                                      'username': username,
                                      'songs': songs,
                                      'ratings': ratings,
                                      'query': q,
                                      'queried': queried})

    # Code that handles the random song button
    if 'random_song' in request.GET:
        try:
            song = random_song_all()
        except ValueError:
            return Http404()
        return HttpResponseRedirect('/rate/'+str(song.id))
                
    # Code that handles the recommend song button
    if 'recommend_song' in request.GET:
        try:
            songs = Song.objects.filter(recommendedsong__username=request.user, recommendedsong__predicted_rating__gt=3.0)
            song = random_song(songs)
        except ValueError:
            return Http404()
        return HttpResponseRedirect('/rate/'+str(song.id))

    # Code that handles the filter unrated songs button
    if 'show_unrated' in request.GET:
        unrated = True;
        songs = Song.objects.exclude(rating__username__exact=request.user)
        return render_to_response('userpages/profile.html', 
                                  {'is_logged_in': is_logged_in, 
                                  'username': username,
                                  'songs': songs,
                                  'ratings': ratings,
                                  'unrated': unrated})

    # Code that handles the randomly rate 200 songs button
    # Only to be used for testing purposes. Remove this functionality on release.
    if 'rate_200_random' in request.GET:
        songs = list(Song.objects.all())
        for i in range(200):
            song = random_song(songs)
            
            rating = random.randint(1, 5)

            try: 
                r = Rating.objects.get(username=request.user, song=song)
            # If he hasn't, make a new rating tuple.
            except Rating.DoesNotExist:
                r = Rating(username=request.user, 
                           song=song, 
                           rating=rating)
            # If he has, update the existing rating.
            else:
                r.rating = rating
            r.save()

            songs.remove(song)

        return HttpResponseRedirect('/')

    # Code that handles the randomly rate all songs button
    # Only to be used for testing purposes. Remove this functionality on release.
    if 'rate_all_random' in request.GET:
        songs = Song.objects.all()
        for song in songs:
            rating = random.randint(1, 5)

            try: 
                r = Rating.objects.get(username=request.user, song=song)
            # If he hasn't, make a new rating tuple.
            except Rating.DoesNotExist:
                r = Rating(username=request.user, 
                           song=song, 
                           rating=rating)
            # If he has, update the existing rating.
            else:
                r.rating = rating
            r.save()
        return HttpResponseRedirect('/')

    # If there is wasn't search query, just return the normal form.
    return render_to_response('userpages/profile.html', 
                              {'is_logged_in': is_logged_in, 
                              'username': username,
                              'songs': songs,
                              'ratings': ratings})
Пример #4
0
def listen(request, playlist_id):
    is_logged_in = request.user.is_authenticated()
    if is_logged_in:
        username = request.user.username
    else:
        username = None
    
    try:
        playlist = Playlist.objects.get(id=playlist_id)
        if request.user != playlist.username:
            return render_to_response('permission_denied.html')
    except ValueError:
        return Http404()

    ratings = Rating.objects.filter(username=request.user)
    song = playlist.song
    playlist_songs = SimilarSong.objects.filter(song=song).order_by('score')[:100]
    playlist_song = random_song(playlist_songs).similar_song

    youtube_link = get_embedded_link(get_video(playlist_song.title, playlist_song.artist.name))
    
    rating = 0
    if 'rate_1' in request.POST:
        rating = 1
    elif 'rate_2' in request.POST:
        rating = 2
    elif 'rate_3' in request.POST:
        rating = 3
    elif 'rate_4' in request.POST:
        rating = 4
    elif 'rate_5' in request.POST:
        rating = 5

    if rating != 0:
        # Check to see if the user has already rated the song.
        try: 
            r = Rating.objects.get(username=request.user, song=playlist_song)
        # If he hasn't, make a new rating tuple.
        except Rating.DoesNotExist:
            r = Rating(username=request.user, 
                       song=playlist_song, 
                       rating=rating)
        # If he has, update the existing rating.
        else:
            r.rating = rating
        r.save()
        rated = True
        return render_to_response('userpages/listen.html', 
                                  {'is_logged_in': is_logged_in, 
                                  'username': username, 
                                  'song': playlist_song,
                                  'ratings': ratings,
                                  'youtube_link': youtube_link,
                                  'rated': rated,
                                  'playlist_id': playlist_id})

    else:        
        return render_to_response('userpages/listen.html', 
                                  {'is_logged_in': is_logged_in, 
                                  'username': username, 
                                  'song': playlist_song,
                                  'ratings': ratings,
                                  'youtube_link': youtube_link,
                                  'playlist_id': playlist_id})