Added thumbnailing functions to api, fixes #17

This commit is contained in:
Sebastian Lohff 2012-01-07 01:37:24 +01:00
parent b7a96cb390
commit 7bf15a1470
3 changed files with 59 additions and 2 deletions

View File

@ -1,3 +1,4 @@
from easy_thumbnails.files import get_thumbnailer
from piston.handler import BaseHandler from piston.handler import BaseHandler
from piston.utils import rc from piston.utils import rc
from k4ever.buyable.models import * from k4ever.buyable.models import *
@ -13,6 +14,9 @@ from helper import *
from django.db.models import Q from django.db.models import Q
import datetime 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): class BuyableItemHandler(BaseHandler):
"""Handler responsible for getting and buying items.""" """Handler responsible for getting and buying items."""
@ -172,6 +176,7 @@ class BuyableItemHandler(BaseHandler):
return rc.CREATED return rc.CREATED
class BuyableTypeHandler(BaseHandler): class BuyableTypeHandler(BaseHandler):
"""Handler for listing all :class:`BuyableType <buyable.models.BuyableType>`. """Handler for listing all :class:`BuyableType <buyable.models.BuyableType>`.
@ -199,6 +204,43 @@ class HistoryHandler(BaseHandler):
return qset[:num] return qset[:num]
return qset 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): class TransactionTransactHandler(BaseHandler):
"""Handler for transaction. """Handler for transaction.

View File

@ -29,13 +29,14 @@ ad = {'authentication': multiAuth}
buyableItemRes = CsrfExemptResource(handler=BuyableItemHandler, **ad) buyableItemRes = CsrfExemptResource(handler=BuyableItemHandler, **ad)
buyableTypeRes = CsrfExemptResource(handler=BuyableTypeHandler, **ad) buyableTypeRes = CsrfExemptResource(handler=BuyableTypeHandler, **ad)
historyRes = CsrfExemptResource(handler=HistoryHandler, **ad) historyRes = CsrfExemptResource(handler=HistoryHandler, **ad)
imgSizesRes = CsrfExemptResource(handler=ImgSizesHandler, **ad)
imgThumbRes = CsrfExemptResource(handler=ImgThumbHandler, **ad)
transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad) transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad)
transactionTypeRes = CsrfExemptResource(handler=TransactionTypeHandler, **ad) transactionTypeRes = CsrfExemptResource(handler=TransactionTypeHandler, **ad)
transactionVirtualRes = CsrfExemptResource(handler=TransactionVirtualHandler, **ad) transactionVirtualRes = CsrfExemptResource(handler=TransactionVirtualHandler, **ad)
accountBalanceRes = CsrfExemptResource(handler=AccountBalanceHandler, **ad) accountBalanceRes = CsrfExemptResource(handler=AccountBalanceHandler, **ad)
authBlobRes = CsrfExemptResource(handler=AuthBlobHandler, **ad) authBlobRes = CsrfExemptResource(handler=AuthBlobHandler, **ad)
authUserRes = CsrfExemptResource(handler=AuthUserHandler, **ad) authUserRes = CsrfExemptResource(handler=AuthUserHandler, **ad)
configRes = CsrfExemptResource(handler=ConfigHandler, **ad) configRes = CsrfExemptResource(handler=ConfigHandler, **ad)
@ -47,6 +48,8 @@ urlpatterns = patterns('',
url(r'buyable/item/bulkbuy/?$', buyableItemRes, {'bulkBuy': True}), url(r'buyable/item/bulkbuy/?$', buyableItemRes, {'bulkBuy': True}),
url(r'buyable/types/?$', buyableTypeRes), url(r'buyable/types/?$', buyableTypeRes),
url(r'buyable/history/?$', historyRes), url(r'buyable/history/?$', historyRes),
url(r'buyable/img/sizes/?$', imgSizesRes),
url(r'buyable/img/thumb/(?P<itemId>\d+)/(?P<xSize>\d+)x(?P<ySize>\d+)/?$', imgThumbRes),
url(r'account/transactions/transact/?$', transactionTransactRes), url(r'account/transactions/transact/?$', transactionTransactRes),
url(r'account/transactions/types/?$', transactionTypeRes), url(r'account/transactions/types/?$', transactionTypeRes),

View File

@ -98,6 +98,18 @@ to the handler or the responsible method for more documentation or code.
num Number of entries num Number of entries
========= ================= ========= =================
**img/**
:class:`sizes/ <api2.handlers.ImgSizesHandler>`
:func:`GET <api2.handlers.ImgSizeHandler.read>`
List all available sizes for thumbnails
:class:`thumb/[itemId]/[xSize]x[ySize]/ <api2.handlers.ImgThumbHandler>`
:func:`GET <api2.handlers.ImgThumbHandler.read>`
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/** **account/**
**transactions/** **transactions/**
:class:`transact/ <api2.handlers.TransactionTransactHandler>` :class:`transact/ <api2.handlers.TransactionTransactHandler>`