diff options
Diffstat (limited to 'talermerchantdemos/survey/survey.py')
-rw-r--r-- | talermerchantdemos/survey/survey.py | 80 |
1 files changed, 52 insertions, 28 deletions
diff --git a/talermerchantdemos/survey/survey.py b/talermerchantdemos/survey/survey.py index 871b417..4813477 100644 --- a/talermerchantdemos/survey/survey.py +++ b/talermerchantdemos/survey/survey.py @@ -30,18 +30,21 @@ from flask_babel import force_locale from flask_babel import gettext import traceback from taler.util.talerconfig import TalerConfig, ConfigurationError -from ..httpcommon import backend_get, backend_post, self_localized +from ..httpcommon import backend_get, backend_post, self_localized, BackendException import sys if not sys.version_info.major == 3 and sys.version_info.minor >= 6: print("Python 3.6 or higher is required.") - print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor)) + print( + "You are using Python {}.{}.".format( + sys.version_info.major, sys.version_info.minor + ) + ) sys.exit(1) -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) -app = flask.Flask(__name__, template_folder=BASE_DIR) +app = flask.Flask(__name__, template_folder="../templates", static_folder="../static") app.debug = True -app.secret_key = base64.b64encode(os.urandom(64)).decode('utf-8') +app.secret_key = base64.b64encode(os.urandom(64)).decode("utf-8") LOGGER = logging.getLogger(__name__) TC = TalerConfig.from_env() @@ -53,22 +56,27 @@ except ConfigurationError as ce: print(ce) exit(1) +BABEL_TRANSLATION_DIRECTORIES = "../translations" + app.config.from_object(__name__) babel = Babel(app) INSTANCED_URL = urljoin(BACKEND_URL, f"instances/survey/") -LOGGER.info("Using translations from:" + ':'.join(list(babel.translation_directories))) +LOGGER.info("Using translations from:" + ":".join(list(babel.translation_directories))) translations = [str(translation) for translation in babel.list_translations()] -if not 'en' in translations: - translations.append('en') -LOGGER.info("Operating with the following translations available: " + ' '.join(translations)) +if not "en" in translations: + translations.append("en") +LOGGER.info( + "Operating with the following translations available: " + " ".join(translations) +) + +app.add_template_global(self_localized) -app.jinja_env.globals.update(self_localized=self_localized) @babel.localeselector def get_locale(): - parts = request.path.split('/', 2) - if (2 >= len(parts)): + parts = request.path.split("/", 2) + if 2 >= len(parts): # Totally unexpected path format, do not localize return "en" lang = parts[1] @@ -76,6 +84,7 @@ def get_locale(): return lang return "en" + ## # Make the environment available into templates. # @@ -106,23 +115,24 @@ def utility_processor(): @app.errorhandler(Exception) def internal_error(e): return flask.render_template( - "templates/error.html.j2", + "survey-error.html.j2", message=gettext("Internal error"), stack=traceback.format_exc(), - lang=get_locale() + lang=get_locale(), ) + ## # Serve the /favicon.ico requests. # # @return the favicon.ico file. @app.route("/favicon.ico") def favicon(): - LOGGER.info("will look into: " + os.path.join(app.root_path, 'static')) + LOGGER.info("will look into: " + os.path.join(app.root_path, "static")) return flask.send_from_directory( - os.path.join(app.root_path, 'static'), + os.path.join(app.root_path, "static"), "favicon.ico", - mimetype="image/vnd.microsoft.ico" + mimetype="image/vnd.microsoft.ico", ) @@ -139,21 +149,23 @@ def submit_survey(lang): tip_spec = dict( amount=CURRENCY + ":1.0", next_url=os.environ.get("TALER_ENV_URL_INTRO", "https://taler.net/"), - justification="Payment methods survey" + justification="Payment methods survey", ) - backend_resp = backend_post(INSTANCED_URL, "private/tips", tip_spec) + backend_resp = backend_post(INSTANCED_URL, "private/tips", tip_spec, auth_token=APIKEY) return flask.redirect(backend_resp["tip_status_url"]) + ## # Serve the main index page, redirecting to /<lang>/ # # @return response object of the index page. @app.route("/") def index(): - default = 'en' + default = "en" target = flask.request.accept_languages.best_match(translations, default) return flask.redirect("/" + target + "/", code=302) + ## # Serve the internationalized main index page. # @@ -161,21 +173,33 @@ def index(): @app.route("/<lang>/", methods=["GET"]) def start(lang): return flask.render_template( - "templates/index.html.j2", merchant_currency=CURRENCY, lang=lang + "survey-index.html.j2", merchant_currency=CURRENCY, lang=lang ) + @app.errorhandler(404) -def handler(e): +def handler_404(e): return flask.render_template( - "templates/error.html.j2", - message=gettext("Page not found"), - lang=get_locale() + "survey-error.html.j2", message=gettext("Page not found"), lang=get_locale() ) + @app.errorhandler(405) -def handler(e): +def handler_405(e): return flask.render_template( - "templates/error.html.j2", + "survey-error.html.j2", message=gettext("HTTP method not allowed for this page"), - lang=get_locale() + lang=get_locale(), + ) + + +@app.errorhandler(BackendException) +def handler_backend_exception(e): + t = flask.render_template( + "survey-error.html.j2", + lang=get_locale(), + message=e.args[0], + json=e.backend_json, + status_code=e.backend_status, ) + return flask.make_response(t, 500)
\ No newline at end of file |