diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2018-09-06 15:11:54 +0200 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2018-09-06 15:11:54 +0200 |
commit | 2e90943de2a8e640e647d39cd59a3063c0146511 (patch) | |
tree | b99bc847cdab9526d4b26109d5d9bd0ff8081e92 | |
parent | ef4a846a1208de253cce599ce00bdbd0ceb3252c (diff) | |
download | bank-2e90943de2a8e640e647d39cd59a3063c0146511.tar.gz bank-2e90943de2a8e640e647d39cd59a3063c0146511.tar.bz2 bank-2e90943de2a8e640e647d39cd59a3063c0146511.zip |
History element order.
The user can choose the order (ascending/descending)
that history elements are returned in.
This allows to NOT change the current wirewatch logic
and to show puclic accounts histories in a descending
fashion.
-rw-r--r-- | talerbank/app/tests.py | 6 | ||||
-rw-r--r-- | talerbank/app/views.py | 32 |
2 files changed, 28 insertions, 10 deletions
diff --git a/talerbank/app/tests.py b/talerbank/app/tests.py index 7c246ec..a3fc291 100644 --- a/talerbank/app/tests.py +++ b/talerbank/app/tests.py @@ -400,12 +400,12 @@ class HistoryTestCase(TestCase): delta="4", direction="both"), HistoryContext( expected_resp={ - "fields": [("row_id", 9)], + "fields": [("row_id", 6)], "status": 200}, delta="+1", start="5", direction="both"), HistoryContext( expected_resp={ - "fields": [("wt_subject", "h")], + "fields": [("wt_subject", "a")], "status": 200}, delta="-1", start=9, direction="both"), HistoryContext( @@ -600,7 +600,7 @@ class MeasureHistory(TestCase): # Measure the time extract_history() needs to retrieve # ~ntransfers records. timer = timeit.Timer( - stmt="extract_history(self.user_bankaccount0)", + stmt="extract_history(self.user_bankaccount0, False)", setup="from talerbank.app.views import extract_history", globals=locals()) total_time = timer.timeit(number=1) diff --git a/talerbank/app/views.py b/talerbank/app/views.py index 6a09edf..1e53d98 100644 --- a/talerbank/app/views.py +++ b/talerbank/app/views.py @@ -184,7 +184,8 @@ def profile_page(request): currency=request.user.bankaccount.amount.currency, account_no=request.user.bankaccount.account_no, wt_form=wtf, - history=extract_history(request.user.bankaccount), + history=extract_history(request.user.bankaccount, + True), ) if settings.TALER_SUGGESTED_EXCHANGE: context["suggested_exchange"] = settings.TALER_SUGGESTED_EXCHANGE @@ -326,7 +327,11 @@ def logout_view(request): return redirect("index") -def extract_history(account, delta=None, start=-1, sign="+"): +def extract_history(account, + descending, + delta=None, + start=-1, + sign="+"): history = [] qs = query_history(account, "both", delta, start, sign) for item in qs: @@ -376,6 +381,7 @@ 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, -1, "+")[DELTA * (page - 1):(DELTA * page)] @@ -420,11 +426,23 @@ def login_via_headers(view_func): # 'sign' being passed. # 'sign': (+|-) indicating that we want records younger|older # than 'start'. - -def query_history(bank_account, direction, delta, start, sign): - qs = query_history_raw(bank_account, direction, start, sign) - # '-id' does descending ordering. - return qs.order_by("-id")[:delta] +# 'descending': records are sorted older->younger per default, +# unless this value is true. + +def query_history(bank_account, + direction, + delta, + start, + sign, + descending=False): + + qs = query_history_raw(bank_account, + direction, + start, + sign) + + order = "-id" if descending else "id" + return qs.order_by(order)[:delta] def query_history_raw(bank_account, direction, start, sign): |