Beispiel #1
0
def create_playlist(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()
    
    errors = []
    if 'title' in request.GET:
        q = request.GET['title']
        if not q:
            errors.append('Enter a title for your playlist.')
        try:
            db_playlist = Playlist.objects.get(username=request.user, song=song)
            errors.append('You already have a playlist based on this song. Please go back and choose another song.')
        except Playlist.DoesNotExist:
            # Do nothing.
            pass
            
        # If there were errors in the form, return them.
        if len(errors) != 0:
            return render_to_response('userpages/create_playlist.html',
                                      {'is_logged_in': is_logged_in,
                                      'username': username,
                                      'song': song,
                                      'errors': errors})
        # Otherwise, create the playlist.
        else:
            db_playlist = Playlist(username=request.user, title=q, song=song)
            db_playlist.save()
            return HttpResponseRedirect('success/')

    else:
        return render_to_response('userpages/create_playlist.html',
                                  {'is_logged_in': is_logged_in,
                                  'username': username,
                                  'song': song})
Beispiel #2
0
def create_playlist(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()

    errors = []
    if "title" in request.GET:
        q = request.GET["title"]
        if not q:
            errors.append("Enter a title for your playlist.")
        try:
            db_playlist = Playlist.objects.get(username=request.user, song=song)
            errors.append("You already have a playlist based on this song. Please go back and choose another song.")
        except Playlist.DoesNotExist:
            # Do nothing.
            pass

        # If there were errors in the form, return them.
        if len(errors) != 0:
            return render_to_response(
                "userpages/create_playlist.html",
                {"is_logged_in": is_logged_in, "username": username, "song": song, "errors": errors},
            )
        # Otherwise, create the playlist.
        else:
            db_playlist = Playlist(username=request.user, title=q, song=song)
            db_playlist.save()
            return HttpResponseRedirect("success/")

    else:
        return render_to_response(
            "userpages/create_playlist.html", {"is_logged_in": is_logged_in, "username": username, "song": song}
        )
Beispiel #3
0
def _add_to_playlist(song, usr):
    # url validating sometimes could be failed?
    if song.audio_url and song.audio_url.find('<') == -1 and song.audio_url.find('>') == -1:
        audio_url = Playlist.objects.filter(location__iexact=song.audio_url, user=usr)
        if audio_url:
            playlist = Playlist()
            playlist.user = usr
            playlist.location = song.audio_url
            playlist.creator = song.singer
            playlist.album = song.album
            playlist.title = song.title
            playlist.annotation = song.comments
            playlist.image = song.song_cover_url
            playlist.info = song.comments
            playlist.link = song.source
            playlist.save()