Esempio n. 1
0
def new():
    current_user = load_user(session.get('user_id'))
    new_url_slug = helpers.generate_urlslug(current_user.id)
    new_catalog = helpers.Catalog(new_url_slug, 'general')
    s = spotipy.Spotify(auth=current_user.access)

    processor = helpers.AsyncAdapter(app)
    user_cache.save_preferences(processor.get_user_preferences(s), new_url_slug)
    artists = user_cache.retrieve_preferences(new_url_slug)

    if user_cache.retrieve_preferences(new_url_slug):
        processor.populate_catalog(artists, 3, catalog=new_catalog)
    if app.config['IS_ASYNC'] is True:
        save_task = db.save_to_database.apply_async(args=[None, current_user.id,
                                                    None, None, new_catalog.id,
                                                    new_url_slug])
        while True:
            if save_task.state == 'SUCCESS':
                print 'just saved DB'
                break
    else:
        db.save_to_database(None, current_user.id, None, None,
                            new_catalog.id, new_url_slug)

    current_festival = db.get_info_from_database(new_url_slug)
    festivalId = current_festival[0]
    userId = current_festival[2]
    try:
        db.save_contributor(festivalId, userId, organizer=1, ready=1)
    except:
        print ("SAVING CONTRIBUTOR FAILED!")
    return redirect(url_for('festival', url_slug=new_url_slug))
Esempio n. 2
0
def new():
    ''' Create a new festival (new_url_slug), gather user
    preferences of organizer, save festival to dB, save organizer
    to dB as a contributor and redirect to newly minted festival page.
    '''
    current_user = load_user(session.get('user_id'))
    new_url_slug = helpers.generate_urlslug(current_user.id)
    new_catalog = helpers.Catalog(new_url_slug, 'general')
    s = spotipy.Spotify(auth=current_user.access)

    processor = helpers.AsyncAdapter(app)
    user_cache.save_preferences(processor.get_user_preferences(s), new_url_slug)
    artists = user_cache.retrieve_preferences(new_url_slug)

    if user_cache.retrieve_preferences(new_url_slug):
        processor.populate_catalog(artists, 3, catalog=new_catalog)

    db.save_to_database(None, current_user.id, None, None,
                        new_catalog.id, new_url_slug)

    current_festival = db.get_info_from_database(new_url_slug)
    festivalId = current_festival[0]
    userId = current_festival[2]
    if app.config['IS_ASYNC']:
        db.save_contributor.apply_async(args=(festivalId, userId),
                                        kwargs={'organizer': 1, 'ready': 1})
    else:
        db.save_contributor(festivalId, userId, organizer=1, ready=1)
    app.logger.warning("NEW festival created at '{}'".format(new_url_slug))
    return redirect(url_for('festival', url_slug=new_url_slug))
Esempio n. 3
0
def update_parameters(url_slug):
    '''
    If not the owner, update contributor's parameters on database.
    '''
    _user = session.get('user_id')
    current_festival = db.get_info_from_database(url_slug)
    festivalId = current_festival[0]
    catalog_id = current_festival[5]
    catalog = helpers.Catalog(catalog_id)
    artists = user_cache.retrieve_preferences(url_slug)
    processor = helpers.AsyncAdapter(app)
    processor.populate_catalog(artists, 3, catalog=catalog)

    festivalId = db.get_info_from_database(url_slug)[0]
    h = request.form.get('hotttnesss')
    d = request.form.get('danceability')
    e = request.form.get('energy')
    v = request.form.get('variety')
    a = request.form.get('adventurousness')
    db.update_parameters(festivalId, _user, h, d, e, v, a)
    flash("You've pitched the perfect festival to the organizer." +
          " Now we wait.")
    return redirect(url_for('festival', url_slug=url_slug))
Esempio n. 4
0
def update_parameters(url_slug):
    ''' Get current user, and save their festival parameters
    for to dB.

    Used by contributors after 'proposing vision'.
    '''
    _user = session.get('user_id')
    current_festival = db.get_info_from_database(url_slug)
    festivalId = current_festival[0]
    catalog_id = current_festival[5]
    catalog = helpers.Catalog(catalog_id)
    artists = user_cache.retrieve_preferences(url_slug)

    get_festival = db.get_info_from_database(url_slug)
    festivalId = get_festival[0]
    festival_org = get_festival[2]
    h = request.form.get('hotttnesss')
    d = request.form.get('danceability')
    e = request.form.get('energy')
    v = request.form.get('variety')
    a = request.form.get('adventurousness')
    if _user == festival_org:
        name = request.form.get('name')
        db.update_festival(name, url_slug)
    db.update_parameters(festivalId, _user, h, d, e, v, a)

    if artists:
        processor = helpers.AsyncAdapter(app)
        processor.populate_catalog(artists, 3, catalog=catalog)
        flash_message = ("You've pitched the perfect festival to the organizer." +
                         " Now we wait.")
    else:
        flash_message = "You haven't contributed artists, but your style is pitched!"
    flash(flash_message)
    app.logger.warning("VISION PROPOSED - '{}'' has saved"
                       " parameters at festival '{}'".format(_user, festivalId))
    return redirect(url_for('festival', url_slug=url_slug))
Esempio n. 5
0
def join(url_slug):
    print ("URLSLUG HERE IS {}".format(url_slug))
    current_festival = db.get_info_from_database(url_slug)
    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')
    if organizer != _user:
        try:
            db.save_contributor(current_festival[0], _user)
        except:
            print ("Contributor {} is already in the database.".format(_user))
    else:
        flash("Welcome back to your own festival!")
    return redirect(url_for('festival', url_slug=url_slug))
Esempio n. 6
0
def join(url_slug):
    ''' Adds current logged-in user to festival located at
    url_slug. If user is a contributor, this information is
    saved to dB.
    '''
    current_festival = db.get_info_from_database(url_slug)
    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 '{}' is joining festival '{}'".format(_user, url_slug))
    if organizer != _user:
        try:
            db.save_contributor(current_festival[0], _user)
        except:
            app.logger.warning("Contributor {} is already in the database.".format(_user))
    else:
        flash("Welcome back to your own festival!")
    return redirect(url_for('festival', url_slug=url_slug))
Esempio n. 7
0
def results(url_slug):
    ''' Based on all inputs (parameters, artist preferences) from
    all contributors in a festival, a new playlist is processed and
    generated - leading user to a page with the embeddable Spotify playlist
    '''
    current_festival = db.get_info_from_database(url_slug)
    festival_catalog = current_festival[5]

    if request.method == 'POST':
        # Did user click on join festival ?
        try:
            if request.form['festival_id']:
                app.logger.warning('User selected join (Why is this here?)')
                auth_url = login()
                app.logger.warning("Just 'logged' in by generating festival?")
                user_cache.festival_id = request.form['festival_id']
                return redirect(auth_url)
        except:
            if user_cache.festival_id is None:
                app.logger.warning('User did not click on join and selected parameter')
            else:
                app.logger.warning('User selected parameters')

        if not user_cache.artists:
            flash("You really should add some artists!"
                  " Maybe you can use our suggestions..")
            return redirect(url_for('home'))

        # parameters
        enough_data = True
        name = request.form.get('name')
        h = request.form.get('hotttnesss')
        d = request.form.get('danceability')
        e = request.form.get('energy')
        v = request.form.get('variety')
        a = request.form.get('adventurousness')
        user_cache.did_user_sel_parameters = True
        current_user = load_user(session.get('user_id')).access
        # db.update_parameters(festivalId, _user, h, d, e, v, a)
        s = spotipy.Spotify(auth=current_user)
        user_id = s.me()['id']

        # db.get_average_parameters(user_cache.current_festival)
        processor = helpers.AsyncAdapter(app)
        playlist = helpers.seed_playlist(catalog=festival_catalog, hotttnesss=h,
                                         danceability=d, energy=e, variety=v,
                                         adventurousness=a)
        songs_id = processor.process_spotify_ids(50, 10, s, playlist)

        if user_cache.festival_id is not None and user_cache.did_user_sel_parameters:

            festival_information = db.get_info_from_database(user_cache.festival_id)
            playlist_url = festival_information[4]
            id_playlist = festival_information[3]
            helpers.add_songs_to_playlist(s, user_id, id_playlist, songs_id)
            return render_template('results.html', playlist_url=playlist_url,
                                   enough_data=enough_data)
        else:
            helpers.create_playlist(s, user_id, name)
            id_playlist = helpers.get_id_from_playlist(s, user_id, name)
            helpers.add_songs_to_playlist(s, user_id, id_playlist, songs_id)
            u_id = str(user_id)
            id_pl = str(id_playlist)
            playlist_url = ('https://embed.spotify.com/?uri=spotify:user:'******'{}:playlist:{}'.format(u_id, id_pl))
            if app.config['IS_ASYNC'] is True:
                db.update_festival.apply_async(args=[name, url_slug, id_playlist,
                                               playlist_url])
            else:
                db.update_festival(name, url_slug, id_playlist, playlist_url)
            app.logger.warning("Playlist for festival '{}'"
                               "successfully generated".format(user_cache.festival_id))
            return render_template('results.html', playlist_url=playlist_url,
                                   enough_data=enough_data)
Esempio n. 8
0
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)
Esempio n. 9
0
def results(url_slug):
    current_festival = db.get_info_from_database(url_slug)
    festival_catalog = current_festival[5]

    if request.method == 'POST':
        # Did user click on join festival ?
        try:
            if request.form['festival_id']:
                print 'User selected join'
                auth_url = login()
                user_cache.festival_id = request.form['festival_id']
                return redirect(auth_url)
        except:
            if user_cache.festival_id is None:
                print 'User did not click on join and selected parameter'
            else:
                print 'User selected parameters'

        if not user_cache.artists:
            flash("You really should add some artists!"
                  " Maybe you can use our suggestions..")
            return redirect(url_for('home'))

        # parameters
        enough_data = True
        name = request.form.get('name')
        h = request.form.get('hotttnesss')
        d = request.form.get('danceability')
        e = request.form.get('energy')
        v = request.form.get('variety')
        a = request.form.get('adventurousness')
        user_cache.did_user_sel_parameters = True
        current_user = load_user(session.get('user_id')).access
        # db.update_parameters(festivalId, _user, h, d, e, v, a)
        s = spotipy.Spotify(auth=current_user)
        user_id = s.me()['id']

        # db.get_average_parameters(user_cache.current_festival)
        processor = helpers.AsyncAdapter(app)
        playlist = helpers.seed_playlist(catalog=festival_catalog, hotttnesss=h,
                                         danceability=d, energy=e, variety=v,
                                         adventurousness=a)
        songs_id = processor.process_spotify_ids(50, 10, s, playlist)

        if user_cache.festival_id is not None and user_cache.did_user_sel_parameters:
            '''
            This will need to be above and we will need
            to update the catalog instead of creating one
            or just add this playlist to existent playlist
            '''
            festival_information = db.get_info_from_database(festival_id)
            playlist_url = festival_information[4]
            id_playlist = festival_information[3]
            helpers.add_songs_to_playlist(s, user_id, id_playlist, songs_id)
            return render_template('results.html', playlist_url=playlist_url,
                                   enough_data=enough_data)
        else:
            helpers.create_playlist(s, user_id, name)
            id_playlist = helpers.get_id_from_playlist(s, user_id, name)
            helpers.add_songs_to_playlist(s, user_id, id_playlist, songs_id)
            u_id = str(user_id)
            id_pl = str(id_playlist)
            playlist_url = ('https://embed.spotify.com/?uri=spotify:user:'******'{}:playlist:{}'.format(u_id, id_pl))
            if app.config['IS_ASYNC'] is True:
                db.update_festival.apply_async(args=[name, id_playlist,
                                               playlist_url, url_slug])
            else:
                db.update_festival(name, id_playlist, playlist_url, url_slug)
            return render_template('results.html', playlist_url=playlist_url,
                                   enough_data=enough_data)
Esempio n. 10
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)