def save_session_playhistory(request,user): # @TODO save playlists here playhistory = request.session.get('playhistory',[]) for song in playhistory: try: lastfm_track_song = LastFMTrackSong.objects.get(id=song['lastfm_track_song_id']) except LastFMTrackSong.DoesNotExist: continue song_played = PlayHistory(musiphile=user,song=lastfm_track_song) song_played.save() playlist = request.session.get('playlist', []) if playlist: pls = Playlist(creator=user) pls.save() pls_user = PlaylistUser(user=user, playlist=pls) pls_user.save() for song in playlist: try: pls_song = PlaylistSong.objects.get(lastfm_track=song.id, playlist=pls) except PlaylistSong.DoesNotExist: pls_song = PlaylistSong(lastfm_track=song, playlist=pls) pls_song.save()
def create(request): musiphile = Musiphile.objects.from_request(request) if not musiphile: return HttpResponse(json.dumps({'status':'failed','message':'User is not a musiphile'}), content_type="application/json") pls = Playlist(creator=musiphile) pls.save() pls_user = PlaylistUser(user=musiphile,playlist=pls) pls_user.save() return HttpResponse(json.dumps({'status':'ok','playlist_id':pls.id}), content_type="application/json")
def add(request): if request.method!="POST": return ResponseNotAllowed(['POST']) lastfm_track_id = request.POST.get('lastfm_track',None) if not lastfm_track_id: return ResponseBadRequest('Required lastfm_track_song_id was not specified.') try: lastfm_track = Track.objects.get(id=lastfm_track_id) except Track.DoesNotExist: return ResponseBadRequest('Could not find track with id : %s' % lastfm_track_id) playlist = None playlist_id = request.POST.get('playlist', None) if playlist_id: try: playlist = Playlist.objects.get(id=playlist_id) except Playlist.DoesNotExist: return ResponseBadRequest('Could not find playlist with id : %s' % playlist_id) if request.user.is_authenticated(): musiphile = Musiphile.objects.get(id=request.user.id) # if no playlist was specified, use the first one found if not playlist: # create a new one for this user and add the song to that playlist playlist = Playlist(creator=musiphile) playlist.save() playlist_user = PlaylistUser(playlist=playlist,user=musiphile) playlist_user.save() # ensure the song has not already been added to the playlist try: playlist_song = PlaylistSong.objects.get(lastfm_track=lastfm_track,playlist=playlist) except PlaylistSong.DoesNotExist: # add song to playlist playlist_song = PlaylistSong(lastfm_track=lastfm_track,playlist=playlist) playlist_song.save() else: playlist = request.session.get('playlist',[]) was_added=False for item in playlist: if item==lastfm_track.id: was_added=True break if not was_added: playlist.append(lastfm_track) request.session['playlist'] = playlist return HttpResponse(json.dumps({'status':'ok'}), content_type="application/json")
def get(request, slug_name): if slug_name=='anonymous': playlist = Playlist() playlist.name = 'untitled playlist' tracks = request.session.get('playlist',[]) else: try: playlist = Playlist.objects.get(slug_name=slug_name) except Playlist.DoesNotExist: raise Http404() tracks = playlist.lastfm_track.all() return direct_to_template(request, 'includes/playlist.html', {'playlist_id':'playlist_%s' % slug_name, 'playlist_title':playlist.name, 'lastfm_tracks':tracks})