gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [taler-bank] 02/02: porting /history-range


From: gnunet
Subject: [GNUnet-SVN] [taler-bank] 02/02: porting /history-range
Date: Fri, 31 May 2019 15:20:34 +0200

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

marcello pushed a commit to branch master
in repository bank.

commit 97147072588914e1607681d96309d272acabf652
Author: Marcello Stanisci <address@hidden>
AuthorDate: Fri May 31 15:20:25 2019 +0200

    porting /history-range
---
 talerbank/app/schemas.py | 43 ++++++-------------------------------------
 talerbank/app/views.py   | 44 ++++++++++++++++++++++----------------------
 2 files changed, 28 insertions(+), 59 deletions(-)

diff --git a/talerbank/app/schemas.py b/talerbank/app/schemas.py
index a27524d..01503a2 100644
--- a/talerbank/app/schemas.py
+++ b/talerbank/app/schemas.py
@@ -63,9 +63,6 @@ class HistoryParamsBase(forms.Form):
             "^(omit|show)$",
             message="Only 'omit' or 'show' are valid")])
 
-    # FIXME: adjust min/max values.
-    start = forms.IntegerField(required=False)
-
     ordering = forms.CharField(
         required=False,
         empty_value="descending",
@@ -85,6 +82,12 @@ class HistoryParamsBase(forms.Form):
 class HistoryParams(HistoryParamsBase):
     # FIXME: adjust min/max values.
     delta = forms.IntegerField()
+    start = forms.IntegerField(required=False)
+
+class HistoryRangeParams(HistoryParamsBase):
+    # FIXME: adjust min/max values.
+    end = forms.IntegerField()
+    start = forms.IntegerField()
 
 ##
 # Exception class to be raised when a expected URL parameter
@@ -203,32 +206,6 @@ REJECT_REQUEST_SCHEMA = {
     }
 }
 
-
-##
-# Definition for /history-range request URL parameters.
-HISTORY_RANGE_REQUEST_SCHEMA = {
-    "type": "object",
-    "properties": {
-        "auth": {"type": "string", "pattern": "^basic$"},
-        "cancelled": {"type": "string",
-                      "pattern": "^(omit|show)$",
-                      "required": False},
-        "start": {"type": "string",
-                  "pattern": r"^[0-9]+$"},
-        "end": {"type": "string",
-                "pattern": r"^[0-9]+$"},
-        "ordering": {"type": "string",
-                     "pattern": r"^(ascending|descending)$",
-                     "required": False},
-        "direction": {"type": "string",
-                      "pattern": r"^(debit|credit|both|cancel\+|cancel-)$"},
-        "account_number": {"type": "string",
-                           "pattern": "^([0-9]+)$",
-                           "required": False}
-    }
-}
-
-
 ##
 # Definition for /add/incoming request bodies.
 INCOMING_REQUEST_SCHEMA = {
@@ -298,13 +275,6 @@ def validate_reject(data):
     validate(data, REJECT_REQUEST_SCHEMA)
 
 ##
-# Check /history-range input data.
-#
-# @param data dict representing /history's GET parameters.
-def validate_history_range(data):
-    validate(data, HISTORY_RANGE_REQUEST_SCHEMA)
-
-##
 # Check wire details
 # (regardless of which endpoint triggered the check)
 #
@@ -342,7 +312,6 @@ def check_withdraw_session(data):
 def validate_data(request, data):
     switch = {
         "/reject": validate_reject,
-        "/history-range": validate_history_range,
         "/admin/add/incoming": validate_add_incoming,
         "/pin/verify": check_withdraw_session,
         "/pin/question": validate_pin_tan
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index 3d1f7bc..34a5b70 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -44,7 +44,7 @@ from django.shortcuts import render, redirect
 from datetime import datetime
 from .models import BankAccount, BankTransaction
 from .amount import Amount
-from .schemas import validate_data, HistoryParams, URLParamValidationError
+from .schemas import validate_data, HistoryParams, HistoryRangeParams, 
URLParamValidationError
 LOGGER = logging.getLogger(__name__)
 
 ##
@@ -704,28 +704,28 @@ def build_history_response(qs, cancelled, user_account):
 @require_GET
 @login_via_headers
 def serve_history_range(request, user_account):
-    validate_data(request, request.GET.dict())
 
-    # Ordering.
-    ordering = request.GET.get("ordering", "descending")
+    get_params = HistoryRangeParams(request.GET.dict())
+    if not get_params.is_valid():
+        raise URLParamValidationError(get_params.errors, 400)
+
+    start_td = datetime.fromtimestamp(
+        get_params.cleaned_data.get("start"))
+    end_td = datetime.fromtimestamp(
+        get_params.cleaned_data.get("end"))
+
+    qs = query_history_range(
+        user_account.bankaccount,
+        request.GET.get("direction"),
+        start_td,
+        end_td,
+        get_params.cleaned_data.get("ordering"))
+
+    history = build_history_response(
+        qs,
+        get_params.cleaned_data.get("cancelled"),
+        user_account)
 
-    # Note: those values got sanity-checked by "validate_data()"
-    start = int(request.GET.get("start"))
-    end = int(request.GET.get("end"))
-    
-    start_td = datetime.fromtimestamp(start)
-    end_td = datetime.fromtimestamp(end)
-
-    qs = query_history_range(user_account.bankaccount,
-                             request.GET.get("direction"),
-                             start_td,
-                             end_td,
-                             "descending" == ordering)
-
-    history = build_history_response(qs,
-                                     request.GET.get("cancelled",
-                                                     "show"),
-                                     user_account)
     if not history:
         return HttpResponse(status=204)
     return JsonResponse(dict(data=history), status=200)
@@ -741,7 +741,7 @@ def serve_history_range(request, user_account):
 def serve_history(request, user_account):
     get_params = HistoryParams(request.GET.dict())
     if not get_params.is_valid():
-      raise URLParamValidationError(get_params.errors, 400)
+        raise URLParamValidationError(get_params.errors, 400)
 
     delta = get_params.cleaned_data.get("delta")
     start = get_params.cleaned_data.get("start")

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



reply via email to

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