def addSongs(toAdd, library): for song in toAdd: newSong = LibraryEntry(library=library, lib_id=song['id'], title=song['title'], artist=song['artist'], album=song['album'], track=song['track'], genre=song['genre'], duration=song['duration'] ) newSong.save()
def addSongs(libJSON, player): for libEntry in libJSON: LibraryEntry(player=player, player_lib_song_id=libEntry['id'], title=libEntry['title'], artist=libEntry['artist'], album=libEntry['album'], genre=libEntry['genre'], track=libEntry['track'], duration=libEntry['duration']).save()
def multiModActivePlaylist(request, user, player_id, activePlayer): if activePlayer.owning_user != user: return HttpResponseForbidden("Only the owner may do that") try: toAdd = json.loads(request.POST['to_add']) toRemove = json.loads(request.POST['to_remove']) except ValueError: return HttpResponseBadRequest( 'Bad JSON\n. Couldn\'t even parse.\n Given data:' + request.raw_post_data) try: #0. lock active playlist activePlayer.lockActivePlaylist() #first, validate all our inputs # 1. Ensure none of the songs that want to be added are already on the playlist alreadyOnPlaylist = getAlreadyOnPlaylist(toAdd, activePlayer) if len(alreadyOnPlaylist) > 0: return HttpResponse(json.dumps(alreadyOnPlaylist), content_type="text/json", status=409) # 2. Ensure none of the songs to be deleted aren't on the playlist notOnPlaylist = getNotOnPlaylist(toRemove, activePlayer) if len(notOnPlaylist) > 0: toReturn = HttpResponse(json.dumps(notOnPlaylist), content_type="text/json", status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn # 3. Ensure all of the songs to be added actually exists in the library notInLibrary = [] for songId in toAdd: if not LibraryEntry.songExsits(songId, activePlayer): notInLibrary.append(songId) if len(notInLibrary) > 0: toReturn = HttpResponse(json.dumps(notInLibrary), content_type="text/json", status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn addSongsToPlaylist(toAdd, activePlayer, user) removeSongsFromPlaylist(toRemove, activePlayer, user) except ValueError: return HttpResponseBadRequest('Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) except TypeError: return HttpResponseBadRequest('Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) return HttpResponse()
def add2ActivePlaylist(request, song_id, library_id, player): player.lockActivePlaylist() if ActivePlaylistEntry.isQueued(song_id, library_id, player): voteSong(player, request.udjuser, song_id, library_id, 1) return HttpResponse() elif ActivePlaylistEntry.isPlaying(song_id, library_id, player): return HttpResponse() if LibraryEntry.songExsitsAndNotBanned(song_id, int(library_id), player): addSongsToPlaylist([{'id' : song_id , 'library_id' : library_id}], player, request.udjuser) else: return HttpResponseMissingResource('song') return HttpResponse(status=201)
def add2ActivePlaylist(user, lib_id, default_library, player): player.lockActivePlaylist() if ActivePlaylistEntry.isQueued(lib_id, default_library, player): voteSong(player, user, lib_id, 1) return HttpResponse() elif ActivePlaylistEntry.isPlaying(lib_id, default_library, player): return HttpResponse() if LibraryEntry.songExsitsAndNotBanned(lib_id, default_library, player): addSongsToPlaylist([lib_id], default_library, player, user) else: toReturn = HttpResponseNotFound() toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn return HttpResponse(status=201)
def multiModActivePlaylist(request, user, player_id, activePlayer): if activePlayer.owning_user != user: return HttpResponseForbidden("Only the owner may do that") try: toAdd = json.loads(request.POST['to_add']) toRemove = json.loads(request.POST['to_remove']) except ValueError: return HttpResponseBadRequest('Bad JSON\n. Couldn\'t even parse.\n Given data:' + request.raw_post_data) try: #0. lock active playlist activePlayer.lockActivePlaylist() #first, validate all our inputs # 1. Ensure none of the songs that want to be added are already on the playlist alreadyOnPlaylist = getAlreadyOnPlaylist(toAdd, activePlayer) if len(alreadyOnPlaylist) > 0: return HttpResponse(json.dumps(alreadyOnPlaylist), content_type="text/json", status=409) # 2. Ensure none of the songs to be deleted aren't on the playlist notOnPlaylist = getNotOnPlaylist(toRemove, activePlayer) if len(notOnPlaylist) > 0: toReturn = HttpResponse(json.dumps(notOnPlaylist), content_type="text/json", status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn # 3. Ensure all of the songs to be added actually exists in the library notInLibrary = [] for songId in toAdd: if not LibraryEntry.songExsits(songId, activePlayer): notInLibrary.append(songId) if len(notInLibrary) > 0: toReturn = HttpResponse(json.dumps(notInLibrary), content_type="text/json", status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn addSongsToPlaylist(toAdd, activePlayer, user) removeSongsFromPlaylist(toRemove, activePlayer, user) except ValueError: return HttpResponseBadRequest('Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) except TypeError: return HttpResponseBadRequest('Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) return HttpResponse()
def multiModActivePlaylist(request, player): player.lockActivePlaylist() try: toAdd = json.loads(request.POST['to_add']) toRemove = json.loads(request.POST['to_remove']) except ValueError: return HttpResponseBadRequest('Bad JSON\n. Couldn\'t even parse.\n Given data:' + request.raw_post_data) user = getUserForTicket(request) #Only admins and owners may remove songs from the playlist if len(toRemove) != 0 and not (player.isAdmin(user) or player.owning_user==user): return HttpResponseForbidden() try: #0. lock active playlist player.lockActivePlaylist() #first, validate/process all our inputs # 1. Ensure none of the songs to be deleted aren't on the playlist notOnPlaylist = getNotOnPlaylist(toRemove, player) if len(notOnPlaylist) > 0: toReturn = HttpJSONResponse(json.dumps(notOnPlaylist), status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn # 2. Ensure all of the songs to be added actually exists in the library notInLibrary = [] for songId in toAdd: if not LibraryEntry.songExsits(songId, player): notInLibrary.append(songId) if len(notInLibrary) > 0: toReturn = HttpJSONResponse(json.dumps(notInLibrary), content_type="text/json", status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn # 3. See if there are any songs that we're trying to add that are already on the playlist # and vote them up instead. alreadyOnPlaylist = getAlreadyOnPlaylist(toAdd, player) toAdd = filter(lambda x: x not in alreadyOnPlaylist, toAdd) try: currentSong = ActivePlaylistEntry.objects.get(song__player=player, state='PL') except ObjectDoesNotExist: currentSong = None for libid in alreadyOnPlaylist: #make sure we don't vote on the currently playing song if currentSong != None and currentSong.song.player_lib_song_id != libid: voteSong(player, user, libid, 1) #alright, should be good to go. Let's actually add/remove songs #Note that we didn't make any actual changes to the DB until we were sure all of our inputs #were good and we weren't going to return an error HttpResponse. This is what allows us to use #the commit on success addSongsToPlaylist(toAdd, player, user) removeSongsFromPlaylist(toRemove, player, user) except ValueError: return HttpResponseBadRequest('Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) except TypeError: return HttpResponseBadRequest('Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) return HttpResponse()
def multiModActivePlaylist(request, player, json_params): """ Code so ugly..... """ player.lockActivePlaylist() toAdd = json_params['to_add'] toRemove = json_params['to_remove'] if len(toAdd) > 0 and not player.user_has_permission('APA', request.udjuser): return HttpResponseForbiddenWithReason('player-permission') if len(toRemove) > 0 and not player.user_has_permission('APR', request.udjuser): return HttpResponseForbiddenWithReason('player-permission') try: #first, validate/process all our inputs # 1. Ensure none of the songs to be deleted aren't on the playlist notOnPlaylist = getNotOnPlaylist(toRemove, player) if len(notOnPlaylist) > 0: toReturn = HttpJSONResponse(json.dumps(notOnPlaylist), status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn # 2. Ensure all of the songs to be added actually exists in the library notInLibrary = filter(lambda x: not LibraryEntry.songExsitsAndNotBanned(x['id'], int(x['library_id']), player), toAdd) if len(notInLibrary) > 0: toReturn = HttpJSONResponse(json.dumps(notInLibrary), content_type="text/json", status=404) toReturn[MISSING_RESOURCE_HEADER] = 'song' return toReturn # 3. See if there are any songs that we're trying to add that are already on the playlist # and vote them up instead. alreadyOnPlaylist = getAlreadyOnPlaylist(toAdd, player) toAdd = filter(lambda x: x not in alreadyOnPlaylist, toAdd) try: currentSong = ActivePlaylistEntry.objects.get(player=player, state='PL') except ObjectDoesNotExist: currentSong = None for song in alreadyOnPlaylist: #make sure we don't vote on the currently playing song if (currentSong != None and not (currentSong.song.lib_id == song['id'] and currentSong.song.library.id == int(song['library_id']))): voteSong(player, request.udjuser, song['id'], int(song['library_id']), 1) #alright, we should be good to go. Let's actually add/remove songs #Note that we didn't make any actual changes to the DB until we were sure all of our inputs #were good and we weren't going to return an error HttpResponse. This is what allows us to use #the commit on success addSongsToPlaylist(toAdd, player, request.udjuser) removeSongsFromPlaylist(toRemove, player, request.udjuser) except ValueError: return HttpResponseBadRequest('Value Error. Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) except TypeError as te: return HttpResponseBadRequest('Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove) + str(te)) except KeyError: return HttpResponseBadRequest('KeyError Bad JSON\n' + 'toAdd: ' + str(toAdd) + '\ntoRemove: ' + str(toRemove)) return HttpResponse()
def getNonExistantLibIds(songIds, library): return filter(lambda x: not LibraryEntry.songExists(x, library.id), songIds)
def getNonExistantLibIds(songIds, player): return filter(lambda x: not LibraryEntry.songExists(x, player.DefaultLibrary, player), songIds)