If an object is passed as a parameter, extract its ID.

This allows things like:
   photos = client.photos.list()
   client.photos.delete(photos)
This commit is contained in:
sneakypete81 2012-09-04 09:08:57 +01:00
parent 21c38c53cb
commit fafeb70ec1
7 changed files with 53 additions and 28 deletions

View file

@ -1,4 +1,5 @@
from openphoto_http import OpenPhotoHttp, OpenPhotoError, OpenPhotoDuplicateError
from openphoto_http import OpenPhotoHttp
from errors import *
import api_photo
import api_tag
import api_album

View file

@ -1,4 +1,4 @@
from openphoto_http import OpenPhotoHttp, OpenPhotoError
from errors import *
from objects import Album
class ApiAlbums:
@ -21,6 +21,7 @@ class ApiAlbum:
def delete(self, album, **kwds):
""" Delete an album """
if not isinstance(album, Album):
album = Album(self._client, {"id": album})
album.delete(**kwds)
@ -35,6 +36,7 @@ class ApiAlbum:
def update(self, album, **kwds):
""" Update an album """
if not isinstance(album, Album):
album = Album(self._client, {"id": album})
album.update(**kwds)
# Don't return the album, since the API doesn't give us the modified album

View file

@ -1,6 +1,6 @@
import base64
from openphoto_http import OpenPhotoHttp, OpenPhotoError
from errors import *
from objects import Photo
class ApiPhotos:
@ -30,11 +30,13 @@ class ApiPhoto:
def delete(self, photo, **kwds):
""" Delete a photo """
if not isinstance(photo, Photo):
photo = Photo(self._client, {"id": photo})
photo.delete(**kwds)
def edit(self, photo, **kwds):
""" Returns an HTML form to edit a photo """
if not isinstance(photo, Photo):
photo = Photo(self._client, {"id": photo})
return photo.edit(**kwds)
@ -49,6 +51,7 @@ class ApiPhoto:
Update a photo with the specified parameters.
Returns the updated photo object
"""
if not isinstance(photo, Photo):
photo = Photo(self._client, {"id": photo})
photo.update(**kwds)
return photo
@ -58,6 +61,7 @@ class ApiPhoto:
Used to view the photo at a particular size.
Returns the requested photo object
"""
if not isinstance(photo, Photo):
photo = Photo(self._client, {"id": photo})
photo.view(**kwds)
return photo
@ -80,6 +84,7 @@ class ApiPhoto:
Returns a dict containing the next and previous photo objects,
given a photo in the middle.
"""
if not isinstance(photo, Photo):
photo = Photo(self._client, {"id": photo})
return photo.next_previous(**kwds)

View file

@ -1,4 +1,4 @@
from openphoto_http import OpenPhotoHttp, OpenPhotoError
from errors import *
from objects import Tag
class ApiTags:
@ -21,13 +21,13 @@ class ApiTag:
def delete(self, tag, **kwds):
""" Delete a tag """
if not isinstance(tag, Tag):
tag = Tag(self._client, {"id": tag})
tag.delete(**kwds)
def update(self, tag, **kwds):
""" Update a tag """
if not isinstance(tag, Tag):
tag = Tag(self._client, {"id": tag})
tag.update(**kwds)
return tag

12
openphoto/errors.py Normal file
View file

@ -0,0 +1,12 @@
class OpenPhotoError(Exception):
""" Indicates that an OpenPhoto operation failed """
pass
class OpenPhotoDuplicateError(OpenPhotoError):
""" Indicates that an upload operation failed due to a duplicate photo """
pass
class NotImplementedError(OpenPhotoError):
""" Indicates that the API function has not yet been coded - please help! """
pass

View file

@ -1,4 +1,4 @@
from openphoto_http import OpenPhotoError, NotImplementedError
from errors import *
class OpenPhotoObject:
""" Base object supporting the storage of custom fields as attributes """

View file

@ -7,17 +7,8 @@ try:
except ImportError:
import simplejson as json
class OpenPhotoError(Exception):
""" Indicates that an OpenPhoto operation failed """
pass
class OpenPhotoDuplicateError(OpenPhotoError):
""" Indicates that an upload operation failed due to a duplicate photo """
pass
class NotImplementedError(OpenPhotoError):
""" Indicates that the API function has not yet been coded - please help! """
pass
from objects import OpenPhotoObject
from errors import *
DUPLICATE_RESPONSE = {"code": 409,
"message": "This photo already exists"}
@ -102,16 +93,30 @@ class OpenPhotoHttp:
""" Converts Unicode/lists/booleans inside HTTP parameters """
processed_params = {}
for key, value in params.items():
# Extract IDs from objects
if isinstance(value, OpenPhotoObject):
value = value.id
# Use UTF-8 encoding
if isinstance(value, unicode):
value = value.encode('utf-8')
# Handle lists
if isinstance(value, list):
value = ",".join(value)
# Make a copy of the list, to avoid overwriting the original
new_list = list(value)
# Extract IDs from objects in the list
for i, item in enumerate(new_list):
if isinstance(item, OpenPhotoObject):
new_list[i] = item.id
# Convert list to string
value = ",".join(new_list)
# Handle booleans
if isinstance(value, bool):
value = 1 if value else 0
processed_params[key] = value
return processed_params
@staticmethod