API: Code cleanup

master
Sebastian Lohff 12 years ago
parent 44c1d2c714
commit 018332e16d

@ -92,18 +92,18 @@ class BuyableItemHandler(BaseHandler):
try:
item = Buyable.objects.get(id=itemId)
except Buyable.DoesNotExist:
return rc.NOT_FOUND
return getError(rc.NOT_FOUND, "A Buyable with that id does not exist.")
# parse post data
deposit = getInt(request.data, 'deposit', self.BUY_ITEM)
amount = getInt(request.data, 'amount', 1)
if amount < 1:
return rc.BAD_REQUEST
return getError(rc.BAD_REQUEST, "Buying zero or negative amounts is not supported.")
if amount > 30:
return getError(rc.BAD_REQUEST, "You are trying to buy more than 30 items at once. This is not permitted. If you think it should, mail the admins / fix this in the handlers.py")
if (not item.hasDeposit() and deposit != self.BUY_ITEM) or \
deposit not in (self.BUY_ITEM, self.BUY_DEPOSIT, self.BUY_ITEM_AND_DEPOSIT):
return rc.BAD_REQUEST
return getError(rc.BAD_REQUEST, "Either this item has no deposit or you specified an invalid deposit buy mode.")
order = Order(user=request.user)
order.save()
@ -161,9 +161,7 @@ class BuyableItemHandler(BaseHandler):
try:
ids[item] = Buyable.objects.get(id=item)
except Buyable.DoesNotExist:
ret = rc.NOT_FOUND
ret.write("\nThe item with the id '%s' could not be found\n" % (item,))
return ret
return getError(rc.NOT_FOUND, "The item with the id '%s' could not be found" % (item,))
except ValueError:
return getError(rc.NOT_FOUND, "Item ids should be numeric (and preferably integers)")
if item in request.data['deposits'] and not ids[item].hasDeposit():
@ -227,22 +225,16 @@ class ImgThumbHandler(BaseHandler):
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
return getError(rc.BAD_REQUEST, "Something is seriously broken, django urls SHOULD have parsed out the non-number thingie.")
if thumbSize not in THUMB_SIZES:
ret = rc.BAD_REQUEST
ret.write("\nThe requested thumbnailsize is not available\n")
return ret
return getError(rc.BAD_REQUEST, "The requested thumbnailsize is not available.")
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
return getError(rc.NOT_FOUND, "The item with the id '%s' could not be found" % (itemId,))
thumbnail_options = dict(size=thumbSize)
thumb = get_thumbnailer(item.image).get_thumbnail(thumbnail_options)
@ -268,7 +260,7 @@ class TransactionTransactHandler(BaseHandler):
"""
num = getInt(request.GET, 'num', 0)
if num < 0:
return rc.BAD_REQUEST
return getError(rc.BAD_REQUEST, "Please supply a positive number of entries.")
userTrans = Transaction.objects.filter(user=request.user).order_by("-dateTime")
if num > 0:
@ -323,7 +315,7 @@ class TransactionVirtualHandler(BaseHandler):
"""
num = getInt(request.GET, 'num', 0)
if num < 0:
return rc.BAD_REQUEST
return getError(rc.BAD_REQUEST, "Please supply a positive number of entries.")
userTrans = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime")
if num > 0:
@ -433,12 +425,12 @@ class AuthUserHandler(BaseHandler):
return getError(rc.BAD_REQUEST, "This plugin does not support unique auth blobs, therefore we can't identify a user uniquely by their authblob")
if not request.GET.has_key('authblob') or request.GET['authblob'] == '':
return rc.BAD_REQUEST
return getError(rc.BAD_REQUEST, "Authblob was empty.")
user = getUserFromAuthblob(request.GET['authblob'], request.plugin)
if user:
return user
return rc.NOT_FOUND
return getError(rc.NOT_FOUND, "A user with that authblob could not be found.")
class ConfigHandler(BaseHandler):
""" Handler for API configuration values

Loading…
Cancel
Save