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 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 submit_delete_requests(request): form = request.POST # Add the songs and redirect to the detail page for this playlist. message = 'The following delete request(s) were filed' if request.user.is_authenticated(): subject = 'Delete Request from ' + request.user.username message += ' by ' + request.user.username + ':\n' else: subject = 'Delete Request from Anonymous' message += ' by an unregistered user:\n' song_list = [] songs = get_song_list(form) for song in songs: song_string = (' * %(id)s - %(artist)s - %(album)s - %(title)s\n' % { 'id': str(song.id), 'artist': song.artist, 'album': song.album, 'title': song.title }) message += song_string song_list.append(song) uri = "%s?ids=%s" % (request.build_absolute_uri( reverse("aenclave-list")), '+'.join([str(song.id) for song in songs])) message += '\nView these songs here: %s\n' % uri mail_admins(subject, message, False) return render_html_template('aenclave/delete_requested.html', request, {'song_list': song_list}, context_instance=RequestContext(request))
def delete_songs(request): form = request.POST # The person must be authenticated if not request.user.is_authenticated(): raise Http404() if not request.user.is_staff: return submit_delete_requests(request) subject = 'Song Deletion by ' + request.user.username message = 'The following files were deleted by ' + request.user.username + ':\n' song_list = [] songs = get_song_list(form) for song in songs: song_string = (' * %(id)s - %(artist)s - %(album)s - %(title)s\n' % { 'id': str(song.id), 'artist': song.artist, 'album': song.album, 'title': song.title }) message += song_string song_list.append(song) try: mail_admins(subject, message, False) except Exception, e: logging.error("Could not send deletion mail: %s" % e)
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 submit_delete_requests(request): form = request.POST # Add the songs and redirect to the detail page for this playlist. message = 'The following delete request(s) were filed' if request.user.is_authenticated(): subject = 'Delete Request from ' + request.user.username message += ' by ' + request.user.username + ':\n' else: subject = 'Delete Request from Anonymous' message += ' by an unregistered user:\n' song_list = [] songs = get_song_list(form) for song in songs: song_string = (' * %(id)s - %(artist)s - %(album)s - %(title)s\n' % {'id': str(song.id), 'artist': song.artist, 'album': song.album, 'title': song.title}) message += song_string song_list.append(song) uri = "%s?ids=%s" % (request.build_absolute_uri(reverse("aenclave-list")), '+'.join([str(song.id) for song in songs])) message += '\nView these songs here: %s\n' % uri mail_admins(subject,message,False) return render_html_template('aenclave/delete_requested.html', request, {'song_list': song_list}, context_instance=RequestContext(request))
def delete_songs(request): form = request.POST # The person must be authenticated if not request.user.is_authenticated(): raise Http404() if not request.user.is_staff: return submit_delete_requests(request) subject = 'Song Deletion by ' + request.user.username message = 'The following files were deleted by ' + request.user.username + ':\n' song_list = [] songs = get_song_list(form) for song in songs: song_string = (' * %(id)s - %(artist)s - %(album)s - %(title)s\n' % {'id': str(song.id), 'artist': song.artist, 'album': song.album, 'title': song.title}) message += song_string song_list.append(song) try: mail_admins(subject,message,False) except Exception, e: logging.error("Could not send deletion mail: %s" % e)
def xml_queue(request): form = request.POST # Get the selected songs. songs = get_song_list(form) # Queue the songs. channel = Channel.default() ctrl = channel.controller() try: ctrl.add_songs(songs) except ControlError, err: return xml_error(str(err)) else: return simple_xml_response('success')
def dl(request): """Serve songs to the user, either as a zip archive or a single file.""" # Use REQUEST to allow GET and POST selections. songs = get_song_list(request.REQUEST) if not songs: # TODO(rnk): Better error handling. raise Exception("No ids were provided to dl.") elif len(songs) == 1: return send_song(songs[0]) else: return send_songs(songs)
def xml_queue(request): form = request.POST # Get the selected songs. songs = get_song_list(form) # Queue the songs. channel = Channel.default() ctrl = channel.controller() try: ctrl.add_songs(songs) except ControlError, err: return xml_error(str(err))
def remove_from_playlist(request): # Get the playlist to be removed from. form = request.POST try: playlist = Playlist.objects.get(pk=get_integer(form, 'pid')) except Playlist.DoesNotExist: return html_error(request, 'That playlist does not exist.', 'Remove Songs') # Make sure the user is allowed to edit this playlist. if not playlist.can_edit(request.user): return html_error(request, 'You lack permission to edit this' ' playlist.', 'Remove Songs') # Remove the songs and redirect to the detail page for this playlist. songs = get_song_list(form) PlaylistEntry.objects.filter(song__in=songs).delete() return HttpResponseRedirect(playlist.get_absolute_url())
def add_to_playlist(request): # Get the playlist to be added to. form = request.POST try: playlist = Playlist.objects.get(pk=get_integer(form, 'pid')) except Playlist.DoesNotExist: return html_error(request, 'That playlist does not exist.', 'Add Songs') # Make sure the user is allowed to edit this playlist. if not playlist.can_edit(request.user): return html_error(request, 'You lack permission to edit this' ' playlist.', 'Add Songs') # Add the songs and redirect to the detail page for this playlist. songs = get_song_list(form) playlist.append_songs(songs) return HttpResponseRedirect(playlist.get_absolute_url())
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 queue_songs(request): form = request.REQUEST # Get the selected songs. songs = get_song_list(form) # Queue the songs. channel = Channel.default() ctrl = channel.controller() referrer = request.META.get('HTTP_REFERER', '') referrer_path = urlparse.urlparse(referrer).path if 'recommendations' in referrer_path: good_recommendations(request) try: ctrl.add_songs(songs) except ControlError, err: if 'getupdate' in form: return json_error(str(err)) else: return html_error(request, str(err))
def queue_to_front(request): form = request.REQUEST songs = get_song_list(form) if len(songs) != 1: raise json_error("Can only queue one song to front") song = songs[0] channel = Channel.default() ctrl = channel.controller() try: ctrl.queue_to_front(song) except ControlError, err: if 'getupdate' in form: return json_error(str(err)) else: return html_error(request, str(err))
def create_playlist(request): form = request.POST name = get_unicode(form, 'name') if not name: return html_error(request, 'No name provided.') # TODO better feedback # Make sure that we can create the playlist. # WTF In fact, we can't use playlist.songs until playlist has been saved. playlist = Playlist(name=name, owner=request.user) try: playlist.save() # BTW This will fail if (name,owner) is not unique. except: return html_error(request, 'A playlist of that name already exists.') # return error(request,'Nonunique name/owner.') # TODO better feedback # Add the specified songs to the playlist. songs = get_song_list(form) playlist.set_songs(songs) playlist.save() # Redirect to the detail page for the newly created playlist. return HttpResponseRedirect(playlist.get_absolute_url())
def create_playlist(request): form = request.POST name = get_unicode(form, 'name') if not name: return html_error(request,'No name provided.') # TODO better feedback # Make sure that we can create the playlist. # WTF In fact, we can't use playlist.songs until playlist has been saved. playlist = Playlist(name=name, owner=request.user) try: playlist.save() # BTW This will fail if (name,owner) is not unique. except: return html_error(request, 'A playlist of that name already exists.') # return error(request,'Nonunique name/owner.') # TODO better feedback # Add the specified songs to the playlist. songs = get_song_list(form) playlist.set_songs(songs) playlist.save() # Redirect to the detail page for the newly created playlist. return HttpResponseRedirect(playlist.get_absolute_url())
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 list_songs(request): songs = get_song_list(request.REQUEST) songs = Song.annotate_favorited(songs, request.user) return render_html_template('aenclave/list_songs.html', request, {'song_list': songs}, context_instance=RequestContext(request))