API: Added support for negative transactions, fixes #28

This commit is contained in:
Sebastian Lohff 2012-01-20 22:04:18 +01:00
parent 8a020a36b5
commit b9c815effe
2 changed files with 8 additions and 5 deletions

View File

@ -272,7 +272,7 @@ class TransactionTransactHandler(BaseHandler):
def create(self, request): def create(self, request):
"""Transact money to an account """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) - type: [req] Type of transaction (id)
""" """
@ -282,8 +282,8 @@ class TransactionTransactHandler(BaseHandler):
amount = getDecimal(request.data, 'amount', Decimal(0)) amount = getDecimal(request.data, 'amount', Decimal(0))
tTypeId = getInt(request.data, 'type', -1) tTypeId = getInt(request.data, 'type', -1)
if amount < Decimal("0.01"): if amount == Decimal("0.0"):
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") return getError(rc.BAD_REQUEST, "An amount equaling zero is not supported")
tType = None tType = None
try: try:
tType = TransactionType.objects.get(id=tTypeId) tType = TransactionType.objects.get(id=tTypeId)

View File

@ -8,11 +8,14 @@ def getInt(d, key, default):
return default return default
def getDecimal(d, key, 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: try:
return Decimal(d.get(key, default)) return Decimal("%.2f" % float(d.get(key, default)), 2)
except InvalidOperation: except InvalidOperation:
return default return default
except ValueError:
return default
def getError(err, msg): def getError(err, msg):
""" Get an error, write a message on it an return it. """ """ Get an error, write a message on it an return it. """