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_photo
import api_tag import api_tag
import api_album import api_album

View file

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

View file

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

View file

@ -1,4 +1,4 @@
from openphoto_http import OpenPhotoHttp, OpenPhotoError from errors import *
from objects import Tag from objects import Tag
class ApiTags: class ApiTags:
@ -21,13 +21,13 @@ class ApiTag:
def delete(self, tag, **kwds): def delete(self, tag, **kwds):
""" Delete a tag """ """ Delete a tag """
if not isinstance(tag, Tag):
tag = Tag(self._client, {"id": tag}) tag = Tag(self._client, {"id": tag})
tag.delete(**kwds) tag.delete(**kwds)
def update(self, tag, **kwds): def update(self, tag, **kwds):
""" Update a tag """ """ Update a tag """
if not isinstance(tag, Tag):
tag = Tag(self._client, {"id": tag}) tag = Tag(self._client, {"id": tag})
tag.update(**kwds) tag.update(**kwds)
return tag 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: class OpenPhotoObject:
""" Base object supporting the storage of custom fields as attributes """ """ Base object supporting the storage of custom fields as attributes """

View file

@ -7,17 +7,8 @@ try:
except ImportError: except ImportError:
import simplejson as json import simplejson as json
class OpenPhotoError(Exception): from objects import OpenPhotoObject
""" Indicates that an OpenPhoto operation failed """ from errors import *
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
DUPLICATE_RESPONSE = {"code": 409, DUPLICATE_RESPONSE = {"code": 409,
"message": "This photo already exists"} "message": "This photo already exists"}
@ -102,16 +93,30 @@ class OpenPhotoHttp:
""" Converts Unicode/lists/booleans inside HTTP parameters """ """ Converts Unicode/lists/booleans inside HTTP parameters """
processed_params = {} processed_params = {}
for key, value in params.items(): for key, value in params.items():
# Extract IDs from objects
if isinstance(value, OpenPhotoObject):
value = value.id
# Use UTF-8 encoding # Use UTF-8 encoding
if isinstance(value, unicode): if isinstance(value, unicode):
value = value.encode('utf-8') value = value.encode('utf-8')
# Handle lists # Handle lists
if isinstance(value, list): 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 # Handle booleans
if isinstance(value, bool): if isinstance(value, bool):
value = 1 if value else 0 value = 1 if value else 0
processed_params[key] = value processed_params[key] = value
return processed_params return processed_params
@staticmethod @staticmethod