API: Code cleanup

This commit is contained in:
Sebastian Lohff 2012-01-21 16:49:55 +01:00
parent 44c1d2c714
commit 018332e16d
1 changed files with 11 additions and 19 deletions

View File

@ -92,18 +92,18 @@ class BuyableItemHandler(BaseHandler):
try: try:
item = Buyable.objects.get(id=itemId) item = Buyable.objects.get(id=itemId)
except Buyable.DoesNotExist: except Buyable.DoesNotExist:
return rc.NOT_FOUND return getError(rc.NOT_FOUND, "A Buyable with that id does not exist.")
# parse post data # parse post data
deposit = getInt(request.data, 'deposit', self.BUY_ITEM) deposit = getInt(request.data, 'deposit', self.BUY_ITEM)
amount = getInt(request.data, 'amount', 1) amount = getInt(request.data, 'amount', 1)
if 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: 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") 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 \ if (not item.hasDeposit() and deposit != self.BUY_ITEM) or \
deposit not in (self.BUY_ITEM, self.BUY_DEPOSIT, self.BUY_ITEM_AND_DEPOSIT): 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 = Order(user=request.user)
order.save() order.save()
@ -161,9 +161,7 @@ class BuyableItemHandler(BaseHandler):
try: try:
ids[item] = Buyable.objects.get(id=item) ids[item] = Buyable.objects.get(id=item)
except Buyable.DoesNotExist: except Buyable.DoesNotExist:
ret = rc.NOT_FOUND return getError(rc.NOT_FOUND, "The item with the id '%s' could not be found" % (item,))
ret.write("\nThe item with the id '%s' could not be found\n" % (item,))
return ret
except ValueError: except ValueError:
return getError(rc.NOT_FOUND, "Item ids should be numeric (and preferably integers)") return getError(rc.NOT_FOUND, "Item ids should be numeric (and preferably integers)")
if item in request.data['deposits'] and not ids[item].hasDeposit(): if item in request.data['deposits'] and not ids[item].hasDeposit():
@ -227,22 +225,16 @@ class ImgThumbHandler(BaseHandler):
try: try:
thumbSize = (int(xSize), int(ySize)) thumbSize = (int(xSize), int(ySize))
except ValueError: except ValueError:
ret = rc.BAD_REQUEST return getError(rc.BAD_REQUEST, "Something is seriously broken, django urls SHOULD have parsed out the non-number thingie.")
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: if thumbSize not in THUMB_SIZES:
ret = rc.BAD_REQUEST return getError(rc.BAD_REQUEST, "The requested thumbnailsize is not available.")
ret.write("\nThe requested thumbnailsize is not available\n")
return ret
item = None item = None
try: try:
item = Buyable.objects.get(id=itemId) item = Buyable.objects.get(id=itemId)
except Buyable.DoesNotExist: except Buyable.DoesNotExist:
ret = rc.NOT_FOUND return getError(rc.NOT_FOUND, "The item with the id '%s' could not be found" % (itemId,))
ret.write("The item with the id '%s' could not be found\n" % (itemId,))
return ret
thumbnail_options = dict(size=thumbSize) thumbnail_options = dict(size=thumbSize)
thumb = get_thumbnailer(item.image).get_thumbnail(thumbnail_options) thumb = get_thumbnailer(item.image).get_thumbnail(thumbnail_options)
@ -268,7 +260,7 @@ class TransactionTransactHandler(BaseHandler):
""" """
num = getInt(request.GET, 'num', 0) num = getInt(request.GET, 'num', 0)
if 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") userTrans = Transaction.objects.filter(user=request.user).order_by("-dateTime")
if num > 0: if num > 0:
@ -323,7 +315,7 @@ class TransactionVirtualHandler(BaseHandler):
""" """
num = getInt(request.GET, 'num', 0) num = getInt(request.GET, 'num', 0)
if 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") userTrans = VirtualTransaction.objects.filter(Q(user=request.user) | Q(recipient=request.user)).order_by("-dateTime")
if num > 0: 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") 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'] == '': 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) user = getUserFromAuthblob(request.GET['authblob'], request.plugin)
if user: if user:
return user return user
return rc.NOT_FOUND return getError(rc.NOT_FOUND, "A user with that authblob could not be found.")
class ConfigHandler(BaseHandler): class ConfigHandler(BaseHandler):
""" Handler for API configuration values """ Handler for API configuration values