diff --git a/tests/misc/test_commands.py b/tests/misc/test_commands.py index c6c57cfa11..33b97265cc 100644 --- a/tests/misc/test_commands.py +++ b/tests/misc/test_commands.py @@ -34,7 +34,7 @@ def test_clean_auth_tokens_revoked(self): }, "revoked": False, } - ).encode("utf-8"), + ), ) self.store.add( "testkey2", @@ -45,7 +45,7 @@ def test_clean_auth_tokens_revoked(self): }, "revoked": True, } - ).encode("utf-8"), + ), ) self.assertEqual(len(self.store.keys()), 2) commands.clean_auth_tokens() diff --git a/zou/app/blueprints/auth/resources.py b/zou/app/blueprints/auth/resources.py index 00799a0de5..b7587eac4f 100644 --- a/zou/app/blueprints/auth/resources.py +++ b/zou/app/blueprints/auth/resources.py @@ -83,9 +83,7 @@ def wrong_auth_handler(identity_user=None): @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): - if identity.id is not None: - from zou.app.services import persons_service - + if isinstance(identity.id, str): try: identity.user = persons_service.get_person(identity.id) diff --git a/zou/app/config.py b/zou/app/config.py index d818dea4ef..3f47cb152c 100644 --- a/zou/app/config.py +++ b/zou/app/config.py @@ -6,7 +6,6 @@ from zou.app.utils.env import envtobool, env_with_semicolon_to_list PROPAGATE_EXCEPTIONS = True -RESTFUL_JSON = {"ensure_ascii": False} DEBUG = envtobool("DEBUG", False) DEBUG_PORT = int(os.getenv("DEBUG_PORT", 5000)) diff --git a/zou/app/utils/api.py b/zou/app/utils/api.py index e7bc9d0e3e..f4369f1196 100644 --- a/zou/app/utils/api.py +++ b/zou/app/utils/api.py @@ -1,4 +1,5 @@ -from flask_restful import Api, output_json +from flask_restful import Api +from zou.app.utils.flask import output_json def configure_api_from_blueprint(blueprint, route_tuples): @@ -13,7 +14,6 @@ def configure_api_from_blueprint(blueprint, route_tuples): api = Api(blueprint, catch_all_404s=True) api.representations = { - "application/json; charset=utf-8": output_json, "application/json": output_json, } diff --git a/zou/app/utils/flask.py b/zou/app/utils/flask.py index 88f75a5313..9088b55b23 100644 --- a/zou/app/utils/flask.py +++ b/zou/app/utils/flask.py @@ -2,8 +2,28 @@ from werkzeug.user_agent import UserAgent from werkzeug.utils import cached_property from flask.json.provider import JSONProvider +from flask import current_app, make_response import orjson +orjson_options = orjson.OPT_NON_STR_KEYS + + +def output_json(data, code, headers=None): + """Makes a Flask response with a JSON encoded body""" + + # If we're in debug mode, and the indent is not set, we set it to a + # reasonable value here. Note that this won't override any existing value + # that was set. + option = orjson_options + if current_app.debug: + option |= orjson.OPT_INDENT_2 + + dumped = orjson.dumps(data, option=option) + + resp = make_response(dumped, code) + resp.headers.extend(headers or {}) + return resp + class ORJSONProvider(JSONProvider): def __init__(self, *args, **kwargs): @@ -14,8 +34,7 @@ def loads(self, s, **kwargs): return orjson.loads(s) def dumps(self, obj, **kwargs): - # decode back to str, as orjson returns bytes - return orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) + return orjson.dumps(obj, option=orjson_options) class ParsedUserAgent(UserAgent):