gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-bank] branch master updated: adapt amounts' format t


From: gnunet
Subject: [GNUnet-SVN] [taler-bank] branch master updated: adapt amounts' format to the standard Taler format.
Date: Mon, 15 May 2017 17:15:44 +0200

This is an automated email from the git hooks/post-receive script.

marcello pushed a commit to branch master
in repository bank.

The following commit(s) were added to refs/heads/master by this push:
     new 6013574  adapt amounts' format to the standard Taler format.
6013574 is described below

commit 60135741d2b74c5bf47c28900147a46495b8a579
Author: Marcello Stanisci <address@hidden>
AuthorDate: Mon May 15 17:14:40 2017 +0200

    adapt amounts' format to the standard Taler format.
---
 talerbank/app/amounts.py | 20 +++++++++++---------
 talerbank/app/tests.py   | 17 ++++++++++++++++-
 talerbank/app/views.py   | 11 ++++++++---
 talerbank/settings.py    |  7 +++----
 4 files changed, 38 insertions(+), 17 deletions(-)

diff --git a/talerbank/app/amounts.py b/talerbank/app/amounts.py
index 4e55abe..f201594 100644
--- a/talerbank/app/amounts.py
+++ b/talerbank/app/amounts.py
@@ -28,6 +28,9 @@ FRACTION = 100000000
 class CurrencyMismatchException(Exception):
     pass
 
+class BadFormatAmount(Exception):
+    pass
+
 def check_currency(a1, a2):
     if a1["currency"] != a2["currency"]:
         logger.error("Different currencies given: %s vs %s" % (a1["currency"], 
a2["currency"]))
@@ -40,14 +43,14 @@ def amount_add(a1, a2):
     check_currency(a1, a2)
     a1_float = floatify(a1)
     a2_float = floatify(a2)
-    return parse_amount("%s %s" % (str(a1_float + a2_float), a2["currency"]))
+    return parse_amount("%s:%s" % (a2["currency"], str(a1_float + a2_float)))
 
 def amount_sub(a1, a2):
     check_currency(a1, a2)
     a1_float = floatify(a1)
     a2_float = floatify(a2)
     sub = a1_float - a2_float
-    fmt = "%s %s" % (str(sub), a2["currency"])
+    fmt = "%s:%s" % (a2["currency"], str(sub))
     return parse_amount(fmt)
 
 # Return -1 if a1 < a2, 0 if a1 == a2, 1 if a1 > a2
@@ -76,15 +79,14 @@ def parse_amount(amount_str):
     Parse amount of return None if not a
     valid amount string
     """
-    parsed = re.search("^\s*([0-9]+)(\.[0-9]+)? ([-_*A-Za-z0-9]+)\s*$", 
amount_str)
+    parsed = re.search("^\s*([-_*A-Za-z0-9]+):([0-9]+)(\.[0-9]+)?\s*$", 
amount_str)
     if not parsed:
-        return None
-    value = int(parsed.group(1))
+        raise BadFormatAmount
+    value = int(parsed.group(2))
     fraction = 0
-    if parsed.group(2) is not None:
-        for i, digit in enumerate(parsed.group(2)[1:]):
+    if parsed.group(3) is not None:
+        for i, digit in enumerate(parsed.group(3)[1:]):
             fraction += int(int(digit) * (FRACTION / 10 ** (i+1)))
-
     return {'value': value,
             'fraction': fraction,
-            'currency': parsed.group(3)}
+            'currency': parsed.group(1)}
diff --git a/talerbank/app/tests.py b/talerbank/app/tests.py
index da76fa6..4de125e 100644
--- a/talerbank/app/tests.py
+++ b/talerbank/app/tests.py
@@ -192,7 +192,7 @@ class HistoryTestCase(TestCase):
 
 
 # This tests whether a bank account goes red and then
-# goes green again
+## goes green again
 class DebitTestCase(TestCase):
 
     def setUp(self):
@@ -238,3 +238,18 @@ class DebitTestCase(TestCase):
         tmp["value"] = 1
 
         self.assertEqual(0, amounts.amount_cmp(ub0.balance_obj, tmp))
+
+class TestParseAmount(TestCase):
+     def test_parse_amount(self):
+         ret = amounts.parse_amount("KUDOS:4")
+         self.assertJSONEqual('{"value": 4, "fraction": 0, "currency": 
"KUDOS"}', json.dumps(ret))
+         ret = amounts.parse_amount("KUDOS:4.00")
+         self.assertJSONEqual('{"value": 4, "fraction": 0, "currency": 
"KUDOS"}', json.dumps(ret))
+         ret = amounts.parse_amount("KUDOS:4.3")
+         self.assertJSONEqual('{"value": 4, "fraction": 30000000, "currency": 
"KUDOS"}', json.dumps(ret))
+         try:
+             amounts.parse_amount("Buggy")
+         except amounts.BadFormatAmount:
+             return
+         # make sure the control doesn't get here
+         self.assertEqual(True, False)
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index 20cb466..bd6f583 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -463,6 +463,9 @@ def add_incoming(request):
                                     credit_account,
                                     subject)
         return JsonResponse(dict(serial_id=transaction.id, 
timestamp="/Date(%s)/" % int(transaction.date.timestamp())))
+    except amounts.BadFormatAmount:
+        logger.error("Amount specified in TALER_MAX_DEBT or 
TALER_MAX_DEBT_BANK is malformed")
+        return HttpResponse(status=500)
     except SameAccountException:
         return JsonResponse(dict(error="debit and credit account are the 
same"), status=422)
     except DebtLimitExceededException:
@@ -472,9 +475,12 @@ def add_incoming(request):
 @login_required
 @require_POST
 def withdraw_nojs(request):
-    amount = amounts.parse_amount(request.POST.get("kudos_amount", ""))
-    if amount is None:
+
+    try:
+        amount = amounts.parse_amount(request.POST.get("kudos_amount", ""))
+    except amounts.BadFormatAmount:
         return HttpResponseBadRequest()
+
     response = HttpResponse(status=202)
     response["X-Taler-Operation"] = "create-reserve"
     response["X-Taler-Callback-Url"] = reverse("pin-question")
@@ -530,7 +536,6 @@ def wire_transfer(amount,
     threshold = amounts.parse_amount(settings.TALER_MAX_DEBT)
     if debit_account.user.username == "Bank":
         threshold = amounts.parse_amount(settings.TALER_MAX_DEBT_BANK)
-        
     if 1 == amounts.amount_cmp(debit_account.balance_obj, threshold) \
        and 0 != amounts.amount_cmp(amounts.get_zero(), threshold) \
        and debit_account.debit:
diff --git a/talerbank/settings.py b/talerbank/settings.py
index d5e7b63..ed15674 100644
--- a/talerbank/settings.py
+++ b/talerbank/settings.py
@@ -177,12 +177,11 @@ ROOT_URLCONF = "talerbank.app.urls"
 TALER_CURRENCY = tc.value_string("taler", "currency", required=True)
 
 TALER_MAX_DEBT = tc.value_string("bank", "MAX_DEBT",
-                                 default="50 {}".format(TALER_CURRENCY),
-                                 required=True)
+                                 default="%s:50" % TALER_CURRENCY)
 
 TALER_MAX_DEBT_BANK = tc.value_string("bank", "MAX_DEBT_BANK",
-                                      default="0 {}".format(TALER_CURRENCY),
-                                      required=True)
+                                      default="%s:0" % TALER_CURRENCY)
+
 TALER_DIGITS = 2
 TALER_PREDEFINED_ACCOUNTS = ['Tor', 'GNUnet', 'Taler', 'FSF', 'Tutorial']
 TALER_EXPECTS_DONATIONS = ['Tor', 'GNUnet', 'Taler', 'FSF']

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]