]> git.gir.st - subscriptionfeed.git/blob - app/__init__.py
avoid int(request.args.get('page'))
[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 def try_int(i):
22 try: int(i)
23 except: None
24 tmp = dict(request.args)
25 for field,what in fields.items():
26 if type(what) is tuple:
27 (plusminus, default) = what
28 tmp[field] = (try_int(tmp.get(field)) or int(default)) + plusminus
29 elif type(what) is type(None):
30 if field in tmp: del tmp[field]
31 else:
32 tmp[field] = what
33 from werkzeug.urls import url_encode
34 return url_encode(tmp)
35
36 # TODO: should this go somewhere else?
37 # 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.
38 from flask import g, request
39 from werkzeug.exceptions import InternalServerError
40 @app.errorhandler(InternalServerError)
41 def log_errors(e):
42 if request.method == "POST":
43 app.logger.error(request.data)
44 if 'api_requests' in g:
45 app.logger.error(g.api_requests)
46 return e
47
48 from .common import anticsrf
49 anticsrf.init(app)
Imprint / Impressum