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: towards /history refact


From: gnunet
Subject: [GNUnet-SVN] [taler-bank] branch master updated: towards /history refactoring
Date: Fri, 05 May 2017 19:09:06 +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 ae74b2d  towards /history refactoring
ae74b2d is described below

commit ae74b2d32a4ad6d21620e84a3190398f8778e19d
Author: Marcello Stanisci <address@hidden>
AuthorDate: Fri May 5 19:08:44 2017 +0200

    towards /history refactoring
---
 talerbank/app/schemas.py |  13 -----
 talerbank/app/tests.py   |  12 ++---
 talerbank/app/views.py   | 123 +++++++++++++++++++++++++++--------------------
 3 files changed, 74 insertions(+), 74 deletions(-)

diff --git a/talerbank/app/schemas.py b/talerbank/app/schemas.py
index 0d821ef..2f33674 100644
--- a/talerbank/app/schemas.py
+++ b/talerbank/app/schemas.py
@@ -45,16 +45,6 @@ auth_schema = {
     }
 }
 
-history_schema = {
-    "type": "object",
-    "properties" : {
-        "direction": {"type": "string", "required": False},
-        "auth": auth_schema,
-        "start": {"type": "integer", "required": False},
-        "delta": {"type": "integer", "required": False}
-    }
-}
-
 amount_schema = {
     "type": "object",
     "properties": {
@@ -78,9 +68,6 @@ incoming_request_schema = {
 def validate_amount(amount):
     validictory.validate(amount, amount_schema)
 
-def validate_history(request_data):
-    validictory.validate(request_data, history_schema)
-
 def validate_wiredetails(wiredetails):
     validictory.validate(wiredetails, wiredetails_schema)
 
diff --git a/talerbank/app/tests.py b/talerbank/app/tests.py
index 7783e3c..d6978e1 100644
--- a/talerbank/app/tests.py
+++ b/talerbank/app/tests.py
@@ -101,15 +101,9 @@ class HistoryTestCase(TestCase):
 
     def test_history(self):
         c = Client()
-        response = c.post(reverse("history", urlconf=urls),
-                          data='{"auth": {"type": "basic"}, \
-                                  "start": 4, \
-                                  "delta": 4, \
-                                  "direction": "whatever"}',
-                          content_type="application/json",
-                          **{"X-Taler-Bank-Username": "User", 
"X-Taler-Bank-Password": "Password"})
-        # Because of the 'whatever' direction given
-        self.assertEqual(400, response.status_code)
+        response = c.get(reverse("history", urlconf=urls), {"auth": "basic", 
"delta": "+4"},
+                         **{"X_TALER_BANK_USERNAME": "User", 
"X_TALER_BANK_PASSWORD": "Password"})
+        self.assertEqual(200, response.status_code)
 
 
 # This tests whether a bank account goes red and then
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index 591eead..c8ab856 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -15,6 +15,7 @@
 #  @author Marcello Stanisci
 #  @author Florian Dold
 
+import re
 import django.contrib.auth
 import django.contrib.auth.views
 import django.contrib.auth.forms
@@ -326,73 +327,89 @@ def public_accounts(request, name=None):
     )
     return render(request, "public_accounts.html", context)
 
address@hidden
address@hidden
 def history(request):
     """
     This API is used to get a list of transactions related to one user.
     """
-    data = json.loads(request.body.decode("utf-8"))
-    try: schemas.validate_history(data)
-    except ValueError:
-        return HttpResponseBadRequest()
-    user_account = auth_and_login(request)
 
+    # login caller
+    user_account = auth_and_login(request)
     if not user_account:
         return JsonResponse(dict(error="authentication failed"),
-                             status=401)
+                            status=401)
+    # delta
+    delta = request.GET.get("delta")
+    if not delta:
+        return HttpResponseBadRequest()
+    parsed_delta = re.search("(\+)([0-9]+)", delta)
+    try:
+        parsed_delta.group(0)
+    except AttributeError:
+        return JasonResponse(dict(error="Bad 'delta' parameter"), status=400)
+    delta = parsed_delta.group(2)
+    # start
+    start = request.GET.get("start")
+
+    # sign (past/future)
+    sign = parsed_delta.group(1)
+    if not start:
+        sign = "-"
+    if "+" == sign:
+        sign = Q(id__gt=start)
+    else:
+        sign = Q(id__lt=start)
 
-    start = data.get("start", 0)
-    delta = data.get("delta", 0)
-    
-    direction = data.get("direction")
+    # direction (debit/credit)
+    direction = request.GET.get("direction")
+
+    # target account
+    target_account = account_number = request.GET.get("account_number")
+    if not target_account:
+        target_account = user_account.bankaccount
+
+    query_string = Q()
     history = []
-    if not direction:
-        qs = 
BankTransaction.objects.filter((Q(debit_account=user_account.bankaccount) |
-                                             
Q(credit_account=user_account.bankaccount)) &
-                                             Q(id__gt=start))[:delta]
-        logger.info("Found %d results for user '%s'" % (len(qs), 
user_account.username))
-        for entry in qs:
-            counterpart = entry.credit_account.user.username
-            sign = "-"
-            if entry.credit_account.user.username == user_account.username:
-                counterpart = entry.debit_account.user.username
-                sign = "+"
-            history.append(dict(counterpart=counterpart,
-                                amount=entry.amount_obj,
-                                sign=sign))
-        logger.info(json.dumps(history))
-        return HttpResponse(200)
-
-    if "debit" == direction:
-        qs = 
BankTransaction.objects.filter(Q(debit_account=user_account.bankaccount) &
-                                             Q(id__gt=start))[:delta]
-        for entry in qs:
-            history.append(dict(amount=entry.amount_obj,
-                                 
counterpart=entry.credit_account.user.username))
-        return JsonResponse(dict(data=history), status=200)
-
-    if "credit" == direction:
-        qs = 
BankTransaction.objects.filter(Q(credit_account=user_account.bankaccount) &
-                                             Q(id__gt=start))[:delta]
-        for entry in qs:
-            history.append(dict(amount=entry.amount_obj,
-                                 
counterpart=entry.debit_account.user.username))
-        return JsonResponse(dict(data=history), status=200)
-
-    return JsonResponse(dict(error="Unknown 'direction' indication"), 
status=400)
+
+    if "credit" != direction:
+        query_string |= Q(debit_account=target_account)
+    if "debit" != direction:
+        query_string |= Q(credit_account=target_account)
+    query_string = sign & (query_string)
+
+    qs = BankTransaction.objects.filter(query_string)[:delta]
+    for entry in qs:
+        counterpart = entry.credit_account.user.username
+        sign = "-"
+        if entry.credit_account.user.username == user_account.username:
+            counterpart = entry.debit_account.user.username
+            sign = "+"
+        history.append(dict(counterpart=counterpart,
+                            amount=entry.amount_obj,
+                            sign=sign,
+                            subject=entry.subject))
+
+    return JsonResponse(history, 200)
 
 
 def auth_and_login(request):
     """Return user instance after checking authentication
        credentials, False if errors occur"""
     
-    data = json.loads(request.body.decode("utf-8"))
-    if "basic" != data["auth"]["type"]:
+    auth_type = None
+
+    if "POST" == request.method:
+        data = json.loads(request.body.decode("utf-8"))
+        auth_type = data["auth"]["type"]
+    if "GET" == request.method:
+        auth_type = request.GET.get("auth")
+
+    if "basic" != auth_type:
         return JsonResponse(dict(error="auth method not supported"),
-                            status=405)
+                            status=405)        
 
-    username = request.META["X-Taler-Bank-Username"]
-    password = request.META["X-Taler-Bank-Password"]
+    username = request.META.get("X-Taler-Bank-Username")
+    password = request.META.get("X-Taler-Bank-Password")
 
     if not username or not password:
         return False
@@ -401,8 +418,6 @@ def auth_and_login(request):
                                             password=password)
 
 
-
-
 @csrf_exempt
 @require_POST
 def add_incoming(request):
@@ -422,6 +437,10 @@ def add_incoming(request):
         logger.error("Bad data POSTed")
         return HttpResponseBadRequest()
 
+    # FIXME, in the bank's current state, the debit account is
+    # always the one of the authenticated user; for future releases,
+    # the debit account should be specified too, as one user may
+    # have multiple accounts at the same bank.
     user_account = auth_and_login(request)
 
     if not user_account:

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



reply via email to

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