def assert_user_owns_playlist(self, playlist_id): """ throws an exception if the playlist is not owned by the current "logged in" i.e. the user is self.name """ self.assert_playlist_exists(playlist_id) if not playlist_id in self.playlist_ids(): raise APIException("playlist not valid for user")
def assert_playlist_viewable(self, playlist_id): """ throws an exception if the playlist is not owned by the current "logged in" i.e. the user is self.name and if the playlist is not shared """ self.assert_playlist_exists(playlist_id) record = self.db.shared.find_one(playlist_id=playlist_id, shared_to_user_id=self.user_id( self.name)) if not record and not playlist_id in self.playlist_ids(): raise APIException("playlist not valid for user")
def playlist_share(self, playlist_id, share_with): """share a playlist with another user(by name)""" self.assert_user_exists(share_with) self.assert_user_owns_playlist(playlist_id) user_id = self.user_id(self.name) shared_with_id = self.user_id(share_with) if user_id == shared_with_id: raise APIException("cannot share a playlist with yourself") self.db.shared.upsert( { 'playlist_id': playlist_id, "shared_to_user_id": shared_with_id }, ['playlist_id', 'shared_to_user_id'])
def assert_user_exists(self, name): """throw an exception if the user does not exist""" if not self.user_id(name): raise APIException("user does not exist")
def assert_playlist_exists(self, playlist_id): """throws an exception if the playlist exists""" if not self.db.playlist.find_one(id=playlist_id): raise APIException( "playlist id {} does not exist".format(playlist_id))
def assert_song_not_in_playlist(self, playlist_id, song): """throw exception if song in playlist""" self.assert_user_owns_playlist(playlist_id) if self.song_in_playlist(playlist_id, song): raise APIException("song already in playlist")
def assert_song_not_exists(self, song_id): """throws an exception if the song_id exists""" if self.song_exists(song_id): raise APIException("pre-existing song error")
def assert_song_exists(self, song_id): """throws an exception if the song_id does not exist""" if not self.song_exists(song_id): raise APIException("song does not exist")
def assert_user_not_exists(self, name): """throws an exception if the user exists""" if self.user_id(name): raise APIException("prexisting user")