API: Added support for negative transactions, fixes #28
This commit is contained in:
parent
8a020a36b5
commit
b9c815effe
|
@ -272,7 +272,7 @@ class TransactionTransactHandler(BaseHandler):
|
|||
def create(self, request):
|
||||
"""Transact money to an account
|
||||
|
||||
- amount: [req] Amount to add to the user's account
|
||||
- amount: [req] Amount to add to the user's account (!= 0)
|
||||
- type: [req] Type of transaction (id)
|
||||
"""
|
||||
|
||||
|
@ -282,8 +282,8 @@ class TransactionTransactHandler(BaseHandler):
|
|||
amount = getDecimal(request.data, 'amount', Decimal(0))
|
||||
tTypeId = getInt(request.data, 'type', -1)
|
||||
|
||||
if amount < Decimal("0.01"):
|
||||
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")
|
||||
if amount == Decimal("0.0"):
|
||||
return getError(rc.BAD_REQUEST, "An amount equaling zero is not supported")
|
||||
tType = None
|
||||
try:
|
||||
tType = TransactionType.objects.get(id=tTypeId)
|
||||
|
|
|
@ -8,11 +8,14 @@ def getInt(d, key, default):
|
|||
return default
|
||||
|
||||
def getDecimal(d, key, default):
|
||||
""" Helper for dict.get to return the default on error. """
|
||||
""" Helper for dict.get to return the default on error or a Decimal
|
||||
with a precision of 2. """
|
||||
try:
|
||||
return Decimal(d.get(key, default))
|
||||
return Decimal("%.2f" % float(d.get(key, default)), 2)
|
||||
except InvalidOperation:
|
||||
return default
|
||||
except ValueError:
|
||||
return default
|
||||
|
||||
def getError(err, msg):
|
||||
""" Get an error, write a message on it an return it. """
|
||||
|
|
Loading…
Reference in New Issue