diff --git a/k4ever/api2/handlers.py b/k4ever/api2/handlers.py index 9a14e7a..5d82d9b 100644 --- a/k4ever/api2/handlers.py +++ b/k4ever/api2/handlers.py @@ -1,3 +1,4 @@ +from easy_thumbnails.files import get_thumbnailer from piston.handler import BaseHandler from piston.utils import rc from k4ever.buyable.models import * @@ -13,6 +14,9 @@ from helper import * from django.db.models import Q import datetime +# Available image sizes for thumbnails +THUMB_SIZES = [(48,48), (64,64), (100,100), (150,150), (220, 220), (330, 330), (470, 470), (680, 680)] + class BuyableItemHandler(BaseHandler): """Handler responsible for getting and buying items.""" @@ -172,6 +176,7 @@ class BuyableItemHandler(BaseHandler): return rc.CREATED + class BuyableTypeHandler(BaseHandler): """Handler for listing all :class:`BuyableType `. @@ -198,7 +203,44 @@ class HistoryHandler(BaseHandler): if num > 0: return qset[:num] return qset - + +class ImgSizesHandler(BaseHandler): + """ Handler for listing all available tumbnailsizes """ + allowed_methods = ('GET',) + + def read(self, request): + return THUMB_SIZES + +class ImgThumbHandler(BaseHandler): + """ Handler providing access to thumbnails for images of buyables """ + allowed_methods = ('GET',) + + def read(self, request, itemId, xSize, ySize): + thumbSize = None + try: + thumbSize = (int(xSize), int(ySize)) + except ValueError: + ret = rc.BAD_REQUEST + ret.write("\nSomething is seriously broken, django urls SHOULD have parsed out the non-number thingies\n") + return ret + + if thumbSize not in THUMB_SIZES: + ret = rc.BAD_REQUEST + ret.write("\nThe requested thumbnailsize is not available\n") + return ret + + item = None + try: + item = Buyable.objects.get(id=itemId) + except Buyable.DoesNotExist: + ret = rc.NOT_FOUND + ret.write("The item with the id '%s' could not be found\n" % (itemId,)) + return ret + + thumbnail_options = dict(size=thumbSize) + thumb = get_thumbnailer(item.image).get_thumbnail(thumbnail_options) + return thumb._get_url() + class TransactionTransactHandler(BaseHandler): """Handler for transaction. diff --git a/k4ever/api2/urls.py b/k4ever/api2/urls.py index 2d8ce8f..49508d8 100644 --- a/k4ever/api2/urls.py +++ b/k4ever/api2/urls.py @@ -29,13 +29,14 @@ ad = {'authentication': multiAuth} buyableItemRes = CsrfExemptResource(handler=BuyableItemHandler, **ad) buyableTypeRes = CsrfExemptResource(handler=BuyableTypeHandler, **ad) historyRes = CsrfExemptResource(handler=HistoryHandler, **ad) +imgSizesRes = CsrfExemptResource(handler=ImgSizesHandler, **ad) +imgThumbRes = CsrfExemptResource(handler=ImgThumbHandler, **ad) transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad) transactionTypeRes = CsrfExemptResource(handler=TransactionTypeHandler, **ad) transactionVirtualRes = CsrfExemptResource(handler=TransactionVirtualHandler, **ad) accountBalanceRes = CsrfExemptResource(handler=AccountBalanceHandler, **ad) - authBlobRes = CsrfExemptResource(handler=AuthBlobHandler, **ad) authUserRes = CsrfExemptResource(handler=AuthUserHandler, **ad) configRes = CsrfExemptResource(handler=ConfigHandler, **ad) @@ -47,6 +48,8 @@ urlpatterns = patterns('', url(r'buyable/item/bulkbuy/?$', buyableItemRes, {'bulkBuy': True}), url(r'buyable/types/?$', buyableTypeRes), url(r'buyable/history/?$', historyRes), + url(r'buyable/img/sizes/?$', imgSizesRes), + url(r'buyable/img/thumb/(?P\d+)/(?P\d+)x(?P\d+)/?$', imgThumbRes), url(r'account/transactions/transact/?$', transactionTransactRes), url(r'account/transactions/types/?$', transactionTypeRes), diff --git a/k4ever/docs/django/api.rst b/k4ever/docs/django/api.rst index 7099880..39e9d8e 100644 --- a/k4ever/docs/django/api.rst +++ b/k4ever/docs/django/api.rst @@ -98,6 +98,18 @@ to the handler or the responsible method for more documentation or code. num Number of entries ========= ================= + **img/** + :class:`sizes/ ` + :func:`GET ` + List all available sizes for thumbnails + + :class:`thumb/[itemId]/[xSize]x[ySize]/ ` + :func:`GET ` + Returns the url to a thumbnail for [itemId] of size[xSize]x[ySize]. + E.g. thumb/1/64x64/ returns the path to an image for the first item + and a dimension of 64x64. If the size is not available an error + is raised. + **account/** **transactions/** :class:`transact/ `