2019-12-06 10:35:21 +02:00
|
|
|
from mailu import models, limiter
|
2018-10-18 17:55:07 +02:00
|
|
|
|
2018-10-18 15:57:43 +02:00
|
|
|
import flask
|
|
|
|
import flask_login
|
|
|
|
import flask_script
|
|
|
|
import flask_migrate
|
|
|
|
import flask_babel
|
|
|
|
|
2018-10-18 17:55:07 +02:00
|
|
|
from werkzeug.contrib import fixers
|
|
|
|
|
2018-10-18 15:57:43 +02:00
|
|
|
|
|
|
|
# Login configuration
|
|
|
|
login = flask_login.LoginManager()
|
|
|
|
login.login_view = "ui.login"
|
|
|
|
|
2018-10-18 17:55:07 +02:00
|
|
|
@login.unauthorized_handler
|
2018-10-18 15:57:43 +02:00
|
|
|
def handle_needs_login():
|
|
|
|
return flask.redirect(
|
|
|
|
flask.url_for('ui.login', next=flask.request.endpoint)
|
|
|
|
)
|
|
|
|
|
2019-12-06 10:35:21 +02:00
|
|
|
# Rate limiter
|
2020-02-09 18:38:18 +02:00
|
|
|
limiter = limiter.LimitWraperFactory()
|
2018-10-18 15:57:43 +02:00
|
|
|
|
|
|
|
# Application translation
|
|
|
|
babel = flask_babel.Babel()
|
|
|
|
|
|
|
|
@babel.localeselector
|
|
|
|
def get_locale():
|
2021-04-03 10:33:08 +02:00
|
|
|
try:
|
|
|
|
language = flask.session['language']
|
|
|
|
except KeyError:
|
|
|
|
language = None
|
|
|
|
|
|
|
|
if language is not None:
|
|
|
|
return language
|
|
|
|
|
2018-10-18 15:57:43 +02:00
|
|
|
translations = list(map(str, babel.list_translations()))
|
|
|
|
return flask.request.accept_languages.best_match(translations)
|
|
|
|
|
|
|
|
# Proxy fixer
|
|
|
|
class PrefixMiddleware(object):
|
|
|
|
def __call__(self, environ, start_response):
|
|
|
|
prefix = environ.get('HTTP_X_FORWARDED_PREFIX', '')
|
|
|
|
if prefix:
|
|
|
|
environ['SCRIPT_NAME'] = prefix
|
|
|
|
return self.app(environ, start_response)
|
|
|
|
|
|
|
|
def init_app(self, app):
|
2020-06-28 23:27:45 +02:00
|
|
|
self.app = fixers.ProxyFix(app.wsgi_app, x_for=1, x_proto=1)
|
2018-10-18 15:57:43 +02:00
|
|
|
app.wsgi_app = self
|
|
|
|
|
|
|
|
proxy = PrefixMiddleware()
|
2018-11-08 21:32:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Data migrate
|
|
|
|
migrate = flask_migrate.Migrate()
|