diff options
author | Marcello Stanisci <marcello.stanisci@inria.fr> | 2016-03-18 15:04:51 +0100 |
---|---|---|
committer | Marcello Stanisci <marcello.stanisci@inria.fr> | 2016-03-18 15:04:51 +0100 |
commit | 7382993df30dd0393abe9ff9fb623df1d85dc582 (patch) | |
tree | ecbd1da3eaee18db2a6a3f0cb4a9bcb1f78fdd22 | |
parent | 836c314fe92703c33370ef5fbca8b3b836eed2ad (diff) | |
download | bank-7382993df30dd0393abe9ff9fb623df1d85dc582.tar.gz bank-7382993df30dd0393abe9ff9fb623df1d85dc582.tar.bz2 bank-7382993df30dd0393abe9ff9fb623df1d85dc582.zip |
more on runtime exeption handling, plus docs
-rw-r--r-- | INSTALL | 31 | ||||
-rw-r--r-- | django/bank_app/captcha.py | 9 | ||||
-rw-r--r-- | django/bank_app/errors.py | 6 | ||||
-rw-r--r-- | django/bank_app/funds_mgmt.py | 8 | ||||
-rw-r--r-- | django/bank_app/history.py | 13 | ||||
-rw-r--r-- | django/bank_app/lib.py | 20 | ||||
-rw-r--r-- | django/bank_app/user_mgmt.py | 25 | ||||
-rw-r--r-- | django/bank_app/util.py | 6 | ||||
-rw-r--r-- | django/taler-bank-manage.sh.in | 9 |
9 files changed, 86 insertions, 41 deletions
@@ -96,10 +96,35 @@ $ ./configure $ make $ make install -Though not strictly needed, the following step populates the database with -sample data +At this point a DB-less bank should be installed. In order to operate with it, +the following three steps must be matched (*): -$ make populatedb +1) a DB called 'talertest' must exist, with the user running the bank having +owning it; try: + +$ taler-bank-manage --createdb OWNER # OWNER will own this DB + +In case of failure, see the Preface above + +2) some canonical tables must be defined on 'talertest' + +$ taler-bank-manage --defintables + +3) some predefined accounts must be created + +$ taler-bank-manage --preaccounts + +4) Optional, to populate the DB with sample data, issue + +$ taler-bank-manage --sampledata + +(*) Steps 2-4 are embedded in the single command + +$ taler-bank-manage --all + +If in doubt, issue + +$ taler-bank-manage --help If the previous steps were successful, then the bank should be run with diff --git a/django/bank_app/captcha.py b/django/bank_app/captcha.py index 54aa111..45eb845 100644 --- a/django/bank_app/captcha.py +++ b/django/bank_app/captcha.py @@ -25,6 +25,7 @@ from bank_app.schemas import Schemas from bank_app.lib import (write_log_file, get_currency, get_bank_account_from_username, + check_db, get_pretty_exchange_details, floatify_amount) from simplemathcaptcha.fields import (MathCaptchaField, @@ -77,10 +78,9 @@ def pin_tan_question_attempt(request): 'exchange': get_pretty_exchange_details(request.GET['exchange'])}) +@is_logged def pin_tan_question(request): try: return pin_tan_question_attempt(request) - except UserNotLogged: - return user_not_logged_handler(request) except MissingGetParameter as e: return JsonResponse({'reason': e.par + " missing in query parameters"}, status=400) except BadAmount: @@ -90,6 +90,8 @@ def pin_tan_question(request): except BadGetParameter: return bad_get_parameter_handler(request) +@check_db +@is_logged def pin_tan_verify(request): try: return pin_tan_verify_attempt(request) except WrongMethod as e: @@ -99,14 +101,13 @@ def pin_tan_verify(request): except BadPostValue: return bad_post_value_handler(request) -@is_logged def pin_tan_verify_attempt(request): if request.method != 'POST': raise WrongMethod("POST") try: given = request.POST['pin_0'] hashed_result = request.POST['pin_1'] - except Exception: + except Exception: # FIXME narrow the Exception type raise BadPostValue hasher = hashlib.new("sha1") hasher.update(settings.SECRET_KEY) diff --git a/django/bank_app/errors.py b/django/bank_app/errors.py index 6a0cef3..8f94816 100644 --- a/django/bank_app/errors.py +++ b/django/bank_app/errors.py @@ -57,6 +57,12 @@ class NoTalerDatabase(Exception): class NoBankAccount(Exception): pass +def no_bank_account_handler(request): + return internal_error_handler(request, + "(The bank itself has no account," + + " please run 'taler-bank-manage --preaccounts')") + + def non_existent_db_handler(request): return internal_error_handler(request, "(db 'talertest' does not exist)") diff --git a/django/bank_app/funds_mgmt.py b/django/bank_app/funds_mgmt.py index a2e6621..8d55f49 100644 --- a/django/bank_app/funds_mgmt.py +++ b/django/bank_app/funds_mgmt.py @@ -42,6 +42,7 @@ from bank_app.models import (BankAccount, History) from django.shortcuts import (render, redirect) +from django.db.utils import OperationalError from django import forms from urllib.parse import (urlparse, urljoin) @@ -89,6 +90,8 @@ def add_incoming_attempt(request): return JsonResponse({'reason' : 'credit or debit account does not exist'}, status=400) except BankAccount.MultipleObjectsReturned: return JsonResponse({'reason' : 'internal error (collision in db)'}, status=500) + except OperationalError: + return JsonResponse({'reason' : 'internal error (bank has no db)'}, status=500) except CurrencyMismatch: return JsonResponse({'reason' : 'currency mismatch between credit/debit account and amount'}, status=500) @@ -109,10 +112,9 @@ def add_incoming(request): def withdraw_attempt(request): return render(request, 'withdraw.html', {'account_no' : request.session["account_no"]}) +@is_logged def withdraw_process(request): - try: return withdraw_attempt(request) - except UserNotLogged: - return user_not_logged_handler(request) + return withdraw_attempt(request) def create_reserve_at_exchange(request, success_url, reserve_set): if not isinstance(reserve_set, Reserve): diff --git a/django/bank_app/history.py b/django/bank_app/history.py index cb599ae..4faa289 100644 --- a/django/bank_app/history.py +++ b/django/bank_app/history.py @@ -21,6 +21,7 @@ from bank_app.lib import (get_logged_user_account, get_bank_account_from_username, get_relative_parameter, write_log_file, + check_db, is_logged, get) from bank_app.errors import (UserNotLogged, @@ -43,24 +44,26 @@ def extract_history(bank_account): 'date': item.date.strftime("%d/%m/%y")}) return ret -@is_logged def history_attempt(request): user_bank_account = get_logged_user_account(request.session['account_no']) render_history = [] render_history = extract_history(user_bank_account) return render(request, 'history.html', {'history': render_history}) +@check_db +@is_logged def history_process(request): - try: return history_attempt(request) - except UserNotLogged: - return user_not_logged_handler(request) + return history_attempt(request) +@check_db def public_accounts_process(request): + return public_accounts_attempt(request) + +def public_accounts_attempt(request): accounts = [] for item in get_public_accounts(): accounts.append({'account_name': item.user.username}) sel_account_name = get(request.GET, 'account') - #sel_account_name = get_relative_parameter(request, 'public-accounts') if not sel_account_name: return redirect("/public-accounts/?account=Tor") sel_account = get_bank_account_from_username(sel_account_name) diff --git a/django/bank_app/lib.py b/django/bank_app/lib.py index 7f12d1a..1afbc73 100644 --- a/django/bank_app/lib.py +++ b/django/bank_app/lib.py @@ -20,17 +20,20 @@ from django.http import HttpResponse, JsonResponse from django.shortcuts import render from django.core.exceptions import ObjectDoesNotExist from bank_app.models import BankAccount, History -from bank_app.errors import (UserNotLogged, - SubPathNotFound, +from bank_app.errors import (SubPathNotFound, + user_not_logged_handler, NoTalerDatabase, ExchangeUnknown, CurrencyMismatch, + internal_error_handler, + no_bank_account_handler, non_existent_db_handler) from urllib.parse import (urljoin, urlparse) from os.path import split from django.contrib.auth.models import User -from django.db.utils import OperationalError +from django.db.utils import (OperationalError, + ProgrammingError) def get(arr, key, default=False): if key not in arr: @@ -111,15 +114,20 @@ def check_db(fn): def fake_query(request): try: User.objects.get(username='Bank') except User.DoesNotExist: - raise NoBankAccount + return no_bank_account_handler(request) + except ProgrammingError: + return internal_error_handler(request, + "(db available but no tables in it. Run" + + " 'taler-bank-manage --definetables')") except OperationalError: - raise NoTalerDatabase + return non_existent_db_handler(request) + return fn(request) return fake_query def is_logged(fn): def check_login(request): if "account_no" not in request.session: - raise UserNotLogged + return user_not_logged_handler(request) return fn(request) return check_login diff --git a/django/bank_app/user_mgmt.py b/django/bank_app/user_mgmt.py index 9dfbf5a..0c0ae89 100644 --- a/django/bank_app/user_mgmt.py +++ b/django/bank_app/user_mgmt.py @@ -36,7 +36,8 @@ from bank_app.errors import (UserNotLogged, NoTalerDatabase, NoBankAccount, user_not_logged_handler, - non_existent_db_handler) + non_existent_db_handler, + no_bank_account_handler) from bank_app.history import extract_history from bank_app.config import pre_accounts from bank_app.lib import wire_transfer_in_out @@ -88,7 +89,6 @@ def register_attempt(request): 'currency': get_currency(request), 'not_available' : not_available}) -@check_db def login_attempt(request): if "account_no" in request.session: return get_central_page(request) @@ -112,28 +112,21 @@ def login_attempt(request): form = LoginForm() return render(request, 'login.html', {'form': form, 'currency': get_currency(request)}) +@check_db def login_process(request): - try: return login_attempt(request) - except NoTalerDatabase: - return non_existent_db_handler(request) - except NoBankAccount: - return internal_error_handler(request, "No predefined 'Bank' account") + return login_attempt(request) -@is_logged def logout_attempt(request): del request.session["account_no"] logout(request) request.session['logged_out'] = True return redirect("/") +@check_db +@is_logged def logout_process(request): - try: return logout_attempt(request) - except UserNotLogged: - return user_not_logged_handler(request) + return logout_attempt(request) +@check_db def register_process(request): - try: return register_attempt(request) - except NoTalerDatabase: - return non_existent_db_handler(request) - except NoBankAccount: - return internal_error_handler(request, "No predefined 'Bank' account") + return register_attempt(request) diff --git a/django/bank_app/util.py b/django/bank_app/util.py index 52773a1..ef45905 100644 --- a/django/bank_app/util.py +++ b/django/bank_app/util.py @@ -45,12 +45,10 @@ def home_page(request, wrong=False): 'wrong': wrong}) +@is_logged def profile_process(request): - try: return redirect_to_central_page(request) - except UserNotLogged: - return user_not_logged_handler(request) + return redirect_to_central_page(request) -@is_logged def redirect_to_central_page(request): if 'registration_successful' in request.session: del request.session['registration_successful'] diff --git a/django/taler-bank-manage.sh.in b/django/taler-bank-manage.sh.in index 47f3112..8cdbc34 100644 --- a/django/taler-bank-manage.sh.in +++ b/django/taler-bank-manage.sh.in @@ -27,6 +27,7 @@ fi function usage(){ echo -e "Usage: taler-bank-manage [options]\n\t\ + --all\tDefine tables, create preaccounts, provide sample data\n\t\ --definetables\tDefine DB tables needed by the bank\n\t\ --flushdata\tDelete any data/table from DB\n\t\ --dumpdata\tDump DB in JSON format\n\t\ @@ -56,6 +57,14 @@ EMPTYARGS=0 while true; do case $1 in + --all) + db_exists + python manage.py makemigrations + python manage.py migrate + python manage.py pre_accounts + python manage.py sample_donations + shift + ;; --definetables) echo "Creating tables..." EMPTYARGS=1 |