Update delete endpoints to return True if successful.

If the operation isn't successful, the API returns an error code,
which raises an OpenPhotoError exception.
This commit is contained in:
Pete 2013-04-09 18:18:12 +01:00
parent 306c8430ba
commit e09f166743
7 changed files with 60 additions and 24 deletions

View file

@ -20,10 +20,14 @@ class ApiAlbum:
return Album(self._client, result) return Album(self._client, result)
def delete(self, album, **kwds): def delete(self, album, **kwds):
""" Delete an album """ """
Delete an album.
Returns True if successful.
Raises an OpenPhotoError if not.
"""
if not isinstance(album, Album): if not isinstance(album, Album):
album = Album(self._client, {"id": album}) album = Album(self._client, {"id": album})
album.delete(**kwds) return album.delete(**kwds)
def form(self, album, **kwds): def form(self, album, **kwds):
raise NotImplementedError() raise NotImplementedError()

View file

@ -14,25 +14,38 @@ class ApiPhotos:
return [Photo(self._client, photo) for photo in photos] return [Photo(self._client, photo) for photo in photos]
def update(self, photos, **kwds): def update(self, photos, **kwds):
""" Updates a list of photos """ """
Updates a list of photos.
Returns True if successful.
Raises OpenPhotoError if not.
"""
if not self._client.post("/photos/update.json", ids=photos, **kwds)["result"]: if not self._client.post("/photos/update.json", ids=photos, **kwds)["result"]:
raise OpenPhotoError("Update response returned False") raise OpenPhotoError("Update response returned False")
return True
def delete(self, photos, **kwds): def delete(self, photos, **kwds):
""" Deletes a list of photos """ """
Deletes a list of photos.
Returns True if successful.
Raises OpenPhotoError if not.
"""
if not self._client.post("/photos/delete.json", ids=photos, **kwds)["result"]: if not self._client.post("/photos/delete.json", ids=photos, **kwds)["result"]:
raise OpenPhotoError("Delete response returned False") raise OpenPhotoError("Delete response returned False")
return True
class ApiPhoto: class ApiPhoto:
def __init__(self, client): def __init__(self, client):
self._client = client self._client = client
def delete(self, photo, **kwds): def delete(self, photo, **kwds):
""" Delete a photo """ """
Delete a photo.
Returns True if successful.
Raises an OpenPhotoError if not.
"""
if not isinstance(photo, Photo): if not isinstance(photo, Photo):
photo = Photo(self._client, {"id": photo}) photo = Photo(self._client, {"id": photo})
photo.delete(**kwds) return photo.delete(**kwds)
def edit(self, photo, **kwds): def edit(self, photo, **kwds):
""" Returns an HTML form to edit a photo """ """ Returns an HTML form to edit a photo """

View file

@ -20,10 +20,14 @@ class ApiTag:
return Tag(self._client, result) return Tag(self._client, result)
def delete(self, tag, **kwds): def delete(self, tag, **kwds):
""" Delete a tag """ """
Delete a tag.
Returns True if successful.
Raises an OpenPhotoError if not.
"""
if not isinstance(tag, Tag): if not isinstance(tag, Tag):
tag = Tag(self._client, {"id": tag}) tag = Tag(self._client, {"id": tag})
tag.delete(**kwds) return tag.delete(**kwds)
def update(self, tag, **kwds): def update(self, tag, **kwds):
""" Update a tag """ """ Update a tag """

View file

@ -39,9 +39,14 @@ class OpenPhotoObject:
class Photo(OpenPhotoObject): class Photo(OpenPhotoObject):
def delete(self, **kwds): def delete(self, **kwds):
""" Delete this photo """ """
self._openphoto.post("/photo/%s/delete.json" % self.id, **kwds) Delete this photo.
Returns True if successful.
Raises an OpenPhotoError if not.
"""
result = self._openphoto.post("/photo/%s/delete.json" % self.id, **kwds)["result"]
self._replace_fields({}) self._replace_fields({})
return result
def edit(self, **kwds): def edit(self, **kwds):
""" Returns an HTML form to edit the photo """ """ Returns an HTML form to edit the photo """
@ -97,9 +102,14 @@ class Photo(OpenPhotoObject):
class Tag(OpenPhotoObject): class Tag(OpenPhotoObject):
def delete(self, **kwds): def delete(self, **kwds):
""" Delete this tag """ """
self._openphoto.post("/tag/%s/delete.json" % self.id, **kwds) Delete this tag.
Returns True if successful.
Raises an OpenPhotoError if not.
"""
result = self._openphoto.post("/tag/%s/delete.json" % self.id, **kwds)["result"]
self._replace_fields({}) self._replace_fields({})
return result
def update(self, **kwds): def update(self, **kwds):
""" Update this tag with the specified parameters """ """ Update this tag with the specified parameters """
@ -125,9 +135,14 @@ class Album(OpenPhotoObject):
self.photos[i] = Photo(self._openphoto, photo) self.photos[i] = Photo(self._openphoto, photo)
def delete(self, **kwds): def delete(self, **kwds):
""" Delete this album """ """
self._openphoto.post("/album/%s/delete.json" % self.id, **kwds) Delete this album.
Returns True if successful.
Raises an OpenPhotoError if not.
"""
result = self._openphoto.post("/album/%s/delete.json" % self.id, **kwds)["result"]
self._replace_fields({}) self._replace_fields({})
return result
def form(self, **kwds): def form(self, **kwds):
raise NotImplementedError() raise NotImplementedError()

View file

@ -15,13 +15,13 @@ class TestAlbums(test_base.TestBase):
self.assertIn(album_name, [a.name for a in self.client.albums.list()]) self.assertIn(album_name, [a.name for a in self.client.albums.list()])
# Delete the album # Delete the album
self.client.album.delete(album.id) self.assertTrue(self.client.album.delete(album.id))
# Check that the album is now gone # Check that the album is now gone
self.assertNotIn(album_name, [a.name for a in self.client.albums.list()]) self.assertNotIn(album_name, [a.name for a in self.client.albums.list()])
# Create it again, and delete it using the Album object # Create it again, and delete it using the Album object
album = self.client.album.create(album_name) album = self.client.album.create(album_name)
album.delete() self.assertTrue(album.delete())
# Check that the album is now gone # Check that the album is now gone
self.assertNotIn(album_name, [a.name for a in self.client.albums.list()]) self.assertNotIn(album_name, [a.name for a in self.client.albums.list()])

View file

@ -6,11 +6,11 @@ class TestPhotos(test_base.TestBase):
def test_delete_upload(self): def test_delete_upload(self):
""" Test photo deletion and upload """ """ Test photo deletion and upload """
# Delete one photo using the OpenPhoto class, passing in the id # Delete one photo using the OpenPhoto class, passing in the id
self.client.photo.delete(self.photos[0].id) self.assertTrue(self.client.photo.delete(self.photos[0].id))
# Delete one photo using the OpenPhoto class, passing in the object # Delete one photo using the OpenPhoto class, passing in the object
self.client.photo.delete(self.photos[1]) self.assertTrue(self.client.photo.delete(self.photos[1]))
# And another using the Photo object directly # And another using the Photo object directly
self.photos[2].delete() self.assertTrue(self.photos[2].delete())
# Check that they're gone # Check that they're gone
self.assertEqual(self.client.photos.list(), []) self.assertEqual(self.client.photos.list(), [])
@ -32,7 +32,7 @@ class TestPhotos(test_base.TestBase):
self.assertIn(ret_val.pathOriginal, pathOriginals) self.assertIn(ret_val.pathOriginal, pathOriginals)
# Delete all photos in one go # Delete all photos in one go
self.client.photos.delete(self.photos) self.assertTrue(self.client.photos.delete(self.photos))
# Check they're gone # Check they're gone
self.photos = self.client.photos.list() self.photos = self.client.photos.list()

View file

@ -16,13 +16,13 @@ class TestTags(test_base.TestBase):
self.assertIn(tag_name, self.client.tags.list()) self.assertIn(tag_name, self.client.tags.list())
# Delete the tag # Delete the tag
self.client.tag.delete(tag_name) self.assertTrue(self.client.tag.delete(tag_name))
# Check that the tag is now gone # Check that the tag is now gone
self.assertNotIn(tag_name, self.client.tags.list()) self.assertNotIn(tag_name, self.client.tags.list())
# Create and delete using the Tag object directly # Create and delete using the Tag object directly
tag = self.client.tag.create(tag_name) tag = self.client.tag.create(tag_name)
tag.delete() self.assertTrue(tag.delete())
# Check that the tag is now gone # Check that the tag is now gone
self.assertNotIn(tag_name, self.client.tags.list()) self.assertNotIn(tag_name, self.client.tags.list())