forked from EpocDotFr/webtodotxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
53 lines (40 loc) · 1.43 KB
/
hooks.py
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
from flask import render_template, request, g, make_response
from werkzeug.exceptions import HTTPException
from webtodotxt import app, babel, auth
from helpers import get_current_auth_backend
@app.before_request
def set_locale():
if not hasattr(g, 'CURRENT_LOCALE'):
if app.config['FORCE_LANGUAGE']:
g.CURRENT_LOCALE = app.config['FORCE_LANGUAGE']
else:
g.CURRENT_LOCALE = request.accept_languages.best_match(app.config['LANGUAGES'].keys(),
default=app.config['DEFAULT_LANGUAGE'])
@auth.get_password
def get_password(username):
backend = get_current_auth_backend()
return backend.retrieve_password(username)
@auth.error_handler
def auth_error():
return http_error_handler(403, without_code=True)
@babel.localeselector
def get_app_locale():
if not hasattr(g, 'CURRENT_LOCALE'):
return app.config['DEFAULT_LANGUAGE']
else:
return g.CURRENT_LOCALE
@app.errorhandler(401)
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(500)
@app.errorhandler(503)
def http_error_handler(error, without_code=False):
if isinstance(error, HTTPException):
error = error.code
elif not isinstance(error, int):
error = 500
body = render_template('errors/{}.html'.format(error))
if not without_code:
return make_response(body, error)
else:
return make_response(body)