def post(self, party_id): # Export the specified party to Spotify. If the export_playlist field # from the database is populated and still exists in Spotify, just # update this playlist. Otherwise, create a new playlist. client = recreate_client_from_session() user_id = get_user_id() if not user_id or not client: abort(403, "User is not logged in to Spotify.") if database.check_party_exists(party_id): if database.is_user_party_host(user_id, party_id): # Okay, we're authenticated and everything exists. # Retrieve party details from database. details = database.get_party(party_id) if not details: return { "message": "Unable to get information about the party from database!", "success": False } prev_playlist = database.get_party_exported_playlist(party_id) if prev_playlist and prev_playlist != "none": old_playlist_id = prev_playlist.split(":")[4] result = client.api.me_unfollow_playlist( user_id, old_playlist_id) # Create a new playlist using name and description from database. playlist = client.api.user_playlist_create( user_id, details["meta"]["party_name"], description=details["meta"]["party_description"]) playlist_id = playlist["id"] database.update_party_exported_playlist( party_id, "spotify:user:{}:playlist:{}".format(user_id, playlist_id)) # Sort and reorder song_uris by song_votes, descending. song_votes = [song["votes"] for song in details["songs"]] song_uris = [ "spotify:track:{}".format(song["song_id"]) for song in details["songs"] ] sorted_idx = sorted(range(len(song_votes)), key=lambda k: song_votes[k], reverse=True) sorted_uris = [song_uris[i] for i in sorted_idx] # Add songs from database to the new playlist. client.api.user_playlist_tracks_add(user_id, playlist_id, sorted_uris) return { "message": "Successfully exported party to Spotify.", "success": True } else: abort(403, "User is not party host.") else: abort(404, "Party does not exist.")
def jukebox_create_party(): user_id = get_user_id() # We are *creating* a new party. if user_id: return render_template("jukebox_create_party.html", context=get_jinja_context()) else: # We don't have a valid token stored. User is not authorized. # Redirect to Spotify for authorization. return redirect(url_for("welcome"))
def jukebox_user_account(): # We are displaying the user's account details. user_id = get_user_id() if user_id: additional_context = { "user_parties": database.get_user_parties(user_id) } return render_template("jukebox_user_account.html", context=get_jinja_context(additional_context)) else: return redirect("welcome")
def delete(self, party_id): """Delete a party and remove it from the database. This action requires that the logged in user is also the party host. """ client = recreate_client_from_session() user_id = get_user_id() if client and user_id: if database.is_user_party_host(user_id, party_id): database.delete_party(party_id) return { "message": "Successfully deleted party {}.".format(party_id), "success": True } else: abort(403, "User is not the party host.") else: abort(403, "User is not logged in to Spotify.")
def post(self): user_id = get_user_id() party_details = request.get_json() if user_id: msg = "" party_id = party_details["party_id"] if "name" in party_details: new_name = party_details["name"] database.update_party_name(party_id, new_name) msg += "Updated party name to '{}'. ".format(new_name) if "description" in party_details: new_description = party_details["description"] database.update_party_description(party_id, new_description) msg += "Updated party description to '{}'. ".format( new_description) if "name" not in party_details and "description" not in party_details: abort(400, "Bad data payload.") return {"message": msg, "success": True} else: abort(403, "User has not logged in to Spotify.")
def post(self): """ Create a new party. User must be authenticated and have a stored session. """ client = recreate_client_from_session() user_id = get_user_id() party_details = request.get_json() if client and user_id: # Create the playlist. party_name = party_details["name"] party_description = party_details["description"] party_starter_playlist = party_details["playlist"] ## Get the tracklist in URIs for the submitted starter playlist. # First, get the URI and parse out the playlist ID. The ID is the # last bit after ``playlist:``: # # spotify:user:spotify:playlist:37i9dQZF1DX4JAvHpjipBk # ^ user_id ^ playlist ID if party_starter_playlist: starter_playlist_user_id = party_starter_playlist.split(":")[2] starter_playlist_id = party_starter_playlist.split(":")[4] # Now, retrieve the playlist's tracks from Spotify's API. received_data = client.api.user_playlist_tracks( starter_playlist_user_id, starter_playlist_id) tracks = [item["track"] for item in received_data["items"]] else: tracks = [] # Add the party and itself details into the database. party_id = generate_party_id() database.create_party(user_id, party_id, party_name, party_description, party_starter_playlist, tracks) return { "party_id": party_id, "message": "Created new party: {}".format(party_details["name"]) } else: abort(403, "User has not logged in to Spotify.")
def jukebox_view_party(party_id): user_id = get_user_id() # get user_id from session, if exists if party_id and database.check_party_exists(party_id): # We are *viewing* an existing party. Check if the current user is # logged in, and if so, if they are the party host. party_details = database.get_party(party_id) highlight = request.args.get("highlight") additional_context = { "party_id": party_id, "party_name": party_details["meta"]["party_name"], "party_description": party_details["meta"]["party_description"], "tracks": party_details["songs"], "is_user": user_id is not None, "is_party_host": database.is_user_party_host(user_id, party_id), "highlight": highlight } return render_template("jukebox_view_party.html", context=get_jinja_context(additional_context)) else: return render_template("jukebox_party_does_not_exist.html", context=get_jinja_context())
def post(self, party_id, song_uri): client = recreate_client_from_session() user_id = get_user_id() if user_id: try: song_id = song_uri.split(":")[2] except: abort(400, "Invalid song URI.") track = client.api.track(song_id) if database.add_song_to_party(party_id, song_id, track): return { "message": "Added song {} to party {}.".format(song_id, party_id), "success": True } else: return { "message": "Specified party does not exist.", "success": False } else: abort(403, "User has not logged in to Spotify.")
def welcome(): user_id = get_user_id() if user_id: return redirect(url_for("jukebox_user_account")) else: return render_template("welcome.html", context=get_jinja_context())