summaryrefslogtreecommitdiff
path: root/talerbank
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2019-05-31 15:20:25 +0200
committerMarcello Stanisci <stanisci.m@gmail.com>2019-05-31 15:20:25 +0200
commit97147072588914e1607681d96309d272acabf652 (patch)
tree78ac5f8e228dde6ae095e4831eb5697d1d0dfc1e /talerbank
parentd8320c684cf3b18964d7d5f80698779478986c19 (diff)
downloadbank-97147072588914e1607681d96309d272acabf652.tar.gz
bank-97147072588914e1607681d96309d272acabf652.tar.bz2
bank-97147072588914e1607681d96309d272acabf652.zip
porting /history-range
Diffstat (limited to 'talerbank')
-rw-r--r--talerbank/app/schemas.py43
-rw-r--r--talerbank/app/views.py44
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")