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

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

# 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.url_map.converters['hash'] = HashConverter
app.url_map.converters['ext'] = ExtensionConverter

@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,
    }

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)