diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2017-12-05 17:28:12 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2017-12-05 17:28:12 +0100 |
commit | 4bacbbeb8d6a3fde2abcbe2806753615b08dba84 (patch) | |
tree | 940141b1b2a9fda42ab66e0735929ba0cf386e80 | |
parent | 19e9dacab749c8a3f70919f53ac7cb162b4af0b1 (diff) | |
download | bank-4bacbbeb8d6a3fde2abcbe2806753615b08dba84.tar.gz bank-4bacbbeb8d6a3fde2abcbe2806753615b08dba84.tar.bz2 bank-4bacbbeb8d6a3fde2abcbe2806753615b08dba84.zip |
done with linting
-rw-r--r-- | talerbank/app/views.py | 151 |
1 files changed, 75 insertions, 76 deletions
diff --git a/talerbank/app/views.py b/talerbank/app/views.py index 7f8aed4..8e2e452 100644 --- a/talerbank/app/views.py +++ b/talerbank/app/views.py @@ -505,84 +505,83 @@ class WireTransferException(Exception): self.response = response super().__init__() -def wire_transfer_exc_handler(view_func): +def wire_transfer(amount, debit_account, credit_account, subject, **kwargs): + def err_cb(exc, resp): LOGGER.error(str(exc)) raise WireTransferException(exc, resp) - def _decorator(*args, request=None, session_expand=None): - try: - return view_func(*args) - except (CurrencyMismatch, BadFormatAmount) as exc: - err_cb(exc, JsonResponse({"error": "internal server error"}, - status=500)) - except DebtLimitExceededException as exc: - if request: - if session_expand: - request.session.update(session_expand) - if request.path == "/pin/verify": - err_cb(exc, redirect("profile")) - else: - err_cb(exc, JsonResponse({"error": "Unallowed debit"}, - status=403)) - except SameAccountException as exc: - err_cb(exc, JsonResponse({"error": "sender account == receiver account"}, - status=422)) - return wraps(view_func)(_decorator) - -@wire_transfer_exc_handler -def wire_transfer(amount, debit_account, credit_account, subject, **kwargs): - LOGGER.info("%s => %s, %s, %s" % - (debit_account.account_no, - credit_account.account_no, - amount.stringify(2), - subject)) - if debit_account.pk == credit_account.pk: - LOGGER.error("Debit and credit account are the same!") - raise SameAccountException() - - transaction_item = BankTransaction(amount=amount, - credit_account=credit_account, - debit_account=debit_account, - subject=subject) - if debit_account.debit: - debit_account.amount.add(amount) - - elif -1 == Amount.cmp(debit_account.amount, amount): - debit_account.debit = True - tmp = Amount(**amount.dump()) - tmp.subtract(debit_account.amount) - debit_account.amount.set(**tmp.dump()) - else: - debit_account.amount.subtract(amount) - - if not credit_account.debit: - credit_account.amount.add(amount) - elif Amount.cmp(amount, credit_account.amount) == 1: - credit_account.debit = False - tmp = Amount(**amount.dump()) - tmp.subtract(credit_account.amount) - credit_account.amount.set(**tmp.dump()) - else: - credit_account.amount.subtract(amount) - - # Check here if any account went beyond the allowed - # debit threshold. - - threshold = Amount.parse(settings.TALER_MAX_DEBT) - if debit_account.user.username == "Bank": - threshold = Amount.parse(settings.TALER_MAX_DEBT_BANK) - if Amount.cmp(debit_account.amount, threshold) == 1 \ - and Amount.cmp(Amount(settings.TALER_CURRENCY), threshold) != 0 \ - and debit_account.debit: - LOGGER.info("Negative balance '%s' not allowed.\ - " % json.dumps(debit_account.amount.dump())) - LOGGER.info("%s's threshold is: '%s'.\ - " % (debit_account.user.username, json.dumps(threshold.dump()))) - raise DebtLimitExceededException() - with transaction.atomic(): - debit_account.save() - credit_account.save() - transaction_item.save() + def wire_transfer_internal(amount, debit_account, credit_account, subject): + LOGGER.info("%s => %s, %s, %s" % + (debit_account.account_no, + credit_account.account_no, + amount.stringify(2), + subject)) + if debit_account.pk == credit_account.pk: + LOGGER.error("Debit and credit account are the same!") + raise SameAccountException() + + transaction_item = BankTransaction(amount=amount, + credit_account=credit_account, + debit_account=debit_account, + subject=subject) + if debit_account.debit: + debit_account.amount.add(amount) + + elif -1 == Amount.cmp(debit_account.amount, amount): + debit_account.debit = True + tmp = Amount(**amount.dump()) + tmp.subtract(debit_account.amount) + debit_account.amount.set(**tmp.dump()) + else: + debit_account.amount.subtract(amount) + + if not credit_account.debit: + credit_account.amount.add(amount) + elif Amount.cmp(amount, credit_account.amount) == 1: + credit_account.debit = False + tmp = Amount(**amount.dump()) + tmp.subtract(credit_account.amount) + credit_account.amount.set(**tmp.dump()) + else: + credit_account.amount.subtract(amount) + + # Check here if any account went beyond the allowed + # debit threshold. + + threshold = Amount.parse(settings.TALER_MAX_DEBT) + if debit_account.user.username == "Bank": + threshold = Amount.parse(settings.TALER_MAX_DEBT_BANK) + if Amount.cmp(debit_account.amount, threshold) == 1 \ + and Amount.cmp(Amount(settings.TALER_CURRENCY), threshold) != 0 \ + and debit_account.debit: + LOGGER.info("Negative balance '%s' not allowed.\ + " % json.dumps(debit_account.amount.dump())) + LOGGER.info("%s's threshold is: '%s'.\ + " % (debit_account.user.username, json.dumps(threshold.dump()))) + raise DebtLimitExceededException() + + with transaction.atomic(): + debit_account.save() + credit_account.save() + transaction_item.save() + + return transaction_item - return transaction_item + try: + return wire_transfer_internal(amount, debit_account, credit_account, subject) + except (CurrencyMismatch, BadFormatAmount) as exc: + err_cb(exc, JsonResponse({"error": "internal server error"}, + status=500)) + except DebtLimitExceededException as exc: + if kwargs.get("request"): + if kwargs.get("session_expand"): + kwargs["request"].session.update(kwargs["session_expand"]) + if kwargs["request"].request.path == "/pin/verify": + err_cb(exc, redirect("profile")) + else: + err_cb(exc, JsonResponse({"error": "Unallowed debit"}, + status=403)) + except SameAccountException as exc: + err_cb(exc, JsonResponse({"error": "sender account == receiver account"}, + status=422)) |