From fafeb70ec12b7c7c295705e75ccba11f52af9df7 Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Tue, 4 Sep 2012 09:08:57 +0100 Subject: [PATCH] If an object is passed as a parameter, extract its ID. This allows things like: photos = client.photos.list() client.photos.delete(photos) --- openphoto/__init__.py | 3 ++- openphoto/api_album.py | 8 +++++--- openphoto/api_photo.py | 17 +++++++++++------ openphoto/api_tag.py | 10 +++++----- openphoto/errors.py | 12 ++++++++++++ openphoto/objects.py | 2 +- openphoto/openphoto_http.py | 29 +++++++++++++++++------------ 7 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 openphoto/errors.py diff --git a/openphoto/__init__.py b/openphoto/__init__.py index c118d4a..7e497bd 100644 --- a/openphoto/__init__.py +++ b/openphoto/__init__.py @@ -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 diff --git a/openphoto/api_album.py b/openphoto/api_album.py index a4628fd..4820ecf 100644 --- a/openphoto/api_album.py +++ b/openphoto/api_album.py @@ -1,4 +1,4 @@ -from openphoto_http import OpenPhotoHttp, OpenPhotoError +from errors import * from objects import Album class ApiAlbums: @@ -21,7 +21,8 @@ class ApiAlbum: def delete(self, album, **kwds): """ Delete an album """ - album = Album(self._client, {"id": album}) + if not isinstance(album, Album): + album = Album(self._client, {"id": album}) album.delete(**kwds) def form(self, album, **kwds): @@ -35,6 +36,7 @@ class ApiAlbum: def update(self, album, **kwds): """ Update an album """ - album = Album(self._client, {"id": 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 diff --git a/openphoto/api_photo.py b/openphoto/api_photo.py index 6109388..0686a88 100644 --- a/openphoto/api_photo.py +++ b/openphoto/api_photo.py @@ -1,6 +1,6 @@ import base64 -from openphoto_http import OpenPhotoHttp, OpenPhotoError +from errors import * from objects import Photo class ApiPhotos: @@ -30,12 +30,14 @@ class ApiPhoto: def delete(self, photo, **kwds): """ Delete a photo """ - photo = Photo(self._client, {"id": 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 """ - photo = Photo(self._client, {"id": photo}) + if not isinstance(photo, Photo): + photo = Photo(self._client, {"id": photo}) return photo.edit(**kwds) def replace(self, photo, photo_file, **kwds): @@ -49,7 +51,8 @@ class ApiPhoto: Update a photo with the specified parameters. Returns the updated photo object """ - photo = Photo(self._client, {"id": photo}) + if not isinstance(photo, Photo): + photo = Photo(self._client, {"id": photo}) photo.update(**kwds) return photo @@ -58,7 +61,8 @@ class ApiPhoto: Used to view the photo at a particular size. Returns the requested photo object """ - photo = Photo(self._client, {"id": photo}) + if not isinstance(photo, Photo): + photo = Photo(self._client, {"id": photo}) photo.view(**kwds) return photo @@ -80,7 +84,8 @@ class ApiPhoto: Returns a dict containing the next and previous photo objects, given a photo in the middle. """ - photo = Photo(self._client, {"id": photo}) + if not isinstance(photo, Photo): + photo = Photo(self._client, {"id": photo}) return photo.next_previous(**kwds) def transform(self, photo, **kwds): diff --git a/openphoto/api_tag.py b/openphoto/api_tag.py index 9360ecf..89f9fee 100644 --- a/openphoto/api_tag.py +++ b/openphoto/api_tag.py @@ -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 """ - tag = Tag(self._client, {"id": tag}) + if not isinstance(tag, Tag): + tag = Tag(self._client, {"id": tag}) tag.delete(**kwds) def update(self, tag, **kwds): """ Update a tag """ - tag = Tag(self._client, {"id": tag}) + if not isinstance(tag, Tag): + tag = Tag(self._client, {"id": tag}) tag.update(**kwds) return tag - - diff --git a/openphoto/errors.py b/openphoto/errors.py new file mode 100644 index 0000000..25a0b24 --- /dev/null +++ b/openphoto/errors.py @@ -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 + diff --git a/openphoto/objects.py b/openphoto/objects.py index 2af570f..0a5586a 100644 --- a/openphoto/objects.py +++ b/openphoto/objects.py @@ -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 """ diff --git a/openphoto/openphoto_http.py b/openphoto/openphoto_http.py index a9881f6..bcc8b3a 100644 --- a/openphoto/openphoto_http.py +++ b/openphoto/openphoto_http.py @@ -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