summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2018-02-20 11:32:17 +0100
committerFlorian Dold <florian.dold@gmail.com>2018-02-20 11:32:17 +0100
commitc14d6c9fe43952329993ea3c16dc1ead486d6c8a (patch)
treefcdca212bd4a82aa0afcb58c1fec2f9998d36c79
parent2ccd46c73166970ebae9063dc2612e2ae2274beb (diff)
downloadbank-c14d6c9fe43952329993ea3c16dc1ead486d6c8a.tar.gz
bank-c14d6c9fe43952329993ea3c16dc1ead486d6c8a.tar.bz2
bank-c14d6c9fe43952329993ea3c16dc1ead486d6c8a.zip
Do not use cookies for pagination, fix/simplify logic
-rw-r--r--talerbank/app/views.py79
1 files changed, 32 insertions, 47 deletions
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index 55686ce..8c05868 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -48,6 +48,10 @@ class LoginFailed(Exception):
hint = "Wrong username/password"
http_status_code = 401
+class PrivateAccountException(Exception):
+ hint = "The selected account is private"
+ http_status_code = 402
+
class DebitLimitException(Exception):
hint = "Debit too high, operation forbidden."
http_status_code = 403
@@ -346,7 +350,6 @@ def extract_history(account, delta=None, start=-1, sign="+"):
def serve_public_accounts(request, name=None, page=None):
-
try:
page = int(page)
except Exception:
@@ -355,54 +358,28 @@ def serve_public_accounts(request, name=None, page=None):
if not name:
name = settings.TALER_PREDEFINED_ACCOUNTS[0]
user = User.objects.get(username=name)
+ if not user.bankaccount.is_public:
+ raise PrivateAccountException("Can't display public history for private account")
- if "public_history_count" not in request.session:
- qs = extract_history(user.bankaccount, sign="-")
- youngest = -1
- if qs:
- youngest = qs[0]["row_id"]
- request.session["public_history_account"] = \
- len(qs), youngest
+ num_records = query_history_raw(user.bankaccount, "both", start=-1, sign="-").count()
DELTA = 30
- youngest = request.session["public_history_account"][1]
- # normalize page
- if not page or page in [0, 1]:
- page = 1
- # page 0 and 1 give both the youngest 100 records.
- if page > 1:
- youngest = youngest - (DELTA * (page - 1)) # goes backwards.
- if not user.bankaccount.is_public:
- request.session["public_accounts_hint"] = \
- True, False, "Could not query private accounts!"
- fail_message, success_message, hint = \
- get_session_hint(request, "public_accounts_hint")
- public_accounts = BankAccount.objects.filter(is_public=True)
+ youngest = 1 + DELTA * (page - 1)
- # Retrieve DELTA records older than 'start'.
- history = extract_history(
- user.bankaccount, DELTA,
- -1 if youngest < 2 else youngest, "-")
+ public_accounts = BankAccount.objects.filter(is_public=True)
- num_pages = max(
- request.session["public_history_account"][0] / DELTA,
- 1) # makes sure pages[0] exists, below.
+ # Retrieve DELTA records, starting from 'youngest'
+ history = extract_history(user.bankaccount, DELTA, youngest - 1, "+")
- pages = list(
- range(max(1, page - 3),
- # need +1 because the range is not inclusive for
- # the upper limit.
- min(page + 4, (math.ceil(num_pages) + 1))))
+ num_pages = max(num_records // DELTA, 1)
+ pages = list(range(1, num_pages + 1))
context = dict(
current_page=page,
- back = page - 1 if pages[0] > 1 else None,
- forth = page + 1 if pages[-1] < num_pages else None,
+ back = page - 1 if page > 1 else None,
+ forth = page + 1 if page < num_pages else None,
public_accounts=public_accounts,
selected_account=dict(
- fail_message=fail_message,
- success_message=success_message,
- hint=hint,
name=name,
number=user.bankaccount.account_no,
history=history,
@@ -411,6 +388,7 @@ def serve_public_accounts(request, name=None, page=None):
)
return render(request, "public_accounts.html", context)
+
def login_via_headers(view_func):
def _decorator(request, *args, **kwargs):
user_account = auth_and_login(request)
@@ -436,15 +414,23 @@ def login_via_headers(view_func):
# than 'start'.
def query_history(bank_account, direction, delta, start, sign):
+ qs = query_history_raw(bank_account, direction, start, sign)
+ # '-id' does descending ordering.
+ ordering = "-id" if sign in ["-", "*"] else "id"
+ return qs.order_by(ordering)[:delta]
+
+
+def query_history_raw(bank_account, direction, start, sign):
direction_switch = {
- "both": Q(debit_account=bank_account) \
- | Q(credit_account=bank_account),
+ "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)}
+ "cancel+": (Q(credit_account=bank_account) &
+ Q(cancelled=True)),
+ "cancel-": (Q(debit_account=bank_account) &
+ Q(cancelled=True))
+ }
sign_filter = {
"+": Q(id__gt=start),
@@ -457,9 +443,8 @@ def query_history(bank_account, direction, delta, start, sign):
return BankTransaction.objects.filter(
direction_switch.get(direction),
- sign_filter.get(sign)).order_by(
- # '-id' does descending ordering.
- "-id" if sign in ["-", "*"] else "id")[:delta]
+ sign_filter.get(sign));
+
@require_GET
@login_via_headers