# This file is part of GNU TALER. # Copyright (C) 2014-2017 INRIA # # TALER is free software; you can redistribute it and/or modify it under the # terms of the GNU Lesser General Public License as published by the Free Software # Foundation; either version 2.1, or (at your option) any later version. # # TALER is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License along with # GNU TALER; see the file COPYING. If not, see # # @author Florian Dold # @author Marcello Stanisci """ Implement URL handlers for backoffice logic. """ from urllib.parse import urljoin, parse_qsl import logging import os import base64 import requests import flask from talerbackoffice.talerconfig import TalerConfig from ..helpers import (join_urlparts, \ get_query_string, backend_error) LOGGER = logging.getLogger(__name__) 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 = os.environ.get("BACKOFFICE_BACKEND") INSTANCES = os.environ.get("BACKOFFICE_INSTANCES") CURRENCY = TC["taler"]["currency"].value_string(required=True) app.config.from_object(__name__) @app.context_processor def utility_processor(): def url(my_url): return join_urlparts(flask.request.script_root, my_url) def env(name, default=None): return os.environ.get(name, default) return dict(url=url, env=env) @app.route("/") def index(): return flask.render_template("templates/backoffice.html", instances=INSTANCES.split()) @app.route("/javascript") def javascript_licensing(): return flask.render_template("templates/javascript.html") @app.route("/history") def history(): qs = get_query_string().decode("utf-8") url = urljoin(BACKEND_URL, "history") resp = requests.get(url, params=dict(parse_qsl(qs)), headers={"Authorization": "ApiKey sandbox"}) if resp.status_code != 200: return backend_error(resp) return flask.jsonify(resp.json()), resp.status_code @app.route("/track/transfer") def track_transfer(): qs = get_query_string().decode("utf-8") url = urljoin(BACKEND_URL, "track/transfer") resp = requests.get (url, params=dict(parse_qsl(qs)), headers={"Authorization": "ApiKey sandbox"}) if resp.status_code != 200: return backend_error(resp) return flask.jsonify(resp.json()), resp.status_code @app.route("/track/order") def track_order(): qs = get_query_string().decode("utf-8") url = urljoin(BACKEND_URL, "track/transaction") resp = requests.get(url, params=dict(parse_qsl(qs)), headers={"Authorization": "ApiKey sandbox"}) if resp.status_code != 200: return backend_error(resp) return flask.jsonify(resp.json()), resp.status_code