def json_email_song_link(request): form = request.POST email_address = form.get('email', '') if not re.match("^[-_a-zA-Z0-9.]+@[-_a-zA-Z0-9.]+$", email_address): return json_response.json_error("Invalid email address.") songs = get_song_list(form) if songs: message = ["From: Audio Enclave <%s>\r\n" % settings.DEFAULT_FROM_EMAIL, "To: %s\r\n\r\n" % email_address, "Someone sent you a link to the following "] if len(songs) == 1: message.append("song:\n\n") subject = songs[0].title else: message.append("songs:\n\n") subject = "%d songs" % len(songs) for song in songs: message.extend((song.title, "\n", song.artist, "\n", song.album, "\n", settings.HOST_NAME + song.get_absolute_url(), "\n\n")) # Ship it! send_mail("Link to " + subject, "".join(message), settings.DEFAULT_FROM_EMAIL, (email_address,)) msg = "An email has been sent to %s." % email_address return json_response.json_success(msg) else: return json_response.json_error("No matching songs were found.")
def bad_recommendations(request): form = request.REQUEST # get songs that were marked as bad bad_songs = utils.get_song_list(form, key='bad_songs') # get the original set of songs that were queued original_songs = utils.get_song_list(form, key='original_songs') # find the clusters that the two song lists have in common # and decrease their weights accordingly. related_clusters = [] for song in original_songs: related_clusters += models.Cluster.objects.filter(songs=song) # now keep only those clusters that contain some song in bad_songs.... bad_related_clusters = [] for rc in related_clusters: for song in bad_songs: if song in rc.songs.all(): bad_related_clusters.append(rc) break # we've already counted it more than once if necessary # WOW, THAT WAS A REALLY DUMB WAY TO DO IT. # TODO FIX THIS. # also TODO make this separate because we have to do this same thing # when boosting songs that did well. for brc in bad_related_clusters: brc.weight = brc.weight * 0.5 brc.save() # record the data bad_rec_entry = models.BadRecs(bad_recs=len(bad_songs)) bad_rec_entry.save() # Songs get removed from the table on the page. return json_response.json_success("Hooray!")
def good_recommendations(request): form = request.REQUEST good_songs = utils.get_song_list(form) original_songs = utils.get_song_list(form, key='original_songs') related_clusters = [] for song in original_songs: related_clusters += models.Cluster.objects.filter(songs=song) good_related_clusters = [] for rc in related_clusters: for song in good_songs: if song in rc.songs.all(): good_related_clusters.append(rc) break # we've already counted it more than once if necessary for grc in good_related_clusters: grc.weight = brc.weight * 2 grc.save() # record data good_rec_entry = models.GoodRecs(good_recs=len(good_songs)) good_rec_entry.save() return json_response.json_success("Yay!!!") # mostly for consistency.
def edit_playlist(request, playlist_id): # Get the playlist. form = request.POST try: playlist = Playlist.objects.get(pk=playlist_id) except Playlist.DoesNotExist: return json_response.json_error('That playlist does not exist.') # Check that they can edit it. if not playlist.can_edit(request.user): return json_response.json_error('You are not authorized to edit this' ' playlist.') songs = get_song_list(form) if songs: playlist.set_songs(songs) playlist.save() return json_response.json_success('Successfully edited "%s".' % playlist.name)
def favorite_song(request, song_id): if not request.user.is_authenticated(): return json_response.json_error('User not authenticated.') favorited = request.POST['favorited'] == 'true' try: song = Song.objects.get(pk=int(song_id)) except Song.DoesNotExist: raise Http404 pl = Playlist.get_favorites(request.user, create=True) try: fav = PlaylistEntry.objects.get(song=song, playlist=pl) except PlaylistEntry.DoesNotExist: fav = None if favorited and not fav: pl.append_songs([song]) elif not favorited and fav: fav.delete() return json_response.json_success("%s favorited: %r" % (song_id, favorited))
def favorite_song(request, song_id): if not request.user.is_authenticated(): return json_response.json_error("User not authenticated.") favorited = request.POST["favorited"] == "true" try: song = Song.objects.get(pk=int(song_id)) except Song.DoesNotExist: raise Http404 pl = Playlist.get_favorites(request.user, create=True) try: fav = PlaylistEntry.objects.get(song=song, playlist=pl) except PlaylistEntry.DoesNotExist: fav = None if favorited and not fav: pl.append_songs([song]) elif not favorited and fav: fav.delete() return json_response.json_success("%s favorited: %r" % (song_id, favorited))
def json_email_song_link(request): form = request.POST email_address = form.get("email", "") if not re.match("^[-_a-zA-Z0-9.]+@[-_a-zA-Z0-9.]+$", email_address): return json_response.json_error("Invalid email address.") songs = get_song_list(form) if songs: message = [ "From: Audio Enclave <%s>\r\n" % settings.DEFAULT_FROM_EMAIL, "To: %s\r\n\r\n" % email_address, "Someone sent you a link to the following ", ] if len(songs) == 1: message.append("song:\n\n") subject = songs[0].title else: message.append("songs:\n\n") subject = "%d songs" % len(songs) for song in songs: message.extend( ( song.title, "\n", song.artist, "\n", song.album, "\n", settings.HOST_NAME + song.get_absolute_url(), "\n\n", ) ) # Ship it! send_mail("Link to " + subject, "".join(message), settings.DEFAULT_FROM_EMAIL, (email_address,)) msg = "An email has been sent to %s." % email_address return json_response.json_success(msg) else: return json_response.json_error("No matching songs were found.")