From 854c4fd605fcaaab616aba17a148b10b34c711b0 Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Tue, 4 Sep 2012 20:36:26 +0100 Subject: [PATCH] Small refactor of code that automatically converts returned dicts into objects for Album commands. Add comments to handle API change when/if frontend issue number 937 lands --- openphoto/api_album.py | 5 ++++- openphoto/objects.py | 30 +++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/openphoto/api_album.py b/openphoto/api_album.py index f20c94f..90bc47d 100644 --- a/openphoto/api_album.py +++ b/openphoto/api_album.py @@ -39,7 +39,10 @@ class ApiAlbum: 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 + + # Don't return the album, since the API currently doesn't give us the modified album + # Uncomment the following once frontend issue #937 is resolved +# return album def view(self, album, **kwds): """ diff --git a/openphoto/objects.py b/openphoto/objects.py index 0a5586a..a2328bf 100644 --- a/openphoto/objects.py +++ b/openphoto/objects.py @@ -104,9 +104,18 @@ class Tag(OpenPhotoObject): class Album(OpenPhotoObject): def __init__(self, openphoto, json_dict): OpenPhotoObject.__init__(self, openphoto, json_dict) - # Update the cover attribute with a photo object - if hasattr(self, "cover") and self.cover is not None: - self.cover = Photo(openphoto, self.cover) + self._update_fields_with_objects() + + def _update_fields_with_objects(self): + """ Convert dict fields into objects, where appropriate """ + # Update the cover with a photo object + if hasattr(self, "cover") and isinstance(self.cover, dict): + self.cover = Photo(self._openphoto, self.cover) + # Update the photo list with photo objects + if hasattr(self, "photos") and isinstance(self.photos, list): + for i, photo in enumerate(self.photos): + if isinstance(photo, dict): + self.photos[i] = Photo(self._openphoto, photo) def delete(self, **kwds): """ Delete this album """ @@ -124,12 +133,16 @@ class Album(OpenPhotoObject): def update(self, **kwds): """ Update this album with the specified parameters """ - self._openphoto.post("/album/%s/update.json" % self.id, - **kwds)["result"] + new_dict = self._openphoto.post("/album/%s/update.json" % self.id, + **kwds)["result"] + # Since the API doesn't give us the modified album, we need to # update our fields based on the kwds that were sent self._set_fields(kwds) + # Replace the above line with the below once frontend issue #937 is resolved +# self._set_fields(new_dict) +# self._update_fields_with_objects() def view(self, **kwds): """ @@ -138,10 +151,5 @@ class Album(OpenPhotoObject): """ result = self._openphoto.get("/album/%s/view.json" % self.id, **kwds)["result"] - # Update the cover attribute with a photo object - if result["cover"] is not None: - result["cover"] = Photo(self._openphoto, result["cover"]) - # Update the photo list with photo objects - for i, photo in enumerate(result["photos"]): - result["photos"][i] = Photo(self._openphoto, result["photos"][i]) self._replace_fields(result) + self._update_fields_with_objects()