コード例 #1
0
ファイル: server.py プロジェクト: edmundo096/MoodMusic
def nav_home_search():
    """
    Route for Search and music playlist recreation in the Home page.
    Used when a Search is done in the nav bar, or a Mood Search is done.
    """
    if 'username' in session:
        # Check if there was any search.
        if request.form['search'] is not None:
            search = escape(request.form['search']).encode("utf-8")
            print "Search: " + search

            listKeyword = search.split()
            session['playlist'] = []

            # Check if search was empty, redirect to home.return
            if not listKeyword:
                return redirect("/home")

            # Search music by Moods (set by the same user), or as a common search.
            if listKeyword[0] == "mood:":
                # Check the type of mood search.
                if listKeyword[1] == 'mood:':
                    listMusic = DbFunct.song_songs_get_with_mood(listKeyword[2:], session['email'])
                elif listKeyword[1] == 'genre:':
                    listMusic = DbFunct.song_songs_get_with_mood_genres(listKeyword[2:], session['email'])
            else:
                listMusic = DbFunct.song_songs_get_with_search(listKeyword)

            # Cerate playlist.
            for music in listMusic:
                session['playlist'].append({'artist': music.artist , 'album': music.album, 'title': music.title})

            print "nav_home_search() POST - session['playlist']: " + str(session['playlist'])

            # Check if there is a 1st song.
            listMusic = session['playlist']
            if not listMusic:
                initial_song = None
            else:
                # Get the 1st song from the DB for the client "music" variable (for metadata and src load).
                initial_song = DbFunct.song_data_get(listMusic[0]['artist'], listMusic[0]['album'], listMusic[0]['title'])

            return render_template('home.html', music=initial_song, music_list=listMusic, username=session['username'])
        else:
            return redirect("/home")
    else:
        return redirect(url_for('nav_login'))