2021-07-01 09:04:57 +00:00
|
|
|
import datetime
|
2021-12-20 22:57:27 +00:00
|
|
|
from logging.config import dictConfig
|
2020-08-17 13:49:48 +00:00
|
|
|
|
2021-12-20 22:57:27 +00:00
|
|
|
from flask import Flask
|
2023-06-23 14:26:38 +00:00
|
|
|
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
|
|
|
|
2023-03-28 18:30:29 +00:00
|
|
|
|
|
|
|
csrf = CSRFProtect()
|
2020-08-17 13:49:48 +00:00
|
|
|
|
|
|
|
|
2022-11-20 21:34:05 +00:00
|
|
|
def setup_sentry(app): # pragma: no cover
|
2022-01-11 17:02:23 +00:00
|
|
|
if not app.config.get("SENTRY_DSN"):
|
|
|
|
return None
|
|
|
|
|
2022-11-20 21:34:05 +00:00
|
|
|
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["SENTRY_DSN"], integrations=[FlaskIntegration()])
|
|
|
|
return sentry_sdk
|
|
|
|
|
|
|
|
|
2021-10-31 13:40:12 +00:00
|
|
|
def setup_logging(app):
|
|
|
|
log_level = app.config.get("LOGGING", {}).get("LEVEL", "WARNING")
|
|
|
|
if not app.config.get("LOGGING", {}).get("PATH"):
|
|
|
|
handler = {
|
|
|
|
"class": "logging.StreamHandler",
|
|
|
|
"stream": "ext://flask.logging.wsgi_errors_stream",
|
|
|
|
"formatter": "default",
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
handler = {
|
|
|
|
"class": "logging.handlers.WatchedFileHandler",
|
|
|
|
"filename": app.config["LOGGING"]["PATH"],
|
|
|
|
"formatter": "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
dictConfig(
|
|
|
|
{
|
|
|
|
"version": 1,
|
|
|
|
"formatters": {
|
|
|
|
"default": {
|
|
|
|
"format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"handlers": {"wsgi": handler},
|
|
|
|
"root": {"level": log_level, "handlers": ["wsgi"]},
|
2023-09-11 16:20:17 +00:00
|
|
|
"loggers": {
|
|
|
|
"faker": {"level": "WARNING"},
|
|
|
|
},
|
2022-12-21 20:52:01 +00:00
|
|
|
"disable_existing_loggers": False,
|
2021-10-31 13:40:12 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
2023-03-22 18:29:28 +00:00
|
|
|
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):
|
2023-12-25 23:23:47 +00:00
|
|
|
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
|
|
|
|
|
2023-12-25 23:23:47 +00:00
|
|
|
app.register_blueprint(canaille.core.endpoints.bp)
|
2023-09-15 15:24:05 +00:00
|
|
|
|
|
|
|
if "OIDC" in app.config:
|
2023-12-25 23:23:47 +00:00
|
|
|
import canaille.oidc.endpoints
|
2023-09-15 15:24:05 +00:00
|
|
|
|
2023-12-25 23:23:47 +00:00
|
|
|
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():
|
2023-04-09 13:52:55 +00:00
|
|
|
from canaille.app.flask import current_user
|
2023-03-28 18:30:29 +00:00
|
|
|
|
|
|
|
return {
|
2023-07-10 18:03:13 +00:00
|
|
|
"debug": app.debug or app.config.get("TESTING", False),
|
2023-03-28 18:30:29 +00:00
|
|
|
"has_smtp": "SMTP" in app.config,
|
2023-09-15 15:24:05 +00:00
|
|
|
"has_oidc": "OIDC" in app.config,
|
2023-05-15 16:06:22 +00:00
|
|
|
"has_password_recovery": app.config.get("ENABLE_PASSWORD_RECOVERY", True),
|
2023-08-15 14:17:19 +00:00
|
|
|
"has_registration": app.config.get("ENABLE_REGISTRATION", False),
|
2022-11-01 11:25:21 +00:00
|
|
|
"has_account_lockability": app.backend.get().has_account_lockability(),
|
2023-03-28 18:30:29 +00:00
|
|
|
"logo_url": app.config.get("LOGO"),
|
|
|
|
"favicon_url": app.config.get("FAVICON", app.config.get("LOGO")),
|
|
|
|
"website_name": app.config.get("NAME", "Canaille"),
|
|
|
|
"user": current_user(),
|
|
|
|
"menu": True,
|
2023-06-23 14:26:38 +00:00
|
|
|
"is_boosted": request.headers.get("HX-Boosted", False),
|
2023-07-20 16:43:28 +00:00
|
|
|
"has_email_confirmation": app.config.get("EMAIL_CONFIRMATION") is True
|
|
|
|
or (app.config.get("EMAIL_CONFIRMATION") is None and "SMTP" in app.config),
|
2023-03-28 18:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-28 15:56:49 +00:00
|
|
|
def setup_flask_converters(app):
|
|
|
|
from canaille.app.flask import model_converter
|
|
|
|
from canaille.app import models
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2023-05-20 15:17:46 +00:00
|
|
|
def create_app(config=None, validate=True, backend=None):
|
2023-06-15 16:29:12 +00:00
|
|
|
from .app.i18n import setup_i18n
|
|
|
|
from .app.configuration import setup_config
|
2023-08-16 15:14:11 +00:00
|
|
|
from .app.themes import setup_themer
|
2023-04-09 21:00:13 +00:00
|
|
|
from .backends import setup_backend
|
2023-06-15 16:29:12 +00:00
|
|
|
|
2021-12-08 15:10:01 +00:00
|
|
|
app = Flask(__name__)
|
2023-07-10 16:45:54 +00:00
|
|
|
with app.app_context():
|
|
|
|
setup_config(app, config, validate)
|
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)
|
|
|
|
if app.debug:
|
|
|
|
backend.install(app.config, True)
|
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)
|
2022-11-20 21:12:18 +00:00
|
|
|
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
|
|
|
|
2023-09-15 15:24:05 +00:00
|
|
|
if "OIDC" in app.config:
|
|
|
|
from .oidc.oauth import setup_oauth
|
|
|
|
|
|
|
|
setup_oauth(app)
|
|
|
|
|
2022-11-20 21:34:05 +00:00
|
|
|
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
|
2021-12-06 20:49:38 +00:00
|
|
|
|
|
|
|
return app
|