diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2019-05-31 15:00:51 +0200 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2019-05-31 15:00:51 +0200 |
commit | 327f37bb62cbe9c058c93c97ce8e8cc07f86d600 (patch) | |
tree | 42999b968a2b6cb046fa1dd3be9628608b02efd8 /talerbank/app/views.py | |
parent | ef37242e94698d13ecdf2448c77a0fc3148918cb (diff) | |
download | bank-327f37bb62cbe9c058c93c97ce8e8cc07f86d600.tar.gz bank-327f37bb62cbe9c058c93c97ce8e8cc07f86d600.tar.bz2 bank-327f37bb62cbe9c058c93c97ce8e8cc07f86d600.zip |
5715.
Initial change in the structure. Just porting the /history
validation to use the 'form' API for validating GET arguments.
Subsequent changes (for GET requests) will follow the same pattern.
Diffstat (limited to 'talerbank/app/views.py')
-rw-r--r-- | talerbank/app/views.py | 89 |
1 files changed, 39 insertions, 50 deletions
diff --git a/talerbank/app/views.py b/talerbank/app/views.py index f4af47c..3d1f7bc 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 +from .schemas import validate_data, HistoryParams, URLParamValidationError LOGGER = logging.getLogger(__name__) ## @@ -259,7 +259,7 @@ def profile_page(request): account_no=request.user.bankaccount.account_no, wt_form=wtf, history=extract_history(request.user.bankaccount, - True), + -1 * (UINT64_MAX / 2 / 2)) ) if settings.TALER_SUGGESTED_EXCHANGE: context["suggested_exchange"] = settings.TALER_SUGGESTED_EXCHANGE @@ -459,12 +459,14 @@ def logout_view(request): # entries will be younger / older than @a start. # @return the history array. def extract_history(account, - descending, - delta=None, - start=UINT64_MAX, - sign="-"): + delta, + start=UINT64_MAX): history = [] - qs = query_history(account, "both", delta, start, sign, True) + qs = query_history(account, + "both", + delta, + start, + "descending") for item in qs: if item.credit_account == account: counterpart = item.debit_account @@ -514,9 +516,9 @@ def serve_public_accounts(request, name=None, page=None): "both", # Note: the parameter below is used for slicing arrays # and django/python is not allowing slicing with big numbers. - (UINT64_MAX / 2 ) / 2, - start=0, - sign="+").count() + UINT64_MAX / 2 / 2, + 0, + "descending").count() DELTA = 30 # '//' operator is NO floating point. num_pages = max(num_records // DELTA, 1) @@ -525,10 +527,8 @@ def serve_public_accounts(request, name=None, page=None): # Retrieve DELTA records younger than 'start_row' (included). history = extract_history(user.bankaccount, - True, DELTA * page, - 0, - "+")[DELTA * (page - 1):(DELTA * page)] + 0)[DELTA * (page - 1):(DELTA * page)] pages = list(range(1, num_pages + 1)) @@ -636,34 +636,31 @@ def query_history_range(bank_account, # * cancel-: only entries where the querying user cancelled # the _paying_ of money will be returned. # @param delta how many history entries will be contained in the -# array (will be passed as-is to the internal routine -# @a query_history). +# array. # @param start any history will be searched starting from this # value (which is a row ID), and going to the past or to # the future (depending on the user choice). However, this # value itself will not be included in the history. # @param sign this value ("+"/"-") determines whether the history # entries will be younger / older than @a start. -# @param descending if True, then the results will have the -# youngest entry in the first position. +# @param ordering "descending" or anything else (for "ascending"). def query_history(bank_account, direction, delta, start, - sign, - descending=True): + ordering): - sign_filter = { - "+": Q(id__gt=start), - "-": Q(id__lt=start), - } + def sign_filter(delta): + if 0 > delta: + return Q(id__lt=start) + return Q(id__gt=start) qs = BankTransaction.objects.filter( direction_switch(bank_account, direction), - sign_filter.get(sign)) + sign_filter(delta)) - order = "-id" if descending else "id" - return qs.order_by(order)[:delta] + order = "-id" if "descending" == ordering else "id" + return qs.order_by(order)[:abs(delta)] ## @@ -733,8 +730,6 @@ def serve_history_range(request, user_account): return HttpResponse(status=204) return JsonResponse(dict(data=history), status=200) - - ## # Serve a request of /history. # @@ -744,33 +739,27 @@ def serve_history_range(request, user_account): @require_GET @login_via_headers def serve_history(request, user_account): - validate_data(request, request.GET.dict()) + get_params = HistoryParams(request.GET.dict()) + if not get_params.is_valid(): + raise URLParamValidationError(get_params.errors, 400) - # delta (it does exist: enforced by the check above.) - parsed_delta = re.search(r"([\+-])?([0-9]+)", - request.GET.get("delta")) - # normalize the sign. - sign = parsed_delta.group(1) - sign = sign if sign else "+" + delta = get_params.cleaned_data.get("delta") + start = get_params.cleaned_data.get("start") - # Ordering. - ordering = request.GET.get("ordering", "descending") - start = request.GET.get("start") - - if not start: - start = 0 if "+" == sign else UINT64_MAX + if None == start: + start = 0 if 0 <= delta else UINT64_MAX qs = query_history(user_account.bankaccount, - request.GET.get("direction"), - int(parsed_delta.group(2)), - int(start), - sign, - "descending" == ordering) + get_params.cleaned_data.get("direction"), + delta, + start, + get_params.cleaned_data.get("ordering")) + + history = build_history_response( + qs, + get_params.cleaned_data.get("cancelled"), + user_account) - 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) |