From fd06d8428a3c8ffbbdb9a6ebc8c10c460b0c0d50 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 1 Aug 2022 17:50:51 +0200 Subject: [PATCH] =?UTF-8?q?Export=20datetime=20en=20JSON:=20conversionn=20?= =?UTF-8?q?syst=C3=A9matique=20en=20ISO=208601?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/__init__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/__init__.py b/app/__init__.py index 54fd0a0e..aafade12 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -16,6 +16,7 @@ from flask import current_app, g, request from flask import Flask from flask import abort, flash, has_request_context, jsonify from flask import render_template +from flask.json import JSONEncoder from flask.logging import default_handler from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate @@ -108,6 +109,15 @@ def handle_invalid_usage(error): return response +# JSON ENCODING +class ScoDocJSONEncoder(JSONEncoder): + def default(self, o): + if isinstance(o, datetime.datetime): + return o.isoformat() + + return super().default(o) + + def render_raw_html(template_filename: str, **args) -> str: """Load and render an HTML file _without_ using Flask Necessary for 503 error mesage, when DB is down and Flask may be broken. @@ -205,6 +215,7 @@ class ReverseProxied(object): def create_app(config_class=DevConfig): app = Flask(__name__, static_url_path="/ScoDoc/static", static_folder="static") app.wsgi_app = ReverseProxied(app.wsgi_app) + app.json_encoder = ScoDocJSONEncoder app.logger.setLevel(logging.INFO) # Evite de logguer toutes les requetes dans notre log