API refactoring, introduced getError
This commit is contained in:
parent
5dc8770783
commit
df32875789
|
@ -33,9 +33,7 @@ class BuyableItemHandler(BaseHandler):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if bulkBuy:
|
if bulkBuy:
|
||||||
ret = rc.NOT_IMPLEMENTED
|
return getError(rc.NOT_IMPLEMENTED, "Bulk buying does not support GET")
|
||||||
ret.write("\nBulk buying does not support GET\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
barcode = request.GET.get('barcode', None)
|
barcode = request.GET.get('barcode', None)
|
||||||
if itemId == None:
|
if itemId == None:
|
||||||
|
@ -45,9 +43,7 @@ class BuyableItemHandler(BaseHandler):
|
||||||
try:
|
try:
|
||||||
return Buyable.objects.get(barcode=barcode)
|
return Buyable.objects.get(barcode=barcode)
|
||||||
except MultipleObjectsReturned:
|
except MultipleObjectsReturned:
|
||||||
ret = rc.DUPLICATE_ENTRY
|
return getError(rc.DUPLICATE_ENTRY, "We found more than one entry with this barcode. Bad.")
|
||||||
ret.write("\nWe found more than one entry with this barcode. Bad.\n")
|
|
||||||
return ret
|
|
||||||
except Buyable.DoesNotExist:
|
except Buyable.DoesNotExist:
|
||||||
return rc.NOT_FOUND
|
return rc.NOT_FOUND
|
||||||
else:
|
else:
|
||||||
|
@ -57,9 +53,7 @@ class BuyableItemHandler(BaseHandler):
|
||||||
try:
|
try:
|
||||||
return Buyable.objects.get(id=itemId)
|
return Buyable.objects.get(id=itemId)
|
||||||
except Buyable.DoesNotExist:
|
except Buyable.DoesNotExist:
|
||||||
error = rc.NOT_FOUND
|
return getError(rc.NOT_FOUND, "This buyable does not exist in our database")
|
||||||
error.write("This buyable does not exist in our database")
|
|
||||||
return error
|
|
||||||
|
|
||||||
@manglePluginPerms
|
@manglePluginPerms
|
||||||
def create(self, request, itemId=None, bulkBuy=False):
|
def create(self, request, itemId=None, bulkBuy=False):
|
||||||
|
@ -94,9 +88,7 @@ class BuyableItemHandler(BaseHandler):
|
||||||
if amount < 1:
|
if amount < 1:
|
||||||
return rc.BAD_REQUEST
|
return rc.BAD_REQUEST
|
||||||
if amount > 30:
|
if amount > 30:
|
||||||
ret = rc.BAD_REQUEST
|
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")
|
||||||
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
|
|
||||||
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 rc.BAD_REQUEST
|
||||||
|
@ -125,14 +117,10 @@ class BuyableItemHandler(BaseHandler):
|
||||||
- deposits: List of items to buy deposit for.
|
- deposits: List of items to buy deposit for.
|
||||||
"""
|
"""
|
||||||
if not request.content_type:
|
if not request.content_type:
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "The content-type of the request must not be empty/urlencoded")
|
||||||
ret.write("\nThe content-type of the request must not be empty/urlencoded\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
if not request.data.has_key("items") and not request.data.has_key("deposits"):
|
if not request.data.has_key("items") and not request.data.has_key("deposits"):
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "You need to specify either items or deposits (or both).")
|
||||||
ret.write("\nYou need to specify either items or deposits (or both).\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
if not request.data.has_key("items"):
|
if not request.data.has_key("items"):
|
||||||
request.data['items'] = []
|
request.data['items'] = []
|
||||||
|
@ -149,20 +137,13 @@ class BuyableItemHandler(BaseHandler):
|
||||||
raise TypeError()
|
raise TypeError()
|
||||||
itemList += request.data['deposits']
|
itemList += request.data['deposits']
|
||||||
except TypeError:
|
except TypeError:
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "The items/deposists parameter have to be a list.")
|
||||||
ret.write("\nThe items/deposists parameter have to be a list.\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
if len(itemList) > 30:
|
if len(itemList) > 30:
|
||||||
ret = rc.BAD_REQUEST
|
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")
|
||||||
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
|
|
||||||
|
|
||||||
if len(itemList) == 0:
|
if len(itemList) == 0:
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "Your request contains no items/deposits.")
|
||||||
ret.write("\nYour request contains no items/deposits.\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
|
|
||||||
ids = {}
|
ids = {}
|
||||||
for item in itemList:
|
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,))
|
ret.write("\nThe item with the id '%s' could not be found\n" % (item,))
|
||||||
return ret
|
return ret
|
||||||
except ValueError:
|
except ValueError:
|
||||||
ret = rc.NOT_FOUND
|
return getError(rc.NOT_FOUND, "Item ids should be numeric (and preferably integers)")
|
||||||
ret.write("\nItem ids should be numeric (and preferably integers)\n")
|
|
||||||
return ret
|
|
||||||
if item in request.data['deposits'] and not ids[item].hasDeposit():
|
if item in request.data['deposits'] and not ids[item].hasDeposit():
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "Item '%s' cant be bought with deposit" % (item,))
|
||||||
ret.write("\nItem '%s' cant be bought with deposit\n" % (item,))
|
|
||||||
return ret
|
|
||||||
|
|
||||||
order = Order()
|
order = Order()
|
||||||
order.create(request.user)
|
order.create(request.user)
|
||||||
|
@ -261,20 +238,20 @@ class TransactionTransactHandler(BaseHandler):
|
||||||
- amount: [req] Amount to add to the user's account
|
- amount: [req] Amount to add to the user's account
|
||||||
- type: [req] Type of transaction (id)
|
- 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"):
|
if amount < Decimal("0.01"):
|
||||||
ret = rc.BAD_REQUEST
|
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")
|
||||||
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
|
|
||||||
tType = None
|
tType = None
|
||||||
try:
|
try:
|
||||||
tType = TransactionType.objects.get(id=tTypeId)
|
tType = TransactionType.objects.get(id=tTypeId)
|
||||||
except TransactionType.DoesNotExist:
|
except TransactionType.DoesNotExist:
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "Your TransactionType could not be found")
|
||||||
ret.write("\nYour TransactionType could not be found\n")
|
|
||||||
return ret
|
|
||||||
trans = Transaction()
|
trans = Transaction()
|
||||||
trans.user = request.user
|
trans.user = request.user
|
||||||
trans.transactionType = tType
|
trans.transactionType = tType
|
||||||
|
@ -326,24 +303,18 @@ class TransactionVirtualHandler(BaseHandler):
|
||||||
amount = getDecimal(request.data, 'amount', Decimal(0))
|
amount = getDecimal(request.data, 'amount', Decimal(0))
|
||||||
|
|
||||||
if amount < Decimal("0.01"):
|
if amount < Decimal("0.01"):
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "You can't transact negatives amount to another users account.")
|
||||||
ret.write("\nYou can't transact negatives amount to another users account.\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
comment = request.data.get('comment', None)
|
comment = request.data.get('comment', None)
|
||||||
if not comment:
|
if not comment:
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "Please supply a comment for the transaction")
|
||||||
ret.write("\nPlease supply a comment for the transaction\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
recipientStr = request.data.get('recipient', None)
|
recipientStr = request.data.get('recipient', None)
|
||||||
recipient = None
|
recipient = None
|
||||||
try:
|
try:
|
||||||
recipient = User.objects.get(username=recipientStr)
|
recipient = User.objects.get(username=recipientStr)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "The recipient user does not exist.")
|
||||||
ret.write("\nThe recipient user does not exist.\n")
|
|
||||||
return ret
|
|
||||||
trans = VirtualTransaction(user=request.user, recipient=recipient, amount=amount, comment=comment)
|
trans = VirtualTransaction(user=request.user, recipient=recipient, amount=amount, comment=comment)
|
||||||
trans.save()
|
trans.save()
|
||||||
return rc.ALL_OK
|
return rc.ALL_OK
|
||||||
|
@ -377,9 +348,7 @@ class AuthBlobHandler(BaseHandler):
|
||||||
:attr:`main.models.Plugin.pluginCanReadAuthblob` to be true.
|
:attr:`main.models.Plugin.pluginCanReadAuthblob` to be true.
|
||||||
"""
|
"""
|
||||||
if not request.plugin.pluginCanReadAuthblob:
|
if not request.plugin.pluginCanReadAuthblob:
|
||||||
ret = rc.FORBIDDEN
|
return getError(rc.FORBIDDEN, "This plugin is not allowed to read the user's authblob")
|
||||||
ret.write("\nThis plugin is not allowed to read the user's authblob\n")
|
|
||||||
return ret
|
|
||||||
return {'authblob': request.pluginperms.authblob}
|
return {'authblob': request.pluginperms.authblob}
|
||||||
|
|
||||||
@requirePlugin
|
@requirePlugin
|
||||||
|
@ -396,12 +365,9 @@ class AuthBlobHandler(BaseHandler):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not request.plugin.pluginCanWriteAuthblob:
|
if not request.plugin.pluginCanWriteAuthblob:
|
||||||
ret = rc.FORBIDDEN
|
return getError(rc.FORBIDDEN, "This plugin is not allowed to write the user's authblob")
|
||||||
ret.write("\nThis plugin is not allowed to write the user's authblob\n")
|
|
||||||
return ret
|
|
||||||
if not request.data.has_key('authblob'):
|
if not request.data.has_key('authblob'):
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "To change the user's auth blob you actually need to provide one")
|
||||||
ret.write("\nTo change the user's auth blob you actually need to provide one\n")
|
|
||||||
request.pluginperms.authblob = request.data['authblob']
|
request.pluginperms.authblob = request.data['authblob']
|
||||||
request.pluginperms.authblob.save()
|
request.pluginperms.authblob.save()
|
||||||
|
|
||||||
|
@ -425,9 +391,7 @@ class AuthUserHandler(BaseHandler):
|
||||||
- authblob: [required] Authblob to search
|
- authblob: [required] Authblob to search
|
||||||
"""
|
"""
|
||||||
if not request.plugin.uniqueAuthblob:
|
if not request.plugin.uniqueAuthblob:
|
||||||
ret = rc.BAD_REQUEST
|
return getError(rc.BAD_REQUEST, "This plugin does not support unique auth blobs, therefore we can't identify a user uniquely by their authblob")
|
||||||
ret.write("\nThis plugin does not support unique auth blobs, therefore we can't identify a user uniquely by their authblob\n")
|
|
||||||
return ret
|
|
||||||
|
|
||||||
if not request.GET.has_key('authblob'):
|
if not request.GET.has_key('authblob'):
|
||||||
return rc.BAD_REQUEST
|
return rc.BAD_REQUEST
|
||||||
|
|
|
@ -13,3 +13,9 @@ def getDecimal(d, key, default):
|
||||||
return Decimal(d.get(key, default))
|
return Decimal(d.get(key, default))
|
||||||
except InvalidOperation:
|
except InvalidOperation:
|
||||||
return default
|
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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue