diff --git a/k4ever/api2/handlers.py b/k4ever/api2/handlers.py index 9353c8f..d0b3996 100644 --- a/k4ever/api2/handlers.py +++ b/k4ever/api2/handlers.py @@ -33,9 +33,7 @@ class BuyableItemHandler(BaseHandler): """ if bulkBuy: - ret = rc.NOT_IMPLEMENTED - ret.write("\nBulk buying does not support GET\n") - return ret + return getError(rc.NOT_IMPLEMENTED, "Bulk buying does not support GET") barcode = request.GET.get('barcode', None) if itemId == None: @@ -45,9 +43,7 @@ class BuyableItemHandler(BaseHandler): try: return Buyable.objects.get(barcode=barcode) except MultipleObjectsReturned: - ret = rc.DUPLICATE_ENTRY - ret.write("\nWe found more than one entry with this barcode. Bad.\n") - return ret + return getError(rc.DUPLICATE_ENTRY, "We found more than one entry with this barcode. Bad.") except Buyable.DoesNotExist: return rc.NOT_FOUND else: @@ -57,9 +53,7 @@ class BuyableItemHandler(BaseHandler): try: return Buyable.objects.get(id=itemId) except Buyable.DoesNotExist: - error = rc.NOT_FOUND - error.write("This buyable does not exist in our database") - return error + return getError(rc.NOT_FOUND, "This buyable does not exist in our database") @manglePluginPerms def create(self, request, itemId=None, bulkBuy=False): @@ -68,7 +62,7 @@ class BuyableItemHandler(BaseHandler): if not request.content_type: request.data = request.POST - + if bulkBuy: return self.bulkBuy(request) else: @@ -94,9 +88,7 @@ class BuyableItemHandler(BaseHandler): if amount < 1: return rc.BAD_REQUEST if amount > 30: - ret = rc.BAD_REQUEST - ret.write("\nYou 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\n"); - return ret + 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 @@ -125,15 +117,11 @@ class BuyableItemHandler(BaseHandler): - deposits: List of items to buy deposit for. """ if not request.content_type: - ret = rc.BAD_REQUEST - ret.write("\nThe content-type of the request must not be empty/urlencoded\n") - return ret - + return getError(rc.BAD_REQUEST, "The content-type of the request must not be empty/urlencoded") + if not request.data.has_key("items") and not request.data.has_key("deposits"): - ret = rc.BAD_REQUEST - ret.write("\nYou need to specify either items or deposits (or both).\n") - return ret - + return getError(rc.BAD_REQUEST, "You need to specify either items or deposits (or both).") + if not request.data.has_key("items"): request.data['items'] = [] if not request.data.has_key("deposits"): @@ -149,20 +137,13 @@ class BuyableItemHandler(BaseHandler): raise TypeError() itemList += request.data['deposits'] except TypeError: - ret = rc.BAD_REQUEST - ret.write("\nThe items/deposists parameter have to be a list.\n") - return ret + return getError(rc.BAD_REQUEST, "The items/deposists parameter have to be a list.") if len(itemList) > 30: - ret = rc.BAD_REQUEST - ret.write("\nYou 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\n"); - return ret + 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 len(itemList) == 0: - ret = rc.BAD_REQUEST - ret.write("\nYour request contains no items/deposits.\n") - return ret - + return getError(rc.BAD_REQUEST, "Your request contains no items/deposits.") ids = {} for item in itemList: @@ -174,13 +155,9 @@ class BuyableItemHandler(BaseHandler): ret.write("\nThe item with the id '%s' could not be found\n" % (item,)) return ret except ValueError: - ret = rc.NOT_FOUND - ret.write("\nItem ids should be numeric (and preferably integers)\n") - return ret + return getError(rc.NOT_FOUND, "Item ids should be numeric (and preferably integers)") if item in request.data['deposits'] and not ids[item].hasDeposit(): - ret = rc.BAD_REQUEST - ret.write("\nItem '%s' cant be bought with deposit\n" % (item,)) - return ret + return getError(rc.BAD_REQUEST, "Item '%s' cant be bought with deposit" % (item,)) order = Order() order.create(request.user) @@ -261,20 +238,20 @@ class TransactionTransactHandler(BaseHandler): - amount: [req] Amount to add to the user's account - type: [req] Type of transaction (id) """ - amount = getDecimal(request.POST, 'amount', Decimal(0)) - tTypeId = getInt(request.POST, 'type', -1) + + if not request.content_type: + request.data = request.POST + + amount = getDecimal(request.data, 'amount', Decimal(0)) + tTypeId = getInt(request.data, 'type', -1) if amount < Decimal("0.01"): - ret = rc.BAD_REQUEST - ret.write("\nA negative amount (or zeroed) is not supported right now (there has not been put enough thought into the 'lending money' process\n") - return ret + return getError(rc.BAD_REQUEST, "A negative amount (or zeroed) is not supported right now (there has not been put enough thought into the 'lending money' process") tType = None try: tType = TransactionType.objects.get(id=tTypeId) except TransactionType.DoesNotExist: - ret = rc.BAD_REQUEST - ret.write("\nYour TransactionType could not be found\n") - return ret + return getError(rc.BAD_REQUEST, "Your TransactionType could not be found") trans = Transaction() trans.user = request.user trans.transactionType = tType @@ -326,24 +303,18 @@ class TransactionVirtualHandler(BaseHandler): amount = getDecimal(request.data, 'amount', Decimal(0)) if amount < Decimal("0.01"): - ret = rc.BAD_REQUEST - ret.write("\nYou can't transact negatives amount to another users account.\n") - return ret + return getError(rc.BAD_REQUEST, "You can't transact negatives amount to another users account.") comment = request.data.get('comment', None) if not comment: - ret = rc.BAD_REQUEST - ret.write("\nPlease supply a comment for the transaction\n") - return ret + return getError(rc.BAD_REQUEST, "Please supply a comment for the transaction") recipientStr = request.data.get('recipient', None) recipient = None try: recipient = User.objects.get(username=recipientStr) except User.DoesNotExist: - ret = rc.BAD_REQUEST - ret.write("\nThe recipient user does not exist.\n") - return ret + return getError(rc.BAD_REQUEST, "The recipient user does not exist.") trans = VirtualTransaction(user=request.user, recipient=recipient, amount=amount, comment=comment) trans.save() return rc.ALL_OK @@ -377,9 +348,7 @@ class AuthBlobHandler(BaseHandler): :attr:`main.models.Plugin.pluginCanReadAuthblob` to be true. """ if not request.plugin.pluginCanReadAuthblob: - ret = rc.FORBIDDEN - ret.write("\nThis plugin is not allowed to read the user's authblob\n") - return ret + return getError(rc.FORBIDDEN, "This plugin is not allowed to read the user's authblob") return {'authblob': request.pluginperms.authblob} @requirePlugin @@ -396,12 +365,9 @@ class AuthBlobHandler(BaseHandler): """ if not request.plugin.pluginCanWriteAuthblob: - ret = rc.FORBIDDEN - ret.write("\nThis plugin is not allowed to write the user's authblob\n") - return ret + return getError(rc.FORBIDDEN, "This plugin is not allowed to write the user's authblob") if not request.data.has_key('authblob'): - ret = rc.BAD_REQUEST - ret.write("\nTo change the user's auth blob you actually need to provide one\n") + return getError(rc.BAD_REQUEST, "To change the user's auth blob you actually need to provide one") request.pluginperms.authblob = request.data['authblob'] request.pluginperms.authblob.save() @@ -425,9 +391,7 @@ class AuthUserHandler(BaseHandler): - authblob: [required] Authblob to search """ if not request.plugin.uniqueAuthblob: - ret = rc.BAD_REQUEST - ret.write("\nThis plugin does not support unique auth blobs, therefore we can't identify a user uniquely by their authblob\n") - return ret + 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'): return rc.BAD_REQUEST diff --git a/k4ever/api2/helper.py b/k4ever/api2/helper.py index 832bbb1..5b95ccf 100644 --- a/k4ever/api2/helper.py +++ b/k4ever/api2/helper.py @@ -13,3 +13,9 @@ def getDecimal(d, key, default): return Decimal(d.get(key, default)) except InvalidOperation: return default + +def getError(err, msg): + """ Get an error, write a message on it an return it. """ + err.write("\n%s\n" % (msg,)) + return err +