diff options
author | Marcello Stanisci <stanisci.m@gmail.com> | 2017-11-06 16:00:14 +0100 |
---|---|---|
committer | Marcello Stanisci <stanisci.m@gmail.com> | 2017-11-06 16:00:14 +0100 |
commit | 5761de4acc86bac945f7311b8cff8b7b886bb070 (patch) | |
tree | b91fa297de272e2736820c6ff7f6520b51b68f1f | |
parent | a52cdc012158842d3019d605eb0a2be83a9787a0 (diff) | |
download | bank-5761de4acc86bac945f7311b8cff8b7b886bb070.tar.gz bank-5761de4acc86bac945f7311b8cff8b7b886bb070.tar.bz2 bank-5761de4acc86bac945f7311b8cff8b7b886bb070.zip |
addressing obvious pylint warnings
-rw-r--r-- | talerbank/app/urls.py | 4 | ||||
-rw-r--r-- | talerbank/app/views.py | 110 |
2 files changed, 57 insertions, 57 deletions
diff --git a/talerbank/app/urls.py b/talerbank/app/urls.py index a437aa0..5a1ce34 100644 --- a/talerbank/app/urls.py +++ b/talerbank/app/urls.py @@ -30,9 +30,9 @@ urlpatterns = [ url(r'^profile$', views.profile_page, name="profile"), url(r'^history$', views.serve_history, name="history"), url(r'^withdraw$', views.withdraw_nojs, name="withdraw-nojs"), - url(r'^public-accounts$', views.public_accounts, name="public-accounts"), + url(r'^public-accounts$', views.serve_public_accounts, name="public-accounts"), url(r'^public-accounts/(?P<name>[a-zA-Z0-9 ]+)$', - views.public_accounts, + views.serve_public_accounts, name="public-accounts"), url(r'^pin/question$', views.pin_tan_question, name="pin-question"), url(r'^pin/verify$', views.pin_tan_verify, name="pin-verify"), diff --git a/talerbank/app/views.py b/talerbank/app/views.py index eb3320c..ab96c34 100644 --- a/talerbank/app/views.py +++ b/talerbank/app/views.py @@ -15,13 +15,13 @@ # @author Marcello Stanisci # @author Florian Dold +from urllib.parse import urljoin import json import logging import time import hashlib import re import validictory -from urllib.parse import urljoin import requests import django.contrib.auth import django.contrib.auth.views @@ -30,14 +30,17 @@ from django.db import transaction from django import forms from django.conf import settings from django.contrib.auth.decorators import login_required -from django.http import JsonResponse, HttpResponse, HttpResponseBadRequest, HttpResponseServerError +from django.http import (JsonResponse, + HttpResponse, + HttpResponseBadRequest as HRBR, + HttpResponseServerError) from django.shortcuts import render, redirect from django.views.decorators.csrf import csrf_exempt from django.views.decorators.http import require_POST, require_GET -from simplemathcaptcha.fields import MathCaptchaField, MathCaptchaWidget from django.core.urlresolvers import reverse from django.contrib.auth.models import User from django.db.models import Q +from simplemathcaptcha.fields import MathCaptchaField, MathCaptchaWidget from . import schemas from .models import BankAccount, BankTransaction from .amount import Amount, CurrencyMismatch, BadFormatAmount @@ -136,7 +139,7 @@ def pin_tan_question(request): # Currency is not checked, as any mismatches will be # detected afterwards except validictory.FieldValidationError as err: - return HttpResponseBadRequest("invalid '%s'" % err.fieldname) + return HRBR("invalid '%s'" % err.fieldname) user_account = BankAccount.objects.get(user=request.user) request.session["exchange_account_number"] = \ json.loads(request.GET["wire_details"])["test"]["account_number"] @@ -185,17 +188,19 @@ def pin_tan_verify(request): BankAccount.objects.get(user=request.user), exchange_bank_account, request.session["reserve_pub"]) - except BankAccount.DoesNotExist: - err = lambda: HttpResponseBadRequest("That exchange is unknown to this bank") - except DebtLimitExceededException: - LOGGER.warning("Withdrawal impossible due to debt limit exceeded") + except BankAccount.DoesNotExist as exc: + ctx = {"err": lambda: HRBR("That exchange is unknown to this bank"), + "exc": exc} + except DebtLimitExceededException as exc: request.session["debt_limit"] = True - err = lambda: redirect("profile") - except (SameAccountException, BadFormatAmount, CurrencyMismatch) as err: - LOGGER.error(err) - err = lambda: JsonResponse(dict(error="Internal server error", status=500)) + ctx = {"err": lambda: redirect("profile"), + "exc": exc} + except (SameAccountException, BadFormatAmount, CurrencyMismatch) as exc: + ctx = {"err": lambda: JsonResponse(dict(error="Internal server error", status=500)), + "exc": exc} if "err" in locals(): - return err() + LOGGER.error(ctx["exc"]) + return ctx["err"]() res = requests.post( urljoin(request.session["exchange_url"], "admin/add/incoming"), @@ -245,18 +250,16 @@ def register(request): wire_transfer(Amount(settings.TALER_CURRENCY, 100, 0), bank_internal_account, user_account, "Joining bonus") - except DebtLimitExceededException: - LOGGER.info("Debt situation encountered") + except (CurrencyMismatch, + BadFormatAmount, + SameAccountException) as exc: + exc = exc + except DebtLimitExceededException as exc: request.session["no_initial_bonus"] = True - return HttpResponseServerError() - except CurrencyMismatch: - LOGGER.error("Currency mismatch internal to the bank") - return HttpResponseServerError() - except BadFormatAmount: - LOGGER.error("Could not parse MAX_DEBT|MAX_BANK_DEBT") - return HttpResponseServerError() - except SameAccountException: - LOGGER.error("Odd situation: SameAccountException should NOT occur in this function") + exc = exc + + if "err" in locals(): + LOGGER.error(exc) return HttpResponseServerError() request.session["just_registered"] = True @@ -297,15 +300,13 @@ def extract_history(account): return history -def public_accounts(request, name=None): +def serve_public_accounts(request, name=None): if not name: name = settings.TALER_PREDEFINED_ACCOUNTS[0] try: user = User.objects.get(username=name) account = BankAccount.objects.get(user=user, is_public=True) - except User.DoesNotExist: - return HttpResponse("account '{}' not found".format(name), status=404) - except BankAccount.DoesNotExist: + except (User.DoesNotExist, BankAccount.DoesNotExist): return HttpResponse("account '{}' not found".format(name), status=404) public_accounts = BankAccount.objects.filter(is_public=True) history = extract_history(account) @@ -332,7 +333,7 @@ def serve_history(request): # delta delta = request.GET.get("delta") if not delta: - return HttpResponseBadRequest() + return HRBR() #FIXME: make the '+' sign optional parsed_delta = re.search(r"([\+-])?([0-9]+)", delta) try: @@ -347,13 +348,13 @@ def serve_history(request): sign = parsed_delta.group(1) - if ("+" == sign) or (not sign): + if (sign == "+") or (not sign): sign = "" # Assuming Q() means 'true' sign_filter = Q() - if "-" == sign and start: + if sign == "-" and start: sign_filter = Q(id__lt=start) - elif "" == sign and start: + elif sign == "" and start: sign_filter = Q(id__gt=start) # direction (debit/credit) direction = request.GET.get("direction") @@ -375,13 +376,13 @@ def serve_history(request): query_string = Q(debit_account=target_account) | Q(credit_account=target_account) history = [] - if "credit" == direction: + if direction == "credit": query_string = Q(credit_account=target_account) - if "debit" == direction: + if direction == "debit": query_string = Q(debit_account=target_account) qs = BankTransaction.objects.filter(query_string, sign_filter).order_by("%sid" % sign)[:delta] - if 0 == qs.count(): + if qs.count() == 0: return HttpResponse(status=204) for entry in qs: counterpart = entry.credit_account.account_no @@ -403,13 +404,12 @@ def auth_and_login(request): credentials, False if errors occur""" auth_type = None - if "POST" == request.method: + if request.method == "POST": data = json.loads(request.body.decode("utf-8")) auth_type = data["auth"]["type"] - if "GET" == request.method: + if request.method == "GET": auth_type = request.GET.get("auth") - - if "basic" != auth_type: + if auth_type != "basic": LOGGER.error("auth method not supported") return False @@ -457,15 +457,15 @@ def add_incoming(request): if settings.TALER_CURRENCY != data["amount"]["currency"]: LOGGER.error("Currency differs from bank's") return JsonResponse(dict(error="Currency differs from bank's"), status=406) - transaction = wire_transfer(Amount(**data["amount"]), - user_account.bankaccount, - credit_account, - subject) - return JsonResponse(dict(serial_id=transaction.id, + wtrans = wire_transfer(Amount(**data["amount"]), + user_account.bankaccount, + credit_account, + subject) + return JsonResponse(dict(serial_id=wtrans.id, timestamp="/Date(%s)/" % - int(transaction.date.timestamp()))) - except ValueError as e: - return JsonResponse(dict(error=e), status=400) + int(wtrans.date.timestamp()))) + except ValueError as exc: + return JsonResponse(dict(error=exc), status=400) except BadFormatAmount: LOGGER.error("Bad MAX_DEBT|MAX_BANK_DEBT format") @@ -492,7 +492,7 @@ def withdraw_nojs(request): amount = Amount.parse(request.POST.get("kudos_amount", "")) except BadFormatAmount: LOGGER.error("Amount did not pass parsing") - return HttpResponseBadRequest() + return HRBR() user_account = BankAccount.objects.get(user=request.user) @@ -533,7 +533,7 @@ def wire_transfer(amount, debit_account, credit_account, subject): if not credit_account.debit: credit_account.amount.add(amount) - elif 1 == Amount.cmp(amount, credit_account.amount): + elif Amount.cmp(amount, credit_account.amount) == 1: credit_account.debit = False tmp = Amount(**amount.dump()) tmp.subtract(credit_account.amount) @@ -547,13 +547,13 @@ def wire_transfer(amount, debit_account, credit_account, subject): threshold = Amount.parse(settings.TALER_MAX_DEBT) if debit_account.user.username == "Bank": threshold = Amount.parse(settings.TALER_MAX_DEBT_BANK) - if 1 == Amount.cmp(debit_account.amount, threshold) \ - and 0 != Amount.cmp(Amount(settings.TALER_CURRENCY), threshold) \ + 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()))) + 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(): |