diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2017-12-29 13:31:30 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2017-12-29 13:31:30 +0100 |
commit | af81a32e5d304187f50a070a4bc65f87ee90750e (patch) | |
tree | 1822547afc2506df6349ed379d3eac9b22537429 /talerbank | |
parent | e3c242043b199ddade6ae540bcc8284c0e377d5e (diff) | |
download | bank-af81a32e5d304187f50a070a4bc65f87ee90750e.tar.gz bank-af81a32e5d304187f50a070a4bc65f87ee90750e.tar.bz2 bank-af81a32e5d304187f50a070a4bc65f87ee90750e.zip |
linting everything but (almost) unavoidable things
Diffstat (limited to 'talerbank')
-rw-r--r-- | talerbank/app/amount.py | 3 | ||||
-rw-r--r-- | talerbank/app/management/commands/dump_talerdb.py | 30 | ||||
-rw-r--r-- | talerbank/app/management/commands/provide_accounts.py | 5 | ||||
-rw-r--r-- | talerbank/app/middleware.py | 65 | ||||
-rw-r--r-- | talerbank/app/schemas.py | 28 | ||||
-rw-r--r-- | talerbank/app/templates/public_accounts.html | 14 | ||||
-rw-r--r-- | talerbank/app/views.py | 67 |
7 files changed, 118 insertions, 94 deletions
diff --git a/talerbank/app/amount.py b/talerbank/app/amount.py index b8447f8..d535586 100644 --- a/talerbank/app/amount.py +++ b/talerbank/app/amount.py @@ -22,8 +22,6 @@ # mentioned above, and it is meant to be manually copied into any project # which might need it. -from typing import Type - class CurrencyMismatch(Exception): hint = "Internal logic error (currency mismatch)" http_status_code = 500 @@ -124,7 +122,6 @@ class Amount: # after the dot. def stringify(self, ndigits: int, pretty=False) -> str: assert ndigits > 0 - ret = '%s:%s.' % (self.currency, str(self.value)) tmp = self.fraction fraction_str = "" while ndigits > 0: diff --git a/talerbank/app/management/commands/dump_talerdb.py b/talerbank/app/management/commands/dump_talerdb.py index ba81444..10dc936 100644 --- a/talerbank/app/management/commands/dump_talerdb.py +++ b/talerbank/app/management/commands/dump_talerdb.py @@ -1,16 +1,19 @@ # This file is part of TALER # (C) 2014, 2015, 2106 INRIA # -# TALER is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 3, or (at your option) any later version. +# TALER is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 3, or (at your +# option) any later version. # -# TALER is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# TALER is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +# You should have received a copy of the GNU General Public +# License along with TALER; see the file COPYING. If not, see +# <http://www.gnu.org/licenses/> # # @author Marcello Stanisci @@ -19,7 +22,6 @@ import logging from django.core.management.base import BaseCommand from django.db.utils import OperationalError, ProgrammingError from ...models import BankAccount, BankTransaction -from .helpers import hard_db_error_log LOGGER = logging.getLogger(__name__) @@ -30,9 +32,11 @@ def dump_accounts(): print("No accounts created yet..") return for acc in accounts: - print(acc.user.username + " has account number " + str(acc.account_no)) + print(acc.user.username + \ + " has account number " + \ + str(acc.account_no)) except (OperationalError, ProgrammingError): - hard_db_error_log() + LOGGER.error("Hard database error, does it exist?") sys.exit(1) @@ -41,15 +45,13 @@ def dump_history(): history = BankTransaction.objects.all() for item in history: msg = [] - # concatenating via 'append' because the + operator put - # as the first/last character on a line makes flake8 complain msg.append("+%s, " % item.credit_account.account_no) msg.append("-%s, " % item.debit_account.account_no) msg.append(item.amount.stringify(2)) msg.append(item.subject) print(''.join(msg)) except (OperationalError, ProgrammingError): - hard_db_error_log() + LOGGER.error("Hard database error, does it exist?") sys.exit(1) diff --git a/talerbank/app/management/commands/provide_accounts.py b/talerbank/app/management/commands/provide_accounts.py index cf8d6e9..6bf8c53 100644 --- a/talerbank/app/management/commands/provide_accounts.py +++ b/talerbank/app/management/commands/provide_accounts.py @@ -45,11 +45,6 @@ def make_account(username): exc_info=True) sys.exit(1) -def basic_accounts(): - ensure_account("Bank") - ensure_account("Exchange") - - class Command(BaseCommand): help = "Provide initial user accounts" diff --git a/talerbank/app/middleware.py b/talerbank/app/middleware.py index ab4269a..88e3b1b 100644 --- a/talerbank/app/middleware.py +++ b/talerbank/app/middleware.py @@ -1,5 +1,6 @@ import logging from django.http import JsonResponse +from django.shortcuts import redirect from .models import BankAccount, BankTransaction from .views import \ (DebitLimitException, SameAccountException, @@ -11,52 +12,54 @@ from .amount import CurrencyMismatch, BadFormatAmount LOGGER = logging.getLogger() -EXCS = { - BankAccount.DoesNotExist: 0, - BankTransaction.DoesNotExist: 1, - SameAccountException: 2, - DebitLimitException: 3, - URLParameterMissing: 8, - URLParameterMalformed: 9, - JSONFieldException: 6, - CurrencyMismatch: 11, - BadFormatAmount: 11, - LoginFailed: 12, - RejectNoRightsException: 13, - UnknownCurrencyException: 14} - -APIS = { - "/reject": 5300, - "/history": 5200, - "/admin/add/incoming": 5100} - -RENDER = { - "/profile": "profile", - "/register": "index", - "/public-accounts": "index", - "/pin/verify": "profile"} - class ExceptionMiddleware: def __init__(self, get_response): self.get_response = get_response + self.excs = { + BankAccount.DoesNotExist: 0, + BankTransaction.DoesNotExist: 1, + SameAccountException: 2, + DebitLimitException: 3, + URLParameterMissing: 8, + URLParameterMalformed: 9, + JSONFieldException: 6, + CurrencyMismatch: 11, + BadFormatAmount: 11, + LoginFailed: 12, + RejectNoRightsException: 13, + UnknownCurrencyException: 14} + + self.apis = { + "/reject": 5300, + "/history": 5200, + "/admin/add/incoming": 5100} + + self.render = { + "/profile": "profile", + "/register": "index", + "/public-accounts": "index", + "/pin/verify": "profile"} + + def __call__(self, request): return self.get_response(request) def process_exception(self, request, exception): - if not EXCS.get(exception.__class__): + if not self.excs.get(exception.__class__): return None - taler_ec = EXCS.get(exception.__class__) + taler_ec = self.excs.get(exception.__class__) # The way error codes compose matches definitions found # at [1]. - taler_ec += APIS.get(request.path, 1000) - render_to = RENDER.get(request.path) + taler_ec += self.apis.get(request.path, 1000) + render_to = self.render.get(request.path) if not render_to: return JsonResponse({"ec": taler_ec, "error": exception.hint}, status=exception.http_status_code) - request.session["profile_hint"] = True, False, hint + request.session["profile_hint"] = \ + True, False, exception.hint return redirect(render_to) -# [1] https://git.taler.net/exchange.git/tree/src/include/taler_error_codes.h#n1502 +# [1] https://git.taler.net/exchange.git/tree/src/include/taler_error_codes.h#n1502 diff --git a/talerbank/app/schemas.py b/talerbank/app/schemas.py index 2ee925c..d001e60 100644 --- a/talerbank/app/schemas.py +++ b/talerbank/app/schemas.py @@ -1,16 +1,19 @@ # This file is part of TALER # (C) 2014, 2015, 2016 INRIA # -# TALER is free software; you can redistribute it and/or modify it under the -# terms of the GNU Affero General Public License as published by the Free Software -# Foundation; either version 3, or (at your option) any later version. +# TALER is free software; you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation; either version 3, or +# (at your option) any later version. # -# TALER is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# TALER is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> +# You should have received a copy of the GNU General Public +# License along with TALER; see the file COPYING. If not, see +# <http://www.gnu.org/licenses/> # # @author Marcello Stanisci @@ -31,21 +34,25 @@ class UnknownCurrencyException(ValueError): def __init__(self, hint, http_status_code): self.hint = hint self.http_status_code = http_status_code + super().__init__() class URLParameterMissing(ValueError): def __init__(self, param, http_status_code): self.hint = "URL parameter '%s' is missing" % param self.http_status_code = http_status_code + super().__init__() class URLParameterMalformed(ValueError): def __init__(self, param, http_status_code): self.hint = "URL parameter '%s' is malformed" % param self.http_status_code = http_status_code + super().__init__() class JSONFieldException(ValueError): def __init__(self, hint, http_status_code): self.hint = hint self.http_status_code = http_status_code + super().__init__() AMOUNT_SCHEMA = { "type": "object", @@ -120,7 +127,7 @@ HISTORY_REQUEST_SCHEMA = { "pattern": "^([0-9]+)$", "required": False}, "direction": {"type": "string", - "pattern": "^(debit|credit|both|cancel\+|cancel-)$"}, + "pattern": r"^(debit|credit|both|cancel\+|cancel-)$"}, "account_number": {"type": "string", "pattern": "^([0-9]+)$", "required": False} @@ -188,7 +195,7 @@ def validate_data(request, data): "/history": validate_history, "/admin/add/incoming": validate_add_incoming, "/pin/verify": check_withdraw_session, - "/pin/question": validate_pin_tan + "/pin/question": validate_pin_tan } try: switch.get(request.path_info)(data) @@ -204,4 +211,3 @@ def validate_data(request, data): raise URLParameterMalformed(exc.fieldname, 400) raise JSONFieldException( "Malformed '%s' field" % exc.fieldname, 400) - diff --git a/talerbank/app/templates/public_accounts.html b/talerbank/app/templates/public_accounts.html index 2623a10..e92edb6 100644 --- a/talerbank/app/templates/public_accounts.html +++ b/talerbank/app/templates/public_accounts.html @@ -25,6 +25,20 @@ <a href="{{ url('index') }}">Back</a> <section id="main"> <article> + {% if fail_message %} + <div class="notification"> + <p class="informational informational-fail"> + {{ hint }} + </p> + </div> + {% endif %} + {% if success_message %} + <div class="notification"> + <p class="informational informational-ok"> + {{ hint }} + </p> + </div> + {% endif %} <div name="accountMenu" class="pure-menu pure-menu-horizontal"> <ul class="pure-menu-list"> {% for account in public_accounts %} diff --git a/talerbank/app/views.py b/talerbank/app/views.py index 52e49ca..df5bbfc 100644 --- a/talerbank/app/views.py +++ b/talerbank/app/views.py @@ -17,15 +17,12 @@ # @author Marcello Stanisci # @author Florian Dold -from urllib.parse import urljoin from functools import wraps import json import logging -import time import hashlib import random import re -import requests import django.contrib.auth import django.contrib.auth.views import django.contrib.auth.forms @@ -39,11 +36,10 @@ from django.views.decorators.http import require_http_methods from django.urls import reverse from django.contrib.auth.models import User from django.db.models import Q -from django.http import (JsonResponse, HttpResponse, - HttpResponseBadRequest as HRBR) +from django.http import JsonResponse, HttpResponse from django.shortcuts import render, redirect from .models import BankAccount, BankTransaction -from .amount import Amount, CurrencyMismatch, BadFormatAmount +from .amount import Amount from .schemas import validate_data LOGGER = logging.getLogger(__name__) @@ -65,7 +61,7 @@ class RejectNoRightsException(Exception): http_status_code = 403 class MyAuthenticationForm( - django.contrib.auth.forms.AuthenticationForm): + django.contrib.auth.forms.AuthenticationForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["username"].widget.attrs["autofocus"] = True @@ -90,14 +86,14 @@ def login_view(request): return response def get_session_flag(request, name): - """ - Get a flag from the session and clear it. - """ - if name in request.session: - ret = request.session[name] - del request.session[name] - return ret - return False + """ + Get a flag from the session and clear it. + """ + if name in request.session: + ret = request.session[name] + del request.session[name] + return ret + return False def get_session_hint(request, name): """ @@ -130,21 +126,24 @@ class InputDatalist(forms.TextInput): {"list": "%slist" % name, "pattern": "[1-9]+"}) - def render(self, name, value, attrs=None): - html = super().render(name, value, attrs=attrs) + def render(self, name, value, attrs=None, renderer=None): + html = super().render( + name, value, attrs=attrs, renderer=renderer) datalist = '<datalist id="%slist">' % self._name - for value, text in self._datalist: + for dl_value, dl_text in self._datalist: datalist += '<option value="%s">%s</option>' \ - % (value, text) + % (dl_value, dl_text) datalist += "</datalist>" return html + datalist class WTForm(forms.Form): '''Form used to wire transfer money internally in the bank.''' - amount = forms.FloatField(min_value=0.1, + amount = forms.FloatField( + min_value=0.1, widget=forms.NumberInput(attrs={"class": "currency-input"})) - receiver = forms.IntegerField(min_value=1, + receiver = forms.IntegerField( + min_value=1, widget=InputDatalist(predefined_accounts_list, "receiver")) subject = forms.CharField() @@ -202,17 +201,17 @@ def hash_answer(ans): def make_question(): num1 = random.randint(1, 10) - op = random.choice(("*", "+", "-")) + operand = random.choice(("*", "+", "-")) num2 = random.randint(1, 10) - if op == "*": + if operand == "*": answer = str(num1 * num2) - elif op == "-": + elif operand == "-": # ensure result is positive num1, num2 = max(num1, num2), min(num1, num2) answer = str(num1 - num2) else: answer = str(num1 + num2) - question = "{} {} {}".format(num1, op, num2) + question = "{} {} {}".format(num1, operand, num2) return question, hash_answer(answer) @@ -221,9 +220,9 @@ def make_question(): def pin_tan_question(request): validate_data(request, request.GET.dict()) user_account = BankAccount.objects.get(user=request.user) - wd = json.loads(request.GET["exchange_wire_details"]) + wire_details = json.loads(request.GET["exchange_wire_details"]) request.session["exchange_account_number"] = \ - wd["test"]["account_number"] + wire_details["test"]["account_number"] amount = Amount(request.GET["amount_currency"], int(request.GET["amount_value"]), int(request.GET["amount_fraction"])) @@ -349,14 +348,22 @@ def extract_history(account): def serve_public_accounts(request, name=None): if not name: name = settings.TALER_PREDEFINED_ACCOUNTS[0] - user = User.objects.get(username=name) + user = User.objects.get(username=name) + 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) - history = extract_history(account) + history = extract_history(user.bankaccount) context = dict( public_accounts=public_accounts, selected_account=dict( + fail_message=fail_message, + success_message=success_message, + hint=hint, name=name, - number=account.account_no, + number=user.bankaccount.account_no, history=history, ) ) |