From b9c815effe63c4a7d12f0c6cfc0c50af05009bd8 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Fri, 20 Jan 2012 22:04:18 +0100 Subject: [PATCH] API: Added support for negative transactions, fixes #28 --- k4ever/api2/handlers.py | 6 +++--- k4ever/api2/helper.py | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/k4ever/api2/handlers.py b/k4ever/api2/handlers.py index 5d82d9b..da5f8a3 100644 --- a/k4ever/api2/handlers.py +++ b/k4ever/api2/handlers.py @@ -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) diff --git a/k4ever/api2/helper.py b/k4ever/api2/helper.py index 5b95ccf..a89c6f1 100644 --- a/k4ever/api2/helper.py +++ b/k4ever/api2/helper.py @@ -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. """