]> git.gir.st - subscriptionfeed.git/blob - app/__init__.py
move anticsrf out of __init__, provide decorator for opting out
[subscriptionfeed.git] / app / __init__.py
1 import base64
2 import secrets
3 import importlib
4 from flask import Flask
5
6 from .common.common import *
7 from .common.user import init_login
8
9 app = Flask(__name__)
10 app.secret_key = base64.b64decode(cf['frontend'].get('secret_key','')) or \
11 secrets.token_bytes(16) # development fallback; CSRF/cookies won't persist.
12 init_login(app)
13
14 for name in cf['frontend']['modules'].split(','):
15 blueprint = importlib.import_module('.'+name, __name__)
16 app.register_blueprint(blueprint.frontend)
17
18 # TODO: move this somewhere else
19 @app.template_global()
20 def querystring_page(fields):
21 tmp = dict(request.args)
22 for field,what in fields.items():
23 if type(what) is tuple:
24 (plusminus, default) = what
25 tmp[field] = int(tmp.get(field) or str(default)) + plusminus
26 elif type(what) is type(None):
27 if field in tmp: del tmp[field]
28 else:
29 tmp[field] = what
30 from werkzeug.urls import url_encode
31 return url_encode(tmp)
32
33 # TODO: should this go somewhere else?
34 # This error handler logs requests to external apis, and POST data. this makes debugging of api responses easier, as the request can be reconstructed and replayed.
35 from flask import g, request
36 from werkzeug.exceptions import InternalServerError
37 @app.errorhandler(InternalServerError)
38 def log_errors(e):
39 if request.method == "POST":
40 app.logger.error(request.data)
41 if 'api_requests' in g:
42 app.logger.error(g.api_requests)
43 return e
44
45 from .common import anticsrf
46 anticsrf.init(app)
Imprint / Impressum