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:
parent
21c38c53cb
commit
fafeb70ec1
7 changed files with 53 additions and 28 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
12
openphoto/errors.py
Normal file
12
openphoto/errors.py
Normal 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
|
||||
|
|
@ -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 """
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue