summaryrefslogtreecommitdiff
path: root/talerbackoffice/backoffice/backoffice.py
blob: 8b6242b8f065111fc5d0c44afb068c1f4ed99849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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
# <http://www.gnu.org/licenses/>
#
# @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 flask_babel import Babel
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')
app.config.from_pyfile("trans_dirs.cfg")
babel = Babel(app)

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__)

LANGUAGES = {
    'de': 'German',
    'en': 'English',
    'it': 'Italian',
    'fr': 'French',
    'pt': 'Portuguese',
    'es': 'Spanish',
    'ru': 'Russian',
}

@babel.localeselector
def get_locale():
  # If any "lang" component exists in the path, then
  # it must be at the second position into the split array.
  lang = flask.request.path.split("/")[1]
  return lang if lang in LANGUAGES.keys() else "en"

@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("/<lang>/")
@app.route("/", defaults={"lang": "en"})
def index(lang):
    # 'lang' parameter not used.
    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