Moved trovebox.api and trovebox.objects into packages
This commit is contained in:
parent
56d6432ff0
commit
fd5389e0d7
16 changed files with 279 additions and 250 deletions
|
@ -19,7 +19,7 @@ class TestAlbums(unittest.TestCase):
|
||||||
"totalRows": 2}]
|
"totalRows": 2}]
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = trovebox.Trovebox(host=self.test_host)
|
self.client = trovebox.Trovebox(host=self.test_host)
|
||||||
self.test_albums = [trovebox.objects.Album(self.client, album)
|
self.test_albums = [trovebox.objects.album.Album(self.client, album)
|
||||||
for album in self.test_albums_dict]
|
for album in self.test_albums_dict]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -171,9 +171,9 @@ class TestHttp(unittest.TestCase):
|
||||||
def test_get_parameter_processing(self):
|
def test_get_parameter_processing(self):
|
||||||
"""Check that the parameter processing function is working"""
|
"""Check that the parameter processing function is working"""
|
||||||
self._register_uri(httpretty.GET)
|
self._register_uri(httpretty.GET)
|
||||||
photo = trovebox.objects.Photo(None, {"id": "photo_id"})
|
photo = trovebox.objects.photo.Photo(None, {"id": "photo_id"})
|
||||||
album = trovebox.objects.Album(None, {"id": "album_id"})
|
album = trovebox.objects.photo.Album(None, {"id": "album_id"})
|
||||||
tag = trovebox.objects.Tag(None, {"id": "tag_id"})
|
tag = trovebox.objects.tag.Tag(None, {"id": "tag_id"})
|
||||||
self.client.get(self.test_endpoint,
|
self.client.get(self.test_endpoint,
|
||||||
photo=photo, album=album, tag=tag,
|
photo=photo, album=album, tag=tag,
|
||||||
list_=[photo, album, tag],
|
list_=[photo, album, tag],
|
||||||
|
|
|
@ -18,7 +18,7 @@ class TestPhotos(unittest.TestCase):
|
||||||
"totalPages": 1, "totalRows": 2}]
|
"totalPages": 1, "totalRows": 2}]
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = trovebox.Trovebox(host=self.test_host)
|
self.client = trovebox.Trovebox(host=self.test_host)
|
||||||
self.test_photos = [trovebox.objects.Photo(self.client, photo)
|
self.test_photos = [trovebox.objects.photo.Photo(self.client, photo)
|
||||||
for photo in self.test_photos_dict]
|
for photo in self.test_photos_dict]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -15,7 +15,7 @@ class TestTags(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = trovebox.Trovebox(host=self.test_host)
|
self.client = trovebox.Trovebox(host=self.test_host)
|
||||||
self.test_tags = [trovebox.objects.Tag(self.client, tag)
|
self.test_tags = [trovebox.objects.tag.Tag(self.client, tag)
|
||||||
for tag in self.test_tags_dict]
|
for tag in self.test_tags_dict]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -4,9 +4,9 @@ __init__.py : Trovebox package top level
|
||||||
from .http import Http
|
from .http import Http
|
||||||
from .errors import *
|
from .errors import *
|
||||||
from ._version import __version__
|
from ._version import __version__
|
||||||
from . import api_photo
|
from api import api_photo
|
||||||
from . import api_tag
|
from api import api_tag
|
||||||
from . import api_album
|
from api import api_album
|
||||||
|
|
||||||
LATEST_API_VERSION = 2
|
LATEST_API_VERSION = 2
|
||||||
|
|
||||||
|
|
4
trovebox/api/__init__.py
Normal file
4
trovebox/api/__init__.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
"""
|
||||||
|
trovebox.api Package
|
||||||
|
Definitions for each of the Trovebox API endpoints
|
||||||
|
"""
|
|
@ -1,9 +1,10 @@
|
||||||
"""
|
"""
|
||||||
api_album.py : Trovebox Album API Classes
|
api_album.py : Trovebox Album API Classes
|
||||||
"""
|
"""
|
||||||
from .objects import Album
|
from trovebox.objects.album import Album
|
||||||
|
|
||||||
class ApiAlbums(object):
|
class ApiAlbums(object):
|
||||||
|
""" Definitions of /albums/ API endpoints """
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ class ApiAlbums(object):
|
||||||
return [Album(self._client, album) for album in results]
|
return [Album(self._client, album) for album in results]
|
||||||
|
|
||||||
class ApiAlbum(object):
|
class ApiAlbum(object):
|
||||||
|
""" Definitions of /album/ API endpoints """
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
||||||
|
|
|
@ -3,9 +3,9 @@ api_photo.py : Trovebox Photo API Classes
|
||||||
"""
|
"""
|
||||||
import base64
|
import base64
|
||||||
|
|
||||||
from .errors import TroveboxError
|
from trovebox.errors import TroveboxError
|
||||||
from . import http
|
from trovebox import http
|
||||||
from .objects import Photo
|
from trovebox.objects.photo import Photo
|
||||||
|
|
||||||
def extract_ids(photos):
|
def extract_ids(photos):
|
||||||
"""
|
"""
|
||||||
|
@ -21,6 +21,7 @@ def extract_ids(photos):
|
||||||
return ids
|
return ids
|
||||||
|
|
||||||
class ApiPhotos(object):
|
class ApiPhotos(object):
|
||||||
|
""" Definitions of /photos/ API endpoints """
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
||||||
|
|
||||||
|
@ -55,6 +56,7 @@ class ApiPhotos(object):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
class ApiPhoto(object):
|
class ApiPhoto(object):
|
||||||
|
""" Definitions of /photo/ API endpoints """
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
"""
|
"""
|
||||||
api_tag.py : Trovebox Tag API Classes
|
api_tag.py : Trovebox Tag API Classes
|
||||||
"""
|
"""
|
||||||
from .objects import Tag
|
from trovebox.objects.tag import Tag
|
||||||
|
|
||||||
class ApiTags(object):
|
class ApiTags(object):
|
||||||
|
""" Definitions of /tags/ API endpoints """
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ class ApiTags(object):
|
||||||
return [Tag(self._client, tag) for tag in results]
|
return [Tag(self._client, tag) for tag in results]
|
||||||
|
|
||||||
class ApiTag(object):
|
class ApiTag(object):
|
||||||
|
""" Definitions of /tag/ API endpoints """
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self._client = client
|
self._client = client
|
||||||
|
|
|
@ -11,7 +11,7 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urlparse import urlparse, urlunparse # Python2
|
from urlparse import urlparse, urlunparse # Python2
|
||||||
|
|
||||||
from .objects import TroveboxObject
|
from objects.trovebox_object import TroveboxObject
|
||||||
from .errors import *
|
from .errors import *
|
||||||
from .auth import Auth
|
from .auth import Auth
|
||||||
|
|
||||||
|
|
|
@ -1,235 +0,0 @@
|
||||||
"""
|
|
||||||
objects.py : Basic Trovebox API Objects
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
from urllib.parse import quote # Python3
|
|
||||||
except ImportError:
|
|
||||||
from urllib import quote # Python2
|
|
||||||
|
|
||||||
from .errors import TroveboxError
|
|
||||||
|
|
||||||
class TroveboxObject(object):
|
|
||||||
""" Base object supporting the storage of custom fields as attributes """
|
|
||||||
def __init__(self, trovebox, json_dict):
|
|
||||||
self.id = None
|
|
||||||
self.name = None
|
|
||||||
self._trovebox = trovebox
|
|
||||||
self._json_dict = json_dict
|
|
||||||
self._set_fields(json_dict)
|
|
||||||
|
|
||||||
def _set_fields(self, json_dict):
|
|
||||||
""" Set this object's attributes specified in json_dict """
|
|
||||||
for key, value in json_dict.items():
|
|
||||||
if key.startswith("_"):
|
|
||||||
raise ValueError("Illegal attribute: %s" % key)
|
|
||||||
setattr(self, key, value)
|
|
||||||
|
|
||||||
def _replace_fields(self, json_dict):
|
|
||||||
"""
|
|
||||||
Delete this object's attributes, and replace with
|
|
||||||
those in json_dict.
|
|
||||||
"""
|
|
||||||
for key in self._json_dict.keys():
|
|
||||||
delattr(self, key)
|
|
||||||
self._json_dict = json_dict
|
|
||||||
self._set_fields(json_dict)
|
|
||||||
|
|
||||||
def _delete_fields(self):
|
|
||||||
"""
|
|
||||||
Delete this object's attributes, including name and id
|
|
||||||
"""
|
|
||||||
for key in self._json_dict.keys():
|
|
||||||
delattr(self, key)
|
|
||||||
self._json_dict = {}
|
|
||||||
self.id = None
|
|
||||||
self.name = None
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
if self.name is not None:
|
|
||||||
return "<%s name='%s'>" % (self.__class__, self.name)
|
|
||||||
elif self.id is not None:
|
|
||||||
return "<%s id='%s'>" % (self.__class__, self.id)
|
|
||||||
else:
|
|
||||||
return "<%s>" % (self.__class__)
|
|
||||||
|
|
||||||
def get_fields(self):
|
|
||||||
""" Returns this object's attributes """
|
|
||||||
return self._json_dict
|
|
||||||
|
|
||||||
|
|
||||||
class Photo(TroveboxObject):
|
|
||||||
def delete(self, **kwds):
|
|
||||||
"""
|
|
||||||
Delete this photo.
|
|
||||||
Returns True if successful.
|
|
||||||
Raises an TroveboxError if not.
|
|
||||||
"""
|
|
||||||
result = self._trovebox.post("/photo/%s/delete.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
if not result:
|
|
||||||
raise TroveboxError("Delete response returned False")
|
|
||||||
self._delete_fields()
|
|
||||||
return result
|
|
||||||
|
|
||||||
def edit(self, **kwds):
|
|
||||||
""" Returns an HTML form to edit the photo """
|
|
||||||
result = self._trovebox.get("/photo/%s/edit.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
return result["markup"]
|
|
||||||
|
|
||||||
def replace(self, photo_file, **kwds):
|
|
||||||
""" Not implemented yet """
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def replace_encoded(self, photo_file, **kwds):
|
|
||||||
""" Not implemented yet """
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def update(self, **kwds):
|
|
||||||
""" Update this photo with the specified parameters """
|
|
||||||
new_dict = self._trovebox.post("/photo/%s/update.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
self._replace_fields(new_dict)
|
|
||||||
|
|
||||||
def view(self, **kwds):
|
|
||||||
"""
|
|
||||||
Used to view the photo at a particular size.
|
|
||||||
Updates the photo's fields with the response.
|
|
||||||
"""
|
|
||||||
new_dict = self._trovebox.get("/photo/%s/view.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
self._replace_fields(new_dict)
|
|
||||||
|
|
||||||
def dynamic_url(self, **kwds):
|
|
||||||
""" Not implemented yet """
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def next_previous(self, **kwds):
|
|
||||||
"""
|
|
||||||
Returns a dict containing the next and previous photo lists
|
|
||||||
(there may be more than one next/previous photo returned).
|
|
||||||
"""
|
|
||||||
result = self._trovebox.get("/photo/%s/nextprevious.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
value = {}
|
|
||||||
if "next" in result:
|
|
||||||
# Workaround for APIv1
|
|
||||||
if not isinstance(result["next"], list):
|
|
||||||
result["next"] = [result["next"]]
|
|
||||||
|
|
||||||
value["next"] = []
|
|
||||||
for photo in result["next"]:
|
|
||||||
value["next"].append(Photo(self._trovebox, photo))
|
|
||||||
|
|
||||||
if "previous" in result:
|
|
||||||
# Workaround for APIv1
|
|
||||||
if not isinstance(result["previous"], list):
|
|
||||||
result["previous"] = [result["previous"]]
|
|
||||||
|
|
||||||
value["previous"] = []
|
|
||||||
for photo in result["previous"]:
|
|
||||||
value["previous"].append(Photo(self._trovebox, photo))
|
|
||||||
|
|
||||||
return value
|
|
||||||
|
|
||||||
def transform(self, **kwds):
|
|
||||||
"""
|
|
||||||
Performs transformation specified in **kwds
|
|
||||||
Example: transform(rotate=90)
|
|
||||||
"""
|
|
||||||
new_dict = self._trovebox.post("/photo/%s/transform.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
|
|
||||||
# APIv1 doesn't return the transformed photo (frontend issue #955)
|
|
||||||
if isinstance(new_dict, bool):
|
|
||||||
new_dict = self._trovebox.get("/photo/%s/view.json" %
|
|
||||||
self.id)["result"]
|
|
||||||
|
|
||||||
self._replace_fields(new_dict)
|
|
||||||
|
|
||||||
class Tag(TroveboxObject):
|
|
||||||
def delete(self, **kwds):
|
|
||||||
"""
|
|
||||||
Delete this tag.
|
|
||||||
Returns True if successful.
|
|
||||||
Raises an TroveboxError if not.
|
|
||||||
"""
|
|
||||||
result = self._trovebox.post("/tag/%s/delete.json" %
|
|
||||||
quote(self.id), **kwds)["result"]
|
|
||||||
if not result:
|
|
||||||
raise TroveboxError("Delete response returned False")
|
|
||||||
self._delete_fields()
|
|
||||||
return result
|
|
||||||
|
|
||||||
def update(self, **kwds):
|
|
||||||
""" Update this tag with the specified parameters """
|
|
||||||
new_dict = self._trovebox.post("/tag/%s/update.json" % quote(self.id),
|
|
||||||
**kwds)["result"]
|
|
||||||
self._replace_fields(new_dict)
|
|
||||||
|
|
||||||
|
|
||||||
class Album(TroveboxObject):
|
|
||||||
def __init__(self, trovebox, json_dict):
|
|
||||||
self.photos = None
|
|
||||||
self.cover = None
|
|
||||||
TroveboxObject.__init__(self, trovebox, json_dict)
|
|
||||||
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 isinstance(self.cover, dict):
|
|
||||||
self.cover = Photo(self._trovebox, self.cover)
|
|
||||||
# Update the photo list with photo objects
|
|
||||||
if isinstance(self.photos, list):
|
|
||||||
for i, photo in enumerate(self.photos):
|
|
||||||
if isinstance(photo, dict):
|
|
||||||
self.photos[i] = Photo(self._trovebox, photo)
|
|
||||||
|
|
||||||
def delete(self, **kwds):
|
|
||||||
"""
|
|
||||||
Delete this album.
|
|
||||||
Returns True if successful.
|
|
||||||
Raises an TroveboxError if not.
|
|
||||||
"""
|
|
||||||
result = self._trovebox.post("/album/%s/delete.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
if not result:
|
|
||||||
raise TroveboxError("Delete response returned False")
|
|
||||||
self._delete_fields()
|
|
||||||
return result
|
|
||||||
|
|
||||||
def form(self, **kwds):
|
|
||||||
""" Not implemented yet """
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def add_photos(self, photos, **kwds):
|
|
||||||
""" Not implemented yet """
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def remove_photos(self, photos, **kwds):
|
|
||||||
""" Not implemented yet """
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def update(self, **kwds):
|
|
||||||
""" Update this album with the specified parameters """
|
|
||||||
new_dict = self._trovebox.post("/album/%s/update.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
|
|
||||||
# APIv1 doesn't return the updated album (frontend issue #937)
|
|
||||||
if isinstance(new_dict, bool):
|
|
||||||
new_dict = self._trovebox.get("/album/%s/view.json" %
|
|
||||||
self.id)["result"]
|
|
||||||
|
|
||||||
self._replace_fields(new_dict)
|
|
||||||
self._update_fields_with_objects()
|
|
||||||
|
|
||||||
def view(self, **kwds):
|
|
||||||
"""
|
|
||||||
Requests the full contents of the album.
|
|
||||||
Updates the album's fields with the response.
|
|
||||||
"""
|
|
||||||
result = self._trovebox.get("/album/%s/view.json" %
|
|
||||||
self.id, **kwds)["result"]
|
|
||||||
self._replace_fields(result)
|
|
||||||
self._update_fields_with_objects()
|
|
4
trovebox/objects/__init__.py
Normal file
4
trovebox/objects/__init__.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
"""
|
||||||
|
trovebox.objects Package
|
||||||
|
Object classes returned by the API.
|
||||||
|
"""
|
73
trovebox/objects/album.py
Normal file
73
trovebox/objects/album.py
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
"""
|
||||||
|
Representation of an Album object
|
||||||
|
"""
|
||||||
|
from trovebox.errors import TroveboxError
|
||||||
|
from .trovebox_object import TroveboxObject
|
||||||
|
from .photo import Photo
|
||||||
|
|
||||||
|
class Album(TroveboxObject):
|
||||||
|
""" Representation of an Album object """
|
||||||
|
def __init__(self, trovebox, json_dict):
|
||||||
|
self.photos = None
|
||||||
|
self.cover = None
|
||||||
|
TroveboxObject.__init__(self, trovebox, json_dict)
|
||||||
|
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 isinstance(self.cover, dict):
|
||||||
|
self.cover = Photo(self._trovebox, self.cover)
|
||||||
|
# Update the photo list with photo objects
|
||||||
|
if isinstance(self.photos, list):
|
||||||
|
for i, photo in enumerate(self.photos):
|
||||||
|
if isinstance(photo, dict):
|
||||||
|
self.photos[i] = Photo(self._trovebox, photo)
|
||||||
|
|
||||||
|
def delete(self, **kwds):
|
||||||
|
"""
|
||||||
|
Delete this album.
|
||||||
|
Returns True if successful.
|
||||||
|
Raises an TroveboxError if not.
|
||||||
|
"""
|
||||||
|
result = self._trovebox.post("/album/%s/delete.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
if not result:
|
||||||
|
raise TroveboxError("Delete response returned False")
|
||||||
|
self._delete_fields()
|
||||||
|
return result
|
||||||
|
|
||||||
|
def form(self, **kwds):
|
||||||
|
""" Not implemented yet """
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def add_photos(self, photos, **kwds):
|
||||||
|
""" Not implemented yet """
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def remove_photos(self, photos, **kwds):
|
||||||
|
""" Not implemented yet """
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def update(self, **kwds):
|
||||||
|
""" Update this album with the specified parameters """
|
||||||
|
new_dict = self._trovebox.post("/album/%s/update.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
|
||||||
|
# APIv1 doesn't return the updated album (frontend issue #937)
|
||||||
|
if isinstance(new_dict, bool):
|
||||||
|
new_dict = self._trovebox.get("/album/%s/view.json" %
|
||||||
|
self.id)["result"]
|
||||||
|
|
||||||
|
self._replace_fields(new_dict)
|
||||||
|
self._update_fields_with_objects()
|
||||||
|
|
||||||
|
def view(self, **kwds):
|
||||||
|
"""
|
||||||
|
Requests the full contents of the album.
|
||||||
|
Updates the album's fields with the response.
|
||||||
|
"""
|
||||||
|
result = self._trovebox.get("/album/%s/view.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
self._replace_fields(result)
|
||||||
|
self._update_fields_with_objects()
|
96
trovebox/objects/photo.py
Normal file
96
trovebox/objects/photo.py
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
"""
|
||||||
|
Representation of a Photo object
|
||||||
|
"""
|
||||||
|
from trovebox.errors import TroveboxError
|
||||||
|
from .trovebox_object import TroveboxObject
|
||||||
|
|
||||||
|
class Photo(TroveboxObject):
|
||||||
|
""" Representation of a Photo object """
|
||||||
|
def delete(self, **kwds):
|
||||||
|
"""
|
||||||
|
Delete this photo.
|
||||||
|
Returns True if successful.
|
||||||
|
Raises an TroveboxError if not.
|
||||||
|
"""
|
||||||
|
result = self._trovebox.post("/photo/%s/delete.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
if not result:
|
||||||
|
raise TroveboxError("Delete response returned False")
|
||||||
|
self._delete_fields()
|
||||||
|
return result
|
||||||
|
|
||||||
|
def edit(self, **kwds):
|
||||||
|
""" Returns an HTML form to edit the photo """
|
||||||
|
result = self._trovebox.get("/photo/%s/edit.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
return result["markup"]
|
||||||
|
|
||||||
|
def replace(self, photo_file, **kwds):
|
||||||
|
""" Not implemented yet """
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def replace_encoded(self, photo_file, **kwds):
|
||||||
|
""" Not implemented yet """
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def update(self, **kwds):
|
||||||
|
""" Update this photo with the specified parameters """
|
||||||
|
new_dict = self._trovebox.post("/photo/%s/update.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
self._replace_fields(new_dict)
|
||||||
|
|
||||||
|
def view(self, **kwds):
|
||||||
|
"""
|
||||||
|
Used to view the photo at a particular size.
|
||||||
|
Updates the photo's fields with the response.
|
||||||
|
"""
|
||||||
|
new_dict = self._trovebox.get("/photo/%s/view.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
self._replace_fields(new_dict)
|
||||||
|
|
||||||
|
def dynamic_url(self, **kwds):
|
||||||
|
""" Not implemented yet """
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def next_previous(self, **kwds):
|
||||||
|
"""
|
||||||
|
Returns a dict containing the next and previous photo lists
|
||||||
|
(there may be more than one next/previous photo returned).
|
||||||
|
"""
|
||||||
|
result = self._trovebox.get("/photo/%s/nextprevious.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
value = {}
|
||||||
|
if "next" in result:
|
||||||
|
# Workaround for APIv1
|
||||||
|
if not isinstance(result["next"], list):
|
||||||
|
result["next"] = [result["next"]]
|
||||||
|
|
||||||
|
value["next"] = []
|
||||||
|
for photo in result["next"]:
|
||||||
|
value["next"].append(Photo(self._trovebox, photo))
|
||||||
|
|
||||||
|
if "previous" in result:
|
||||||
|
# Workaround for APIv1
|
||||||
|
if not isinstance(result["previous"], list):
|
||||||
|
result["previous"] = [result["previous"]]
|
||||||
|
|
||||||
|
value["previous"] = []
|
||||||
|
for photo in result["previous"]:
|
||||||
|
value["previous"].append(Photo(self._trovebox, photo))
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
def transform(self, **kwds):
|
||||||
|
"""
|
||||||
|
Performs transformation specified in **kwds
|
||||||
|
Example: transform(rotate=90)
|
||||||
|
"""
|
||||||
|
new_dict = self._trovebox.post("/photo/%s/transform.json" %
|
||||||
|
self.id, **kwds)["result"]
|
||||||
|
|
||||||
|
# APIv1 doesn't return the transformed photo (frontend issue #955)
|
||||||
|
if isinstance(new_dict, bool):
|
||||||
|
new_dict = self._trovebox.get("/photo/%s/view.json" %
|
||||||
|
self.id)["result"]
|
||||||
|
|
||||||
|
self._replace_fields(new_dict)
|
31
trovebox/objects/tag.py
Normal file
31
trovebox/objects/tag.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
"""
|
||||||
|
Representation of a Tag object
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
from urllib.parse import quote # Python3
|
||||||
|
except ImportError:
|
||||||
|
from urllib import quote # Python2
|
||||||
|
|
||||||
|
from trovebox.errors import TroveboxError
|
||||||
|
from .trovebox_object import TroveboxObject
|
||||||
|
|
||||||
|
class Tag(TroveboxObject):
|
||||||
|
""" Representation of a Tag object """
|
||||||
|
def delete(self, **kwds):
|
||||||
|
"""
|
||||||
|
Delete this tag.
|
||||||
|
Returns True if successful.
|
||||||
|
Raises an TroveboxError if not.
|
||||||
|
"""
|
||||||
|
result = self._trovebox.post("/tag/%s/delete.json" %
|
||||||
|
quote(self.id), **kwds)["result"]
|
||||||
|
if not result:
|
||||||
|
raise TroveboxError("Delete response returned False")
|
||||||
|
self._delete_fields()
|
||||||
|
return result
|
||||||
|
|
||||||
|
def update(self, **kwds):
|
||||||
|
""" Update this tag with the specified parameters """
|
||||||
|
new_dict = self._trovebox.post("/tag/%s/update.json" % quote(self.id),
|
||||||
|
**kwds)["result"]
|
||||||
|
self._replace_fields(new_dict)
|
50
trovebox/objects/trovebox_object.py
Normal file
50
trovebox/objects/trovebox_object.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
"""
|
||||||
|
Base object supporting the storage of custom fields as attributes
|
||||||
|
"""
|
||||||
|
class TroveboxObject(object):
|
||||||
|
""" Base object supporting the storage of custom fields as attributes """
|
||||||
|
def __init__(self, trovebox, json_dict):
|
||||||
|
self.id = None
|
||||||
|
self.name = None
|
||||||
|
self._trovebox = trovebox
|
||||||
|
self._json_dict = json_dict
|
||||||
|
self._set_fields(json_dict)
|
||||||
|
|
||||||
|
def _set_fields(self, json_dict):
|
||||||
|
""" Set this object's attributes specified in json_dict """
|
||||||
|
for key, value in json_dict.items():
|
||||||
|
if key.startswith("_"):
|
||||||
|
raise ValueError("Illegal attribute: %s" % key)
|
||||||
|
setattr(self, key, value)
|
||||||
|
|
||||||
|
def _replace_fields(self, json_dict):
|
||||||
|
"""
|
||||||
|
Delete this object's attributes, and replace with
|
||||||
|
those in json_dict.
|
||||||
|
"""
|
||||||
|
for key in self._json_dict.keys():
|
||||||
|
delattr(self, key)
|
||||||
|
self._json_dict = json_dict
|
||||||
|
self._set_fields(json_dict)
|
||||||
|
|
||||||
|
def _delete_fields(self):
|
||||||
|
"""
|
||||||
|
Delete this object's attributes, including name and id
|
||||||
|
"""
|
||||||
|
for key in self._json_dict.keys():
|
||||||
|
delattr(self, key)
|
||||||
|
self._json_dict = {}
|
||||||
|
self.id = None
|
||||||
|
self.name = None
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
if self.name is not None:
|
||||||
|
return "<%s name='%s'>" % (self.__class__, self.name)
|
||||||
|
elif self.id is not None:
|
||||||
|
return "<%s id='%s'>" % (self.__class__, self.id)
|
||||||
|
else:
|
||||||
|
return "<%s>" % (self.__class__)
|
||||||
|
|
||||||
|
def get_fields(self):
|
||||||
|
""" Returns this object's attributes """
|
||||||
|
return self._json_dict
|
Loading…
Add table
Add a link
Reference in a new issue