Example #1
0
def playlist(user_id):
    if request.method == 'GET':
        playlists = Playlist.objects().filter(user_id=user_id)
        return playlists.to_json()
    if request.method == 'POST':
        playlist = Playlist()
        playlist.user_id = user_id
        playlist.name = 'New Playlist'
        playlist.song_ids = []
        playlist.save()
        return playlist.to_json()
Example #2
0
	def post(self):
		""" Create a new playlist. """
		
		listName = self.request.get('name')
		
		if not listName:
			errorOut(self.response, 400)
			return
		
		user = users.get_current_user()
		
		if not user:
			errorOut(self.response, 401)
			return
		
		# Generate a unique ID for the new list.
		listId = ''
		while True:
			listId = uuid4().hex
			if not Playlist.gql('WHERE playlistId = :1', listId).get():
				break
		
		# Create and store the playlist.
		newList = Playlist()
		newList.playlistId = listId
		newList.name = listName
		newList.put()
		
		# Identify the user as the owner of that playlist.
		playlistUserMap = PlaylistUser()
		playlistUserMap.playlistId = listId
		playlistUserMap.user = user
		playlistUserMap.isOwner = True
		playlistUserMap.put()
		
		# Output the playlist's metadata as JSON.
		self.response.headers['Content-Type'] = 'application/json'
		self.response.out.write(json.dumps({
			'playlistId': listId,
			'name': listName
		}))