summaryrefslogtreecommitdiffstats
path: root/bitvend/__init__.py
blob: 2ab00a671e316253cc5eaca176fb2cb27c7a1949 (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
import flask
import os

from bitvend.processor import PaymentProcessor
from bitvend.mdb import BitvendCashlessMDBDevice
from spaceauth import SpaceAuth

dev = BitvendCashlessMDBDevice()
proc = PaymentProcessor(dev)
spaceauth = SpaceAuth()

from bitvend.utils import to_local_currency, from_local_currency, format_btc, \
    sat_to_btc
from bitvend.models import db, Transaction, User

import bitvend.views
import bitvend.admin

@spaceauth.user_loader
def bitvend_user_loader(username, profile=None):
    u = User.find(username)

    if not u:
        u = User(uid=username)
        db.session.add(u)
        db.session.commit()

    return u

def create_app():
    app = flask.Flask(__name__)
    app.config.from_object('bitvend.default_settings')
    print('Loading extra settings from {}...'.format(os.environ.get('BITVEND_SETTINGS', '')))
    app.config.from_pyfile(os.environ.get('BITVEND_SETTINGS', ''), silent=True)

    # Use proper proxy headers, this fixes invalid scheme in
    # url_for(_external=True)
    from werkzeug.contrib.fixers import ProxyFix
    app.wsgi_app = ProxyFix(app.wsgi_app)

    db.init_app(app)
    spaceauth.init_app(app)
    dev.init_app(app)
    proc.init_app(app)

    app.register_blueprint(bitvend.views.bp)
    app.register_blueprint(bitvend.admin.bp, url_prefix='/admin')

    @app.context_processor
    def ctx_utils():
        return {
            'from_local_currency': from_local_currency,
            'to_local_currency': to_local_currency,
            'format_btc': format_btc,
            'sat_to_btc': sat_to_btc,
            'qrcode': lambda data: flask.url_for('bitvend.qrcode_gen', data=data),
            'current_transaction': Transaction.query.filter(Transaction.finished == False).first(),
            'mdb_online': dev.online,
            'proc_online': proc.online,
            'static': lambda fn, **kwargs: flask.url_for('static', filename=fn,
                                                         **kwargs)
        }

    def url_for_other_page(page):
        args = flask.request.view_args.copy()
        args['page'] = page
        return flask.url_for(flask.request.endpoint, **args)
    app.jinja_env.globals['url_for_other_page'] = url_for_other_page

    return app