[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [taler-bank] 01/02: still against the "too many return stat
From: |
gnunet |
Subject: |
[GNUnet-SVN] [taler-bank] 01/02: still against the "too many return statement" warning; fetch login credentials from the headers via a dedicated decorator. |
Date: |
Mon, 06 Nov 2017 18:41:37 +0100 |
This is an automated email from the git hooks/post-receive script.
marcello pushed a commit to branch master
in repository bank.
commit bdb278fc69616d4bdf3d55967d498345bb7c0fe7
Author: Marcello Stanisci <address@hidden>
AuthorDate: Mon Nov 6 18:28:07 2017 +0100
still against the "too many return statement" warning;
fetch login credentials from the headers via a dedicated decorator.
---
talerbank/app/views.py | 87 ++++++++++++++++++++++++++------------------------
1 file changed, 45 insertions(+), 42 deletions(-)
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index ab96c34..ecab92a 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -16,6 +16,7 @@
# @author Florian Dold
from urllib.parse import urljoin
+from functools import wraps
import json
import logging
import time
@@ -162,6 +163,8 @@ def pin_tan_question(request):
exchange=request.GET["exchange"])
return render(request, "pin_tan.html", context)
+def err_ctx(resp, msg):
+ return dict(resp=resp, msg=msg)
@require_POST
@login_required
@@ -189,18 +192,17 @@ def pin_tan_verify(request):
exchange_bank_account,
request.session["reserve_pub"])
except BankAccount.DoesNotExist as exc:
- ctx = {"err": lambda: HRBR("That exchange is unknown to this bank"),
- "exc": exc}
+ err = err_ctx(HRBR("That exchange is unknown to this bank"),
+ exc)
except DebtLimitExceededException as exc:
request.session["debt_limit"] = True
- ctx = {"err": lambda: redirect("profile"),
- "exc": exc}
+ err = err_ctx(redirect("profile"), exc)
except (SameAccountException, BadFormatAmount, CurrencyMismatch) as exc:
- ctx = {"err": lambda: JsonResponse(dict(error="Internal server error",
status=500)),
- "exc": exc}
+ err = err_ctx(JsonResponse({"error": "Internal server error"},
status=500),
+ exc)
if "err" in locals():
- LOGGER.error(ctx["exc"])
- return ctx["err"]()
+ LOGGER.error(err["msg"])
+ return err["resp"]()
res = requests.post(
urljoin(request.session["exchange_url"],
"admin/add/incoming"),
@@ -422,10 +424,21 @@ def auth_and_login(request):
return django.contrib.auth.authenticate(username=username,
password=password)
+def login_via_headers(view_func):
+ def _decorator(request, *args, **kwargs):
+ user_account = auth_and_login(request)
+ if not user_account:
+ LOGGER.error("authentication failed")
+ return JsonResponse(dict(error="authentication failed"),
+ status=401)
+ return view_func(request, user_account, *args, **kwargs)
+ return wraps(view_func)(_decorator)
+
@csrf_exempt
@require_POST
-def add_incoming(request):
address@hidden
+def add_incoming(request, user_account):
"""
Internal API used by exchanges to notify the bank
of incoming payments.
@@ -437,22 +450,12 @@ def add_incoming(request):
subject = "%s %s" % (data["wtid"], data["exchange_url"])
try:
schemas.validate_incoming_request(data)
- except ValueError as error:
- LOGGER.error("Bad data POSTed: %s" % error)
- return JsonResponse(dict(error="invalid data POSTed: %s" % error),
status=400)
-
- user_account = auth_and_login(request)
-
- if not user_account:
- LOGGER.error("authentication failed")
- return JsonResponse(dict(error="authentication failed"),
- status=401)
+ except ValueError as exc:
+ LOGGER.error(exc)
+ return JsonResponse({"error": exc}, status=400)
try:
credit_account = BankAccount.objects.get(user=data["credit_account"])
- except BankAccount.DoesNotExist:
- return HttpResponse(status=404)
- try:
schemas.validate_amount(data["amount"])
if settings.TALER_CURRENCY != data["amount"]["currency"]:
LOGGER.error("Currency differs from bank's")
@@ -461,28 +464,28 @@ def add_incoming(request):
user_account.bankaccount,
credit_account,
subject)
- return JsonResponse(dict(serial_id=wtrans.id,
- timestamp="/Date(%s)/" %
- int(wtrans.date.timestamp())))
+ except BankAccount.DoesNotExist:
+ return JsonResponse({"error": "credit_account not found"},
+ status=404)
except ValueError as exc:
- return JsonResponse(dict(error=exc), status=400)
-
- except BadFormatAmount:
- LOGGER.error("Bad MAX_DEBT|MAX_BANK_DEBT format")
- return JsonResponse(dict(error="Internal server error"),
- status=500)
- except CurrencyMismatch:
- LOGGER.error("Internal currency inconsistency")
- return JsonResponse(dict(error="Internal server error"),
- status=500)
+ err = err_ctx(JsonResponse({"error": exc}, status=400), exc)
+ except (CurrencyMismatch, BadFormatAmount) as exc:
+ err = err_ctx(JsonResponse({"error": "Internal server error"},
+ status=500),
+ exc)
except SameAccountException:
- return JsonResponse(dict(error="debit and credit account are the
same"),
- status=422)
- except DebtLimitExceededException:
- LOGGER.info("Prevenetd transfer, debit account would go beyond debt
threshold")
- return JsonResponse(dict(error="debit count has reached its debt
limit",
- status=403),
- status=403)
+ err = err_ctx(JsonResponse({"error":"same debit and credit account"},
+ status=422),
+ exc)
+ except DebtLimitExceededException as exc:
+ err = err_ctx(JsonResponse({"error": "debt situation"}, status=403),
+ exc)
+ if "err" in locals():
+ LOGGER.error(err["msg"])
+ return err["resp"]
+ return JsonResponse({"serial_id": wtrans.id,
+ "timestamp":
+ "/Date(%s)/" % int(wtrans.date.timestamp())})
@login_required
@require_POST
--
To stop receiving notification emails like this one, please contact
address@hidden