def listen(long_id): # Get the round from the long id round = Round.query.filter_by(long_id=long_id).first() # Make sure this is the correct handler for the round's current status if round.status != RoundStatus.listen: raise MusicrecsAlert(f"This round is currently in the {round.status.name} phase...", redirect_location=url_for(f'round.{round.status.name}', long_id=round.long_id)) # Prepare the playlist form if round.music_type == MusicType.album: playlist_form = None elif round.music_type == MusicType.track: playlist_form = PlaylistForm() else: raise MusicrecsError(f"Unknown music type {round.music_type}") # Get the playlist if it's already been created playlist = None if round.playlist_link: playlist = spotify_iface.get_playlist_from_link(round.playlist_link) # If the playlist form has been submitted, then create the playlist and # reload the round elif playlist_form and playlist_form.submit_playlist.data and playlist_form.validate(): create_playlist(long_id, playlist_form.name.data) return redirect(url_for('round.listen', long_id=long_id)) # Notify the user if there were errors elif playlist_form and playlist_form.errors: flash("There were errors in your rec submission", "warning") # Process guess submissions guess_form = GuessForm(round) guess_form.name.choices.extend(list(set(get_user_names(round)) - set(get_guesser_names(round)))) if guess_form.submit_guess.data and guess_form.validate(): # Add the user's guesses within the form to the database process_guess_form(round, guess_form) # Alert the user that the guess was submitted successfully flash("Successfully submitted your guess", "success") return redirect(url_for('round.listen', long_id=round.long_id)) elif guess_form.errors: flash("There were errors in your guess", "warning") else: # Prefill the usernames, with the appropriate number of rows guess_form.guess_field.render_kw["rows"] = len(round.submissions) guess_form.guess_field.data = "\n".join([f"{submission.user_name}: " for submission in round.submissions]) return render_template('round/listen_phase.html', round_link=get_abs_round_link(round), round=round, music_submissions=get_shuffled_music_submissions(round), playlist_form=playlist_form, playlist=playlist, guess_form=guess_form)
def index(long_id): # Get the round from the long id round = Round.query.filter_by(long_id=long_id).first() if round.status == RoundStatus.submit: return redirect(url_for('round.submit', long_id=round.long_id)) elif round.status == RoundStatus.listen: return redirect(url_for('round.listen', long_id=round.long_id)) elif round.status == RoundStatus.revealed: return redirect(url_for('round.revealed', long_id=round.long_id)) else: raise MusicrecsError(f"Unknown round status {round.status}")
def get_snoozin_rec(round: Round) -> SpotifyMusic: """Get a random or similar music recommendation. - Get random music recommendations by searching a random 1 or 2 word phrase in spotify - Get similar music recommendations by using spotify's recommendation api """ snoozin_rec = None if round.snoozin_rec_type == SnoozinRecType.random: rw_gen = random_words.RandomWords() num_words = random.randint(1, 2) search_term = "" num_attempts = 0 while snoozin_rec is None: if num_attempts > MAX_SNOOZIN_REC_SEARCH_ATTEMPTS: raise MusicrecsAlert("We're having trouble getting a rec from snoozin...", redirect_location=url_for("round.index", long_id=round.long_id)) search_term = " ".join( rw_gen.get_random_words(num_words) ) search_results = spotify_iface.search_for_music( round.music_type, search_term, num_results=1, popularity_threshold=15) if len(search_results): snoozin_rec = search_results[0] num_attempts += 1 # Set the round's search term round.snoozin_rec_search_term = search_term db.session.commit() elif round.snoozin_rec_type == SnoozinRecType.similar: snoozin_rec = spotify_iface.recommend_music(round.music_type, _get_music_list(round)) else: raise MusicrecsError(f"Unknown Snoozin rec type {round.snoozin_rec_type}") return snoozin_rec
def submit(long_id): # Get the round from the long id round = Round.query.filter_by(long_id=long_id).first() # Make sure this is the correct handler for the round's current status if round.status != RoundStatus.submit: raise MusicrecsAlert(f"This round is currently in the {round.status.name} phase...", redirect_location=url_for(f'round.{round.status.name}', long_id=round.long_id)) # Get data from the rec form if round.music_type == MusicType.album: rec_form = AlbumrecForm(round) elif round.music_type == MusicType.track: rec_form = TrackrecForm(round) else: raise MusicrecsError(f"Unknown music type {round.music_type}") # Process the submission form if rec_form.validate_on_submit(): # Add the submission to the database add_submission_to_db(round.id, rec_form.name.data, rec_form.spotify_link.data) # Alert the user that the form was successfully submitted flash("Successfully submitted your recommendation: " f"{spotify_iface.get_music_from_link(round.music_type, rec_form.spotify_link.data)}", "success") # Redirect back to the round after successful submission return redirect(url_for('round.submit', long_id=long_id)) elif rec_form.errors: flash("There were errors in your rec submission", "warning") return render_template('round/submit_phase.html', round_link=get_abs_round_link(round), rec_form=rec_form, round=round)
def validate_playlist_link(self, key, playlist_link): if playlist_link is not None and len(playlist_link) > MAX_SPOTIFY_LINK_LENGTH: raise MusicrecsError(f"playlist link greater than storage limit of {MAX_SPOTIFY_LINK_LENGTH} characters.") return playlist_link
def validate_long_id(self, key, long_id): if len(long_id) > MAX_LONG_ID_LENGTH: raise MusicrecsError(f"long id greater than storage limit of {MAX_LONG_ID_LENGTH} characters.") return long_id
def validate_user_name(self, key, user_name): if len(user_name) > MAX_USERNAME_LENGTH: raise MusicrecsError(f"User name greater than storage limit of {MAX_USERNAME_LENGTH} characters.") return user_name
def validate_spotify_link(self, key, spotify_link): if len(spotify_link) > MAX_SPOTIFY_LINK_LENGTH: raise MusicrecsError(f"Spotify link greater than storage limit of {MAX_SPOTIFY_LINK_LENGTH} characters.") return spotify_link