Browse Source

API: Added support for negative transactions, fixes #28

master
Sebastian Lohff 11 years ago
parent
commit
b9c815effe
  1. 6
      k4ever/api2/handlers.py
  2. 7
      k4ever/api2/helper.py

6
k4ever/api2/handlers.py

@ -272,7 +272,7 @@ class TransactionTransactHandler(BaseHandler): @@ -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): @@ -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)

7
k4ever/api2/helper.py

@ -8,11 +8,14 @@ def getInt(d, key, default): @@ -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…
Cancel
Save