def main(): """ Initialize each of the database entries we want. """ # # Flush the database first # print('Flushing database') # execute_from_command_line('flush') print('Adding user jerrysmith') test_user = User.objects.create_user(username='******', password='******', email='*****@*****.**', is_superuser=True, is_staff=True) print('Making dj account for jerrysmith: "DJ Jerry"') test_dj = DJ(user=test_user, name='DJ Jerry', first_name='Jerry', last_name='Smith') test_dj.save() print('Creating show "Earth Radio"') test_show = Show(name='Earth Radio', desc='hmm i like it') test_show.save() print('Adding DJ Jerry as dj of "Earth Radio"') test_show.dj.add(test_dj) print('Creating a playlist on "Earth Radio"', end=' ') test_playlist = Playlist( show=test_show, subtitle='Human Music', desc='Just some music for humans', ) test_playlist.save() print('(id=%d)' % test_playlist.id) # Add some songs and some spins to the playlist print('Adding songs to playlist...') for i, s in enumerate(TEST_SONGS): # Create artist if they don't exist _, _, song, label = get_or_create(s['artist'], s['album'], s['title']) print('\tAdding %s by %s' % (s['title'], s['artist'])) spin = Spin(song=song, playlist=test_playlist, index=i + 1) spin.save()
def add(request, playlist_id): """ Adds the song specified by the provided title, artist, album, and label to the playlist specified by the playlist_id at the position specified by index. """ print('Add received...') print(json.loads(request.body)) args = json.loads(request.body) try: playlist = Playlist.objects.get(pk=playlist_id) spins = Spin.objects.filter(playlist__pk=playlist_id) song_title = args['title'] artist_name = args['artist'] album_name = args['album'] label_name = args['label'] spin_index = spins.count() + 1 except Playlist.DoesNotExist: return error('Invalid URI') except (KeyError, ValueError): return error('Invalid request') if any(arg is '' for arg in (song_title, artist_name, album_name)): return error('Invalid request') # Get the artist, album, and song objects, creating each if necessary artist, album, song, label = get_or_create(artist_name, album_name, song_title, label_name) # Add the medium if it's provided new_spin = Spin(song=song, index=spin_index, playlist=playlist) if 'medium' in request.POST: new_spin.medium = request.POST['medium'] new_spin.save() # Update the playcounts on all our objects song.playcount = F('playcount') + 1 artist.playcount = F('playcount') + 1 album.playcount = F('playcount') + 1 # Hacky, forgive me response = {"ok": True, "spin": spin_to_dict(new_spin)} return JsonResponse(response)
def add_spins(spins, min_id=0): """ Iterate through a list of spins and add them to the proper playlist """ count = 0 total = len(spins) try: for spin_id, spin in spins.items(): # keep count progress_bar(count, total) count += 1 if int(spin_id) < min_id: continue # attempt to salvage classical if spin['album'] is None: spin['album'] = spin['ensemble'] # skip breaks if spin['song'] == "BREAK" or not all( (spin['album'], spin['song'], spin['artist'])): continue # find stuff artist, album, song, _ = get_or_create(spin['artist'], spin['album'], spin['song'], spin['label']) try: playlist = Playlist.objects.get(pk=int(spin['showID'])) except Playlist.DoesNotExist: continue artist.playcount += 1 album.playcount += 1 song.playcount += 1 artist.save() album.save() song.save() index = Spin.objects.filter(playlist=playlist).count() + 1 spin = Spin(song=song, playlist=playlist, index=index) spin.save() except KeyboardInterrupt as e: print('Last spin: %d' % spin_id) raise e
def update(request, playlist_id): """ Updates the specified entry with provided basic spin dict """ print('Update received...') print(json.loads(request.body)) args = json.loads(request.body) try: spin = Spin.objects.get(id=args['id']) except KeyError: return error('Invalid request') except Spin.DoesNotExist: return error('No matching spin') try: label_name = args.get('label', '') artist, album, song, label = get_or_create(args['artist'], args['album'], args['title'], label_name) except (KeyError, ValueError): return error('Invalid request') spin.song = song spin.save() # if 'title' in args: # song = Song.objects.get_or_create(name=args['title'], artist=spin.song.artist, album=spin.song.album) # song.playcount += 1 # if spin.song.playcount <= 1: # spin.song.delete() # spin.update(song=song) # elif 'album' in args: # album = Album.objects.get_or_create(name=args['album']) # elif 'artist' in args: # artist = Artist.objects.get_or_create(name=args['artist']) # elif 'label' in args: # label = Label.objects.get_or_create(name=args['label']) return JsonResponse({'ok': True, 'spin': spin_to_dict(spin)})