def playlist_delete(id, user=None): playlist = Playlist.query.filter_by(playlist_id=id, owner_id=user.id).first() if not playlist: return abort(400) db.session.delete(playlist) db.session.commit() return jsonify(playlist_schema.dump(playlist))
def playlist_update(id, user=None): #Update a playlist playlist_fields = playlist_schema.load(request.json) playlists = Playlist.query.filter_by(playlist_id=id, owner_id=user.id) if playlists.count() != 1: return abort(401, description="Unauthorised to update this playlist") playlists.update(playlist_fields) db.session.commit() return jsonify(playlist_schema.dump(playlists[0]))
def playlist_create(user=None): #Create a new playlist playlist_fields = playlist_schema.load(request.json) new_playlist = Playlist() new_playlist.name = playlist_fields["name"] new_playlist.description = playlist_fields["description"] new_playlist.collaborative = playlist_fields["collaborative"] new_playlist.public = playlist_fields["public"] user.playlists.append(new_playlist) db.session.commit() return jsonify(playlist_schema.dump(new_playlist))
def playlist_delete(id): #Delete a playlist user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") playlist = Playlist.query.filter_by(id=id, user_id=user.id).first() if not playlist: return abort(400) db.session.delete(playlist) db.session.commit() return jsonify(playlist_schema.dump(playlist))
def playlist_update(id): #Update a playlist playlist_fields = playlist_schema.load(request.json) user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") playlists = Playlist.query.filter_by(id=id) if playlists.count() != 1: return abort(401, description="Unauthorized to update this book") playlists.update(playlist_fields) db.session.commit() return jsonify(playlist_schema.dump(playlists[0]))
def playlist_create(): #Create a new playlist playlist_fields = playlist_schema.load(request.json) user_id = get_jwt_identity() user = User.query.get(user_id) if not user: return abort(401, description="Invalid user") new_playlist = Playlist() new_playlist.playlist_title = playlist_fields["playlist_title"] user.playlists.append(new_playlist) db.session.add(new_playlist) db.session.commit() return jsonify(playlist_schema.dump(new_playlist))
def playlist_show(id): #Return a single playlist playlist = Playlist.query.get(id) return jsonify(playlist_schema.dump(playlist))