gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [taler-bank] 01/05: history extracting logic goes in one po


From: gnunet
Subject: [GNUnet-SVN] [taler-bank] 01/05: history extracting logic goes in one point, and /history calls it now.
Date: Mon, 15 Jan 2018 17:08:01 +0100

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

marcello pushed a commit to branch master
in repository bank.

commit 544b1f0927cd2dbbdba3854ecf4fdc4c2d36d0fd
Author: Marcello Stanisci <address@hidden>
AuthorDate: Mon Jan 15 11:25:30 2018 +0100

    history extracting logic goes in one point, and /history
    calls it now.
---
 talerbank/app/views.py | 70 ++++++++++++++++++++++++++++----------------------
 1 file changed, 40 insertions(+), 30 deletions(-)

diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index 6383a68..a41c072 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -320,11 +320,10 @@ def logout_view(request):
     return redirect("index")
 
 
-def extract_history(account):
+def extract_history(account, delta=None, start=-1, sign="+"):
     history = []
-    related_transactions = BankTransaction.objects.filter(
-        Q(debit_account=account) | Q(credit_account=account))
-    for item in related_transactions:
+    qs = query_history(account, "both", delta, start, sign)
+    for item in qs:
         if item.credit_account == account:
             counterpart = item.debit_account
             sign = ""
@@ -378,6 +377,38 @@ def login_via_headers(view_func):
         return view_func(request, user_account, *args, **kwargs)
     return wraps(view_func)(_decorator)
 
+# Internal function used by /history and /public-accounts.  It
+# offers abstraction against the query string definition and DB
+# querying.
+#
+# 'bank_account': whose history is going to be retrieved.
+# 'direction': (both|credit|debit|cancel+|cancel-).
+# 'delta': how many results are going to be extracted.  If 'None'
+#   is given, no filter of this kind will be applied.
+# 'start': a "id" indicating the first record to be returned.
+#   If -1 is given, then the first record will be the youngest.
+# 'sign': (+|-) indicating that we want records younger|older
+#   than 'start'.
+
+def query_history(bank_account, direction, delta, start, sign):
+    direction_switch = {
+        "both": Q(debit_account=bank_account) \
+                | Q(credit_account=bank_account),
+        "credit": Q(credit_account=bank_account),
+        "debit": Q(debit_account=bank_account),
+        "cancel+": Q(credit_account=bank_account) \
+                      & Q(cancelled=True),
+        "cancel-": Q(debit_account=bank_account) \
+                      & Q(cancelled=True)}
+    sign_filter = {
+        "+": Q(id__gt=start),
+        "-": Q(id__lt=start)    
+    }
+    return BankTransaction.objects.filter(
+        direction_switch.get(direction),
+        sign_filter.get(sign)).order_by(
+            "-id" if sign == "-" else "id")[:delta]
+
 @require_GET
 @login_via_headers
 def serve_history(request, user_account):
@@ -390,36 +421,15 @@ def serve_history(request, user_account):
     parsed_delta = re.search(r"([\+-])?([0-9]+)",
                              request.GET.get("delta"))
     sign = parsed_delta.group(1)
-    # start
-    start = int(request.GET.get("start", -1))
 
-    # translating delta's sign into query object
-    sign_filter = Q()
-    if start >= 0:
-        sign_filter = Q(id__gt=start)
-        if sign == "-":
-            sign_filter = Q(id__lt=start)
+    qs = query_history(user_account.bankaccount,
+                       request.GET.get("direction"),
+                       int(parsed_delta.group(2)),
+                       int(request.GET.get("start", -1)),
+                       sign if sign else "+")
 
-
-    direction_switch = {
-        "both": Q(debit_account=user_account.bankaccount) \
-                | Q(credit_account=user_account.bankaccount),
-        "credit": Q(credit_account=user_account.bankaccount),
-        "debit": Q(debit_account=user_account.bankaccount),
-        "cancel+": Q(credit_account=user_account.bankaccount) \
-                      & Q(cancelled=True),
-        "cancel-": Q(debit_account=user_account.bankaccount) \
-                      & Q(cancelled=True)
-    }
-    # Sanity checks are done at the beginning, so 'direction' key
-    # (and its value as switch's key) does exist here.
-    query_string = direction_switch[request.GET["direction"]]
     history = []
     cancelled = request.GET.get("cancelled", "show")
-    qs = BankTransaction.objects.filter(
-        query_string, sign_filter).order_by(
-            "-id" if sign == "-" else "id") \
-            [:int(parsed_delta.group(2))]
     for entry in qs:
         counterpart = entry.credit_account.account_no
         sign_ = "-"

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



reply via email to

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