Beispiel #1
0
def nav_home():
    """
    Main Home page route, shows the user playlist songs.
    If user has no playlist, show no songs nor initial songs.
    If there is a 'random' key in the querystring, makes call to the either gets 10 random songs with 'song_songs_get_all()',
    Then, If user selected one song (that is, a song data is on the query string), add it to the playlist.
    """
    if 'username' in session:
        # If there is a 'random' query string key, get the 10 random songs.
        if request.args.has_key('random'):
            session['playlist'] = []
            for music in DbFunct.song_songs_get_all(random=True, limit=config.random_songs_number):
                session['playlist'].append({'artist': music.artist , 'album': music.album, 'title': music.title})
            session.modified = True

        list = session['playlist']

        # If a song was passed by arguments, add it at the beginning of the playlist.
        args_has_song = request.args.has_key('artist') and request.args.has_key('album') and request.args.has_key('title')
        if args_has_song:
            # Todo: check for SQL injection.
            # Check if the song in arguments in on DB.
            args_song =  DbFunct.song_data_get(request.args.get('artist'), request.args.get('album'), request.args.get('title'))
            if args_song is not None:
                # this change is not picked up because a mutable object (here a list) is changed.
                list.insert(0, {
                    'artist': args_song.artist, 'album': args_song.album, 'title': args_song.title
                })
                # so mark it as modified yourself
                session.modified = True

        print "home list: {list}".format(list=list)

        # Check if there is a 1st song.
        if not session['playlist']:
            initial_song = None
        else:
            # Get the first song from the list.
            initial_song = DbFunct.song_data_get(list[0]['artist'], list[0]['album'], list[0]['title'])

        return render_template('home.html', music=initial_song, music_list=list, username=session['username'])

    else:
        return redirect(url_for('nav_login'))