示例#1
0
def search(s_results=None):
    searchform = SearchForm()
    suggest_pl_but = SuggestedPlaylistButton()
    art_select = ArtistSelect(request.form)

    if searchform.validate_on_submit():
        # if request.form.get('artist_search'):
        new_artist = searchform.artist_search.data
        User.search_results = search_artist_echonest(new_artist)
        art_select.artist_display.choices = User.search_results

    if art_select.artist_display.data:
        if art_select.is_submitted():
            option_n = int(art_select.artist_display.data) + 1
            chosen_art = User.search_results[option_n][1]
            User.artists.append(chosen_art)

    if suggest_pl_but.validate_on_submit():
        if request.form.get('add_button'):
            User.artists.append(["DO 1"])
    return render_template('search.html',
                           searchform=searchform,
                           art_select=art_select,
                           suggest_pl_but=suggest_pl_but,
                           artists=User.artists)
示例#2
0
def search(s_results=None):
    searchform = SearchForm()
    suggest_pl_but = SuggestedPlaylistButton()
    art_select = ArtistSelect(request.form)

    if searchform.validate_on_submit():
        # if request.form.get('artist_search'):
        new_artist = searchform.artist_search.data
        User.search_results = search_artist_echonest(new_artist)
        art_select.artist_display.choices = User.search_results

    if art_select.artist_display.data:
        if art_select.is_submitted():
            option_n = int(art_select.artist_display.data) + 1
            chosen_art = User.search_results[option_n][1]
            User.artists.append(chosen_art)

    if suggest_pl_but.validate_on_submit():
        if request.form.get('add_button'):
            User.artists.append(["DO 1"])
    return render_template('search.html', searchform=searchform,
                            art_select=art_select,
                            suggest_pl_but=suggest_pl_but,
                            artists=User.artists)
示例#3
0
文件: auth.py 项目: tychovk/spotifest
def festival(url_slug):
    '''
    Fetches a festival page based on the url_slug provided:
     - retrieve contributor information from dB
     - retrieve artist preferences of current user from dB or cache
     - prepares page forms that populate page
    '''
    current_festival = db.get_info_from_database(url_slug)
    user_cache.cur_festival_id = current_festival[0]
    if not current_festival:
        flash(("Festival '{}' does not exist! Please check"
               "the code and try again.").format(url_slug))
        return redirect(url_for('home'))

    organizer = current_festival[2]
    _user = session.get('user_id')
    app.logger.warning("User '{}' accessing festival '{}'".format(_user,
                                                                   url_slug))
    is_org = True
    # check if organizer & if so, find name
    if organizer != _user:
        is_org = False
        if current_festival[1]:
            festival_name = current_festival[1]
        else:
            festival_name = "Spotifest 2016"
    elif organizer == _user:
        is_org = True
        festival_name = None
    # fetch contributors: the 0th term = the main organizer!
    try:
        all_users = db.get_contributors(current_festival[0])
        app.logger.warning("All users in this festival -- '{}'".format(all_users))
        if all_users is None:
            flash(("Festival '{}' is having problems. Please check with the "
                   "organizer. Try again later or create a new festival!").format(url_slug))
            return redirect(url_for('home'))
    except:
        app.logger.warning("Couldn't find contributors - check DB functions or app code.")
        flash(("Festival '{}' is having problems. Please check with the "
               "organizer. Try again later or create a new festival!.").format(url_slug))
        return redirect(url_for('home'))

    new = None
    new_artist = None

    current_user = load_user(session.get('user_id')).access
    s = spotipy.Spotify(auth=current_user)
    try:
        if not user_cache.retrieve_preferences(url_slug):
            processor = helpers.AsyncAdapter(app)
            artists = processor.get_user_preferences(s)
            user_cache.save_preferences(artists, url_slug)
        else:
            app.logger.warning("Current # of artists for "
                               "user '{}' - '{}'".format(_user, len(user_cache.retrieve_preferences(url_slug))))
    except:
        app.logger.warning("No artists followed found in the user's Spotify account.")

    # prep forms
    searchform = frontend_helpers.SearchForm()
    suggested_pl_butt = frontend_helpers.SuggestedPlaylistButton()
    art_select = frontend_helpers.ArtistSelect(request.form)
    params_form = frontend_helpers.ParamsForm()

    saved_params = db.get_parameters(_user, url_slug)
    frontend_helpers.populate_params(params_form, saved_params)

    if searchform.validate_on_submit():
        s_artist = searchform.artist_search.data
        user_cache.search_results = helpers.search_artist_echonest(s_artist)
        art_select.artist_display.choices = user_cache.search_results
        app.logger.warning("Search results '{}'".format(user_cache.search_results))

    if request.form.get("selectartist"):
        chosen_art = request.form.get("selectartist")
        cur_user_preferences = user_cache.retrieve_preferences(url_slug)
        new_artist = chosen_art

        if not cur_user_preferences or chosen_art not in cur_user_preferences:
            app.logger.warning("Adding chosen artist.. {}".format(chosen_art))
            user_cache.update_preferences(set([chosen_art]), url_slug)
            new_artist = chosen_art
            new = 1
        else:
            new = 0
        user_cache.search_results = list()

    if suggested_pl_butt.validate_on_submit():
        if request.form.get("add_button"):
            user_cache.update_preferences(helpers.suggested_artists, url_slug)
            new = True

    return render_template('festival.html', url_slug=url_slug,
                           s_results=user_cache.search_results,
                           art_select=art_select, searchform=searchform,
                           suggested_pl_butt=suggested_pl_butt,
                           artists=user_cache.artists,
                           params_form=params_form,
                           all_users=all_users,
                           festival_name=festival_name,
                           user=_user,
                           new=new, new_artist=new_artist, is_org=is_org)
示例#4
0
def festival(url_slug):
    current_festival = db.get_info_from_database(url_slug)
    user_cache.cur_festival_id = current_festival[0]
    if user_cache.artists:
        for person in user_cache.artists.keys():
            print "Person: ", person
            if user_cache.artists[person].keys():
                print "Festivals Active in: ", user_cache.artists[person].keys()

    if not current_festival:
        flash(("Festival '{}' does not exist! Please check"
               "the code and try again.").format(url_slug))
        return redirect(url_for('home'))

    organizer = current_festival[2]
    _user = session.get('user_id')
    is_org = True
    # check if organizer & if so, find name
    if organizer != _user:
        is_org = False
        festival_name = current_festival[1]
    elif organizer == _user:
        is_org = True
        festival_name = None
    # fetch contributors: the 0th term = the main organizer!
    try:
        all_users = db.get_contributors(current_festival[0])
        if all_users is None:
            flash(("Festival '{}' is having problems. Please check with the "
                "organizer. Try again later.").format(url_slug))
            return redirect(url_for('home'))
    except:
        flash(("Festival '{}' is having problems. Please check with the "
                "organizer. Try again later.").format(url_slug))
        return redirect(url_for('home'))

    new = None
    new_artist = None

    current_user = load_user(session.get('user_id')).access
    s = spotipy.Spotify(auth=current_user)
    try:
        if not user_cache.retrieve_preferences(url_slug):
            processor = helpers.AsyncAdapter(app)
            artists = processor.get_user_preferences(s)
            user_cache.save_preferences(artists, url_slug)
        else:
            print "CURRENT NUM of ARTISTS", len(user_cache.retrieve_preferences(url_slug))
    except:
        print ("No artists followed found in the user's Spotify account.")

    # prep forms
    searchform = frontend_helpers.SearchForm()
    suggested_pl_butt = frontend_helpers.SuggestedPlaylistButton()
    art_select = frontend_helpers.ArtistSelect(request.form)
    params_form = frontend_helpers.ParamsForm()

    if searchform.validate_on_submit():
        s_artist = searchform.artist_search.data
        user_cache.search_results = helpers.search_artist_echonest(s_artist)
        art_select.artist_display.choices = user_cache.search_results

    if art_select.artist_display.data:
        if art_select.is_submitted():
            option_n = int(art_select.artist_display.data) - 1
            chosen_art = user_cache.search_results[option_n][1]
            cur_user_preferences = user_cache.retrieve_preferences(url_slug)
            if chosen_art not in cur_user_preferences:
                print "ADDING CHOSEN ART", chosen_art
                user_cache.update_preferences(set([chosen_art]), url_slug)
                new_artist = chosen_art
                new = 1
            else:
                new = 0
            # user_cache.search_results = list()

    elif suggested_pl_butt.validate_on_submit():
        if request.form.get("add_button"):
            new_artist = ', '.join(suggested_artists)
            user_cache.update_preferences(set([chosen_art]), url_slug)
            new = True

    return render_template('festival.html', url_slug=url_slug,
                           s_results=user_cache.search_results,
                           art_select=art_select, searchform=searchform,
                           suggested_pl_butt=suggested_pl_butt,
                           artists=user_cache.artists,
                           params_form=params_form,
                           all_users=all_users,
                           festival_name=festival_name,
                           user=_user,
                           new=new, new_artist=new_artist, is_org=is_org)