diff --git a/web/flaskr/__init__.py b/web/flaskr/__init__.py index 6ff8749f..6ef16f1a 100755 --- a/web/flaskr/__init__.py +++ b/web/flaskr/__init__.py @@ -25,19 +25,7 @@ LANGUAGES = ["en", "fr"] -def setup_babel(app): - babel = Babel(app) - - @babel.localeselector - def get_locale(): - if request.args.get("lang"): - session["lang"] = request.args["lang"] - return session.get("lang", "fr") - - -def create_app(test_config=None, gunicorn_logging=False): - # create and configure the app - app = Flask(__name__, instance_relative_config=True) +def setup_cache(app): cache.init_app( app, config={ @@ -45,6 +33,9 @@ def create_app(test_config=None, gunicorn_logging=False): "CACHE_DIR": "/tmp/flask-caching", }, ) + + +def setup_logging(app, test_config=None, gunicorn_logging=False): if gunicorn_logging: gunicorn_logger = logging.getLogger("gunicorn.error") app.logger.handlers = gunicorn_logger.handlers @@ -53,6 +44,34 @@ def create_app(test_config=None, gunicorn_logging=False): if test_config: app.config.from_mapping(test_config) + +def setup_i18n(app): + babel = Babel(app) + + @babel.localeselector + def get_locale(): + if request.args.get("lang"): + session["lang"] = request.args["lang"] + return session.get("lang", "fr") + + +def setup_csrf(app): + csrf = CSRFProtect() + csrf.init_app(app) + + +def setup_database(app): + with app.app_context(): + import flaskr.routes + + app.register_blueprint(flaskr.routes.bp) + from .models import db + + db.init_app(app) + Migrate(app, db, compare_type=True) + + +def setup_jinja(app): @app.context_processor def global_processor(): return { @@ -64,22 +83,37 @@ def global_processor(): **app.config["WORDINGS"], } - # translations - setup_babel(app) - # Protect App Form with CSRF - csrf = CSRFProtect() - csrf.init_app(app) +def setup_error_pages(app): + from flask import render_template - # init database - with app.app_context(): - import flaskr.routes + @app.errorhandler(400) + def bad_request(error): + return render_template("errors/400.html", error=error), 400 - app.register_blueprint(flaskr.routes.bp) - from .models import db + @app.errorhandler(403) + def not_authorized(error): + return render_template("errors/403.html", error=error), 403 - db.init_app(app) - Migrate(app, db, compare_type=True) + @app.errorhandler(404) + def not_found(error): + return render_template("errors/404.html", error=error), 404 + + @app.errorhandler(500) + def internal_error(error): + return render_template("errors/500.html", error=error), 500 + + +def create_app(test_config=None, gunicorn_logging=False): + # create and configure the app + app = Flask(__name__, instance_relative_config=True) + setup_cache(app) + setup_logging(app, test_config, gunicorn_logging) + setup_i18n(app) + setup_csrf(app) + setup_database(app) + setup_jinja(app) + setup_error_pages(app) # ensure the instance folder exists os.makedirs(app.instance_path, exist_ok=True) diff --git a/web/flaskr/routes.py b/web/flaskr/routes.py index 50357b09..560e68ea 100755 --- a/web/flaskr/routes.py +++ b/web/flaskr/routes.py @@ -1544,33 +1544,3 @@ def delete_video_meeting(): @auth.oidc_logout def logout(): return redirect(url_for("routes.index")) - - -@current_app.errorhandler(403) -def page_not_authorized(e): - return ( - render_template( - "errors/403.html", - ), - 403, - ) - - -@current_app.errorhandler(404) -def page_not_found(e): - return ( - render_template( - "errors/404.html", - ), - 404, - ) - - -@current_app.errorhandler(500) -def page_error(e): - return ( - render_template( - "errors/500.html", - ), - 500, - ) diff --git a/web/flaskr/templates/errors/400.html b/web/flaskr/templates/errors/400.html new file mode 100644 index 00000000..d42677d6 --- /dev/null +++ b/web/flaskr/templates/errors/400.html @@ -0,0 +1,18 @@ +{% extends 'layout.html' %} + +{% block main %} +
+ {% if error %} + {{ error }} + {% else %} + {% trans %}La requête que vous avez effectué est invalide. Vous pouvez retourner à l’accueil.{% endtrans %} + {% endif %} +
+