Export datetime en JSON: conversionn systématique en ISO 8601

This commit is contained in:
Emmanuel Viennet 2022-08-01 17:50:51 +02:00
parent bd718d456d
commit fd06d8428a

View File

@ -16,6 +16,7 @@ from flask import current_app, g, request
from flask import Flask from flask import Flask
from flask import abort, flash, has_request_context, jsonify from flask import abort, flash, has_request_context, jsonify
from flask import render_template from flask import render_template
from flask.json import JSONEncoder
from flask.logging import default_handler from flask.logging import default_handler
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate from flask_migrate import Migrate
@ -108,6 +109,15 @@ def handle_invalid_usage(error):
return response 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: def render_raw_html(template_filename: str, **args) -> str:
"""Load and render an HTML file _without_ using Flask """Load and render an HTML file _without_ using Flask
Necessary for 503 error mesage, when DB is down and Flask may be broken. 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): def create_app(config_class=DevConfig):
app = Flask(__name__, static_url_path="/ScoDoc/static", static_folder="static") app = Flask(__name__, static_url_path="/ScoDoc/static", static_folder="static")
app.wsgi_app = ReverseProxied(app.wsgi_app) app.wsgi_app = ReverseProxied(app.wsgi_app)
app.json_encoder = ScoDocJSONEncoder
app.logger.setLevel(logging.INFO) app.logger.setLevel(logging.INFO)
# Evite de logguer toutes les requetes dans notre log # Evite de logguer toutes les requetes dans notre log