def add_video_to_playlist(playlist_id, video_id): """add a video to the end of playlist arguments: playlist_id -- the id of the playlist video_id -- the id of the video """ try: playlist = PlayList.findPlaylistById(playlist_id) if playlist is None: raise DataLayerException({ 'code': 404, 'message': 'No play list found' }) video = Video.findVideoById(video_id) if video is None: raise DataLayerException({ 'code': 404, 'message': 'No video found' }) playlist.addVideo(video) except ValueError as e: response.status = 400 return json.dumps({'error': 'Bad Request'}) except DataLayerException as e: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': 'OK'})
def delete_playlist(id): """delete a playlist with id arguments: id -- the id of the playlist""" try: playlist = PlayList.findPlaylistById(id) if playlist is None: raise DataLayerException({ 'code': 404, 'message': 'No play list found' }) playlist.delete() except ValueError as e: response.status = 400 return json.dumps({'error': 'Bad Request'}) except DataLayerException as e: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': 'OK'})
def get_playlist_videos(id): """get the list of videos in playlist arguments: id -- the playlist id return { "data": [ video list ] } """ try: playlist = PlayList.findPlaylistById(id) if playlist is None: raise DataLayerException({ 'code': 404, 'message': 'No play list found' }) videos = playlist.getVideos() except DataLayerException as e: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': map(lambda v: v.toJSONSerializable(), videos)})
def update_playlist(id): """update a playlist, it accept an json object as the body: arguments: id -- the playlist id requset body: { "name": "the new playlist name" } """ try: try: data = request.json except: raise ValueError if data is None: raise ValueError if data['name'] is None: raise ValueError playlist = PlayList.findPlaylistById(id) if playlist is None: raise DataLayerException({ 'code': 404, 'message': 'No play list found' }) playlist.name = data['name'] playlist.save() except ValueError as e: response.status = 400 return json.dumps({'error': 'Bad Request'}) except DataLayerException as e: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': playlist.toJSONSerializable()})
def get_playlist_info(id): """get the playlist information arguments: id -- the playlist id return { "data": { playlist object } }""" try: playlist = PlayList.findPlaylistById(id) if playlist is None: raise DataLayerException({ 'code': 404, 'message': 'No play list found' }) except DataLayerException as e: response.status = e.errorArgs['code'] return json.dumps({'error': e.errorArgs['message']}) response.headers['Content-Type'] = 'application/json' return json.dumps({'data': playlist.toJSONSerializable()})