summaryrefslogtreecommitdiff
path: root/talersurvey/survey/survey.py
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2017-12-05 18:08:41 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2017-12-05 18:08:41 +0100
commit450921cef3476197d8ebc3909ee8246f428f7145 (patch)
treee7f97c000301677a7594ff1027756ebb3b3f62bf /talersurvey/survey/survey.py
parentfc348c3a69c3c36907ef6863748d677254ac382e (diff)
downloadsurvey-450921cef3476197d8ebc3909ee8246f428f7145.tar.gz
survey-450921cef3476197d8ebc3909ee8246f428f7145.tar.bz2
survey-450921cef3476197d8ebc3909ee8246f428f7145.zip
linting all but 'app' global name (enforced by Flask)
Diffstat (limited to 'talersurvey/survey/survey.py')
-rw-r--r--talersurvey/survey/survey.py71
1 files changed, 36 insertions, 35 deletions
diff --git a/talersurvey/survey/survey.py b/talersurvey/survey/survey.py
index d44db73..80b07d1 100644
--- a/talersurvey/survey/survey.py
+++ b/talersurvey/survey/survey.py
@@ -14,53 +14,53 @@
#
# @author Marcello Stanisci
-import flask
import os
import base64
-import requests
import logging
import json
-from .amount import Amount
-from talersurvey.talerconfig import TalerConfig
from urllib.parse import urljoin
+import flask
+import requests
+from talersurvey.talerconfig import TalerConfig
+from .amount import Amount
-
-base_dir = os.path.dirname(os.path.abspath(__file__))
-app = flask.Flask(__name__, template_folder=base_dir)
+BASE_DIR = os.path.dirname(os.path.abspath(__file__))
+app = flask.Flask(__name__, template_folder=BASE_DIR)
app.debug = True
app.secret_key = base64.b64encode(os.urandom(64)).decode('utf-8')
-tc = TalerConfig.from_env()
-BACKEND_URL = tc["frontends"]["backend"].value_string(required=True)
-CURRENCY = tc["taler"]["currency"].value_string(required=True)
+TC = TalerConfig.from_env()
+BACKEND_URL = TC["frontends"]["backend"].value_string(required=True)
+CURRENCY = TC["taler"]["currency"].value_string(required=True)
app.config.from_object(__name__)
-logger = logging.getLogger(__name__)
+LOGGER = logging.getLogger(__name__)
def backend_error(requests_response):
- logger.error("Backend error: status code: "
+ LOGGER.error("Backend error: status code: "
+ str(requests_response.status_code))
try:
return flask.jsonify(requests_response.json()), requests_response.status_code
except json.decoder.JSONDecodeError:
- logger.error("Backend error (NO JSON returned): status code: "
+ LOGGER.error("Backend error (NO JSON returned): status code: "
+ str(requests_response.status_code))
return flask.jsonify(dict(error="Backend died, no JSON got from it")), 502
@app.context_processor
def utility_processor():
+
def join_urlparts(*parts):
- s = ""
- i = 0
- while i < len(parts):
- n = parts[i]
- i += 1
- if s.endswith("/"):
- n = n.lstrip("/")
- elif s and not n.startswith("/"):
- n = "/" + n
- s += n
- return s
+ ret = ""
+ part = 0
+ while part < len(parts):
+ buf = parts[part]
+ part += 1
+ if ret.endswith("/"):
+ buf = buf.lstrip("/")
+ elif ret and not buf.startswith("/"):
+ buf = "/" + buf
+ ret += buf
+ return ret
def url(my_url):
return join_urlparts(flask.request.script_root, my_url)
@@ -72,11 +72,11 @@ def utility_processor():
@app.route("/tip-pickup", methods=["POST"])
def pick():
body = flask.request.get_json()
- r = requests.post(urljoin(BACKEND_URL, 'tip-pickup'), json=body)
- if 200 != r.status_code:
- return backend_error(r)
- return flask.jsonify(r.json())
-
+ resp = requests.post(urljoin(BACKEND_URL, 'tip-pickup'),
+ json=body)
+ if resp.status_code != 200:
+ return backend_error(resp)
+ return flask.jsonify(resp.json())
@app.route("/submit-survey", methods=["POST"])
def submit_survey():
@@ -84,14 +84,15 @@ def submit_survey():
amount=Amount(CURRENCY, 1).dump(),
instance="default",
justification="Payment methods survey")
- r = requests.post(urljoin(BACKEND_URL, 'tip-authorize'), json=tip_spec)
- if 200 != r.status_code:
- return backend_error(r)
+ resp = requests.post(urljoin(BACKEND_URL, 'tip-authorize'),
+ json=tip_spec)
+ if resp.status_code != 200:
+ return backend_error(resp)
response = flask.make_response(
- flask.render_template("templates/wait.html", success=True),
- 402)
- response.headers["X-Taler-Tip"] = r.json()["tip_token"]
+ flask.render_template("templates/wait.html", success=True),
+ 402)
+ response.headers["X-Taler-Tip"] = resp.json()["tip_token"]
return response