Browse Source

Added thumbnailing functions to api, fixes #17

Sebastian Lohff 10 years ago
parent
commit
7bf15a1470
3 changed files with 59 additions and 2 deletions
  1. 43
    1
      k4ever/api2/handlers.py
  2. 4
    1
      k4ever/api2/urls.py
  3. 12
    0
      k4ever/docs/django/api.rst

+ 43
- 1
k4ever/api2/handlers.py View File

@@ -1,3 +1,4 @@
1
+from easy_thumbnails.files import get_thumbnailer
1 2
 from piston.handler import BaseHandler
2 3
 from piston.utils import rc
3 4
 from k4ever.buyable.models import *
@@ -13,6 +14,9 @@ from helper import *
13 14
 from django.db.models import Q
14 15
 import datetime
15 16
 
17
+# Available image sizes for thumbnails
18
+THUMB_SIZES = [(48,48), (64,64), (100,100), (150,150), (220, 220), (330, 330), (470, 470), (680, 680)]
19
+
16 20
 class BuyableItemHandler(BaseHandler):
17 21
 	"""Handler responsible for getting and buying items."""
18 22
 	
@@ -172,6 +176,7 @@ class BuyableItemHandler(BaseHandler):
172 176
 		
173 177
 		return rc.CREATED
174 178
 
179
+
175 180
 class BuyableTypeHandler(BaseHandler):
176 181
 	"""Handler for listing all :class:`BuyableType <buyable.models.BuyableType>`.
177 182
 
@@ -198,7 +203,44 @@ class HistoryHandler(BaseHandler):
198 203
 		if num > 0:
199 204
 			return qset[:num]
200 205
 		return qset
201
-			
206
+
207
+class ImgSizesHandler(BaseHandler):
208
+	""" Handler for listing all available tumbnailsizes """
209
+	allowed_methods = ('GET',)
210
+	
211
+	def read(self, request):
212
+		return THUMB_SIZES
213
+
214
+class ImgThumbHandler(BaseHandler):
215
+	""" Handler providing access to thumbnails for images of buyables """
216
+	allowed_methods = ('GET',)
217
+	
218
+	def read(self, request, itemId, xSize, ySize):
219
+		thumbSize = None
220
+		try:
221
+			thumbSize = (int(xSize), int(ySize))
222
+		except ValueError:
223
+			ret = rc.BAD_REQUEST
224
+			ret.write("\nSomething is seriously broken, django urls SHOULD have parsed out the non-number thingies\n")
225
+			return ret
226
+
227
+		if thumbSize not in THUMB_SIZES:
228
+			ret = rc.BAD_REQUEST
229
+			ret.write("\nThe requested thumbnailsize is not available\n")
230
+			return ret
231
+
232
+		item = None
233
+		try:
234
+			item = Buyable.objects.get(id=itemId)
235
+		except Buyable.DoesNotExist:
236
+			ret = rc.NOT_FOUND
237
+			ret.write("The item with the id '%s' could not be found\n" % (itemId,))
238
+			return ret
239
+		
240
+		thumbnail_options = dict(size=thumbSize)
241
+		thumb = get_thumbnailer(item.image).get_thumbnail(thumbnail_options)
242
+		return thumb._get_url()
243
+
202 244
 
203 245
 class TransactionTransactHandler(BaseHandler):
204 246
 	"""Handler for transaction.

+ 4
- 1
k4ever/api2/urls.py View File

@@ -29,13 +29,14 @@ ad = {'authentication': multiAuth}
29 29
 buyableItemRes    = CsrfExemptResource(handler=BuyableItemHandler,     **ad)
30 30
 buyableTypeRes    = CsrfExemptResource(handler=BuyableTypeHandler,     **ad)
31 31
 historyRes = CsrfExemptResource(handler=HistoryHandler,         **ad)
32
+imgSizesRes  = CsrfExemptResource(handler=ImgSizesHandler, **ad)
33
+imgThumbRes = CsrfExemptResource(handler=ImgThumbHandler, **ad)
32 34
 
33 35
 transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad)
34 36
 transactionTypeRes     = CsrfExemptResource(handler=TransactionTypeHandler, **ad)
35 37
 transactionVirtualRes  = CsrfExemptResource(handler=TransactionVirtualHandler, **ad)
36 38
 accountBalanceRes      = CsrfExemptResource(handler=AccountBalanceHandler, **ad)
37 39
 
38
-
39 40
 authBlobRes = CsrfExemptResource(handler=AuthBlobHandler, **ad)
40 41
 authUserRes = CsrfExemptResource(handler=AuthUserHandler, **ad)
41 42
 configRes   = CsrfExemptResource(handler=ConfigHandler, **ad)
@@ -47,6 +48,8 @@ urlpatterns = patterns('',
47 48
 	url(r'buyable/item/bulkbuy/?$', buyableItemRes, {'bulkBuy': True}),
48 49
 	url(r'buyable/types/?$', buyableTypeRes),
49 50
 	url(r'buyable/history/?$', historyRes),
51
+	url(r'buyable/img/sizes/?$', imgSizesRes),
52
+	url(r'buyable/img/thumb/(?P<itemId>\d+)/(?P<xSize>\d+)x(?P<ySize>\d+)/?$', imgThumbRes),
50 53
 	
51 54
 	url(r'account/transactions/transact/?$', transactionTransactRes),
52 55
 	url(r'account/transactions/types/?$', transactionTypeRes),

+ 12
- 0
k4ever/docs/django/api.rst View File

@@ -98,6 +98,18 @@ to the handler or the responsible method for more documentation or code.
98 98
    		num			Number of entries
99 99
 		=========	=================
100 100
 
101
+  **img/**
102
+	:class:`sizes/ <api2.handlers.ImgSizesHandler>`
103
+   		:func:`GET <api2.handlers.ImgSizeHandler.read>`
104
+			List all available sizes for thumbnails
105
+
106
+	:class:`thumb/[itemId]/[xSize]x[ySize]/ <api2.handlers.ImgThumbHandler>`
107
+   		:func:`GET <api2.handlers.ImgThumbHandler.read>`
108
+			Returns the url to a thumbnail for [itemId] of size[xSize]x[ySize].
109
+			E.g. thumb/1/64x64/ returns the path to an image for the first item
110
+			and a dimension of 64x64. If the size is not available an error
111
+			is raised.
112
+
101 113
 **account/**
102 114
   **transactions/**
103 115
 	:class:`transact/ <api2.handlers.TransactionTransactHandler>`

Loading…
Cancel
Save