From 46dada71a68acf6612a97bf80e386456d77aaf9b Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 1 Aug 2023 22:07:23 +0200 Subject: [PATCH] API: exporte les date et datetime en ISO et non pas en RFC 822. --- app/__init__.py | 10 ++++++++-- tests/api/test_api_etudiants.py | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 86646eb4..f1fba060 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -148,7 +148,7 @@ def handle_invalid_usage(error): # JSON ENCODING # used by some internal finctions -# the API is now using flask_son, NOT THIS ENCODER +# the API is now using flask_json, NOT THIS ENCODER class ScoDocJSONEncoder(json.JSONEncoder): def default(self, o): # pylint: disable=E0202 if isinstance(o, (datetime.date, datetime.datetime)): @@ -260,7 +260,13 @@ def create_app(config_class=DevConfig): CAS(app, url_prefix="/cas", configuration_function=cas.set_cas_configuration) app.wsgi_app = ReverseProxied(app.wsgi_app) - FlaskJSON(app) + app_json = FlaskJSON(app) + + @app_json.encoder + def scodoc_json_encoder(o): + "Overide default date encoding (RFC 822) and use ISO" + if isinstance(o, (datetime.date, datetime.datetime)): + return o.isoformat() # Pour conserver l'ordre des objets dans les JSON: # e.g. l'ordre des UE dans les bulletins diff --git a/tests/api/test_api_etudiants.py b/tests/api/test_api_etudiants.py index 56f3193d..c7d948ef 100644 --- a/tests/api/test_api_etudiants.py +++ b/tests/api/test_api_etudiants.py @@ -17,6 +17,7 @@ Utilisation : pytest tests/api/test_api_etudiants.py """ +import re import requests from app.scodoc import sco_utils as scu @@ -100,6 +101,7 @@ def test_etudiants_courant(api_headers): etud = etudiants[-1] assert verify_fields(etud, ETUD_FIELDS) is True + assert re.match(r"^\d{4}-\d\d-\d\d$", etud["date_naissance"]) def test_etudiant(api_headers):