summaryrefslogtreecommitdiff
path: root/fbin/__init__.py
blob: 7bcdf0b279e18626c0d33afb8c1755fac4c37084 (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
from flask import Flask, url_for, Markup, request
from flask_login import current_user
from werkzeug.routing import BaseConverter
from werkzeug.middleware.proxy_fix import ProxyFix

app = Flask(__name__)
app.config.from_pyfile('fbin.cfg')
app.wsgi_app = ProxyFix(app.wsgi_app)


# Set up some custom converters. These are needed for file URLs to be properly parsed.
class HashConverter(BaseConverter):
    regex = r'\w+'


class ExtensionConverter(BaseConverter):
    regex = r'\.\w+'


@app.context_processor
def context_processors():
    def nav_html(view, name=None):
        url = url_for(view)
        if not name:
            name = view.rsplit('.', 1)[-1].replace('_', ' ').capitalize()
        if view == '.logout':
            name += ' [{}]'.format(current_user.username)
        return Markup('<li{}><a href="{}">{}</a></li>'.format(' class="active"' if url == request.path else '', url, name))
    return {
        'nav_html': nav_html,
    }


app.url_map.converters['hash'] = HashConverter
app.url_map.converters['ext'] = ExtensionConverter

with app.app_context():
    from .fbin import app as fbin, db
    from .api import app as api
    from .login import login_manager
    app.register_blueprint(fbin)
    app.register_blueprint(api, url_prefix='/api')
    login_manager.init_app(app)
    db.init_app(app)