canaille-globuzma/canaille/__init__.py

127 lines
3.3 KiB
Python
Raw Normal View History

2021-07-01 09:04:57 +00:00
import datetime
import sys
2020-08-17 13:49:48 +00:00
2021-12-20 22:57:27 +00:00
from flask import Flask
from flask import request
2021-12-20 22:57:27 +00:00
from flask import session
2023-03-28 18:30:29 +00:00
from flask_wtf.csrf import CSRFProtect
2020-09-01 15:11:30 +00:00
2024-10-28 21:17:47 +00:00
from canaille.app.forms import password_strength_calculator
2023-03-28 18:30:29 +00:00
csrf = CSRFProtect()
2020-08-17 13:49:48 +00:00
def setup_sentry(app): # pragma: no cover
if not app.config["CANAILLE"]["SENTRY_DSN"]:
2022-01-11 17:02:23 +00:00
return None
try:
2022-01-11 17:02:23 +00:00
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
except Exception:
return None
sentry_sdk.init(
dsn=app.config["CANAILLE"]["SENTRY_DSN"], integrations=[FlaskIntegration()]
)
2022-01-11 17:02:23 +00:00
return sentry_sdk
2021-12-08 17:06:50 +00:00
def setup_jinja(app):
2021-12-10 16:08:43 +00:00
app.jinja_env.filters["len"] = len
2024-10-28 21:17:47 +00:00
app.jinja_env.filters["password_strength"] = password_strength_calculator
app.jinja_env.policies["ext.i18n.trimmed"] = True
2021-12-08 17:06:50 +00:00
2021-12-08 15:10:01 +00:00
def setup_blueprints(app):
import canaille.core.endpoints
2022-01-05 15:30:46 +00:00
2021-12-08 15:10:01 +00:00
app.url_map.strict_slashes = False
app.register_blueprint(canaille.core.endpoints.bp)
if "CANAILLE_OIDC" in app.config:
import canaille.oidc.endpoints
app.register_blueprint(canaille.oidc.endpoints.bp)
2020-09-27 15:32:23 +00:00
2023-03-28 18:30:29 +00:00
def setup_flask(app):
csrf.init_app(app)
@app.before_request
def make_session_permanent():
session.permanent = True
app.permanent_session_lifetime = datetime.timedelta(days=365)
@app.context_processor
def global_processor():
from canaille.app.session import current_user
2023-03-28 18:30:29 +00:00
return {
"debug": app.debug or app.config.get("TESTING", False),
"logo_url": app.config["CANAILLE"]["LOGO"],
"favicon_url": app.config["CANAILLE"]["FAVICON"]
or app.config["CANAILLE"]["LOGO"],
"website_name": app.config["CANAILLE"]["NAME"],
2023-03-28 18:30:29 +00:00
"user": current_user(),
"menu": True,
"is_boosted": request.headers.get("HX-Boosted", False),
"features": app.features,
2023-03-28 18:30:29 +00:00
}
2023-06-28 15:56:49 +00:00
def setup_flask_converters(app):
from canaille.app import models
from canaille.app.flask import model_converter
2023-06-28 15:56:49 +00:00
2023-04-09 21:00:13 +00:00
for model_name, model_class in models.MODELS.items():
app.url_map.converters[model_name.lower()] = model_converter(model_class)
2023-06-28 15:56:49 +00:00
def create_app(
config=None, validate=True, backend=None, env_file=".env", env_prefix=""
):
from .app.configuration import setup_config
from .app.features import setup_features
from .app.i18n import setup_i18n
from .app.logging import setup_logging
from .app.themes import setup_themer
2023-04-09 21:00:13 +00:00
from .backends import setup_backend
2021-12-08 15:10:01 +00:00
app = Flask(__name__)
with app.app_context():
if not setup_config(
app=app,
config=config,
test_config=validate,
env_file=env_file,
env_prefix=env_prefix,
): # pragma: no cover
sys.exit(1)
2021-12-08 15:10:01 +00:00
2022-01-11 17:02:23 +00:00
sentry_sdk = setup_sentry(app)
2021-12-08 15:10:01 +00:00
try:
setup_logging(app)
2023-12-25 12:22:43 +00:00
backend = setup_backend(app, backend)
setup_features(app)
2023-06-28 15:56:49 +00:00
setup_flask_converters(app)
2021-12-08 15:10:01 +00:00
setup_blueprints(app)
2021-12-08 17:06:50 +00:00
setup_jinja(app)
setup_i18n(app)
2021-12-08 15:10:01 +00:00
setup_themer(app)
2023-03-28 18:30:29 +00:00
setup_flask(app)
2020-09-27 15:32:23 +00:00
if "CANAILLE_OIDC" in app.config:
from .oidc.oauth import setup_oauth
setup_oauth(app)
except Exception as exc: # pragma: no cover
2022-01-11 17:02:23 +00:00
if sentry_sdk:
2020-09-29 16:21:41 +00:00
sentry_sdk.capture_exception(exc)
raise
return app