inveniosoftware / invenio-theme

Invenio standard theme.
https://invenio-theme.readthedocs.io
MIT License
7 stars 53 forks source link

migrate to flask babel #344

Closed utnapischtim closed 1 year ago

utnapischtim commented 1 year ago
utnapischtim commented 1 year ago

@kpsherva The problem here is the same as i had in invenio-i18n. The only solution i found was to reset babel_local within the context (like)

Problem description: The get_locale function of flask_babel does not only gets the locale but also puts the value in a cache stored within the actual context given by _get_current_context (this function stores the context within the flask g variable).

This caching mechanism should be fine if there is a normal client->server request, but within the tests there are several examples where within one test function multiple test_request_context functions are called. The problem is that the invenio_i18n.get_locale method works as expected because it gets the value from e.g. request.args like here and it works in following tests but it does not work if also gettext is called like here there it was necessary to reset the cached babel_locale value here

Further the with app.test_request_context(): exit method of the used ContextManager does not destroy the actual current context.

Discussion point: What functionality of invenio-theme are the following tests really testing?

Solutions: 1.) remove tests that are not passing and are not testing functionality from invenio-theme. 2.) add the g._flask_babel.babel_locale = None between the with app.test_request_context() contexts 3.) fix the bug of not cleaning the context stack within g after closing the app.test_request_context() (i think this could be difficult)

Thoughts: This problem should be only problem within the tests not on a real environment. Every new request should create the whole a new _flask_babel SimpleNamespace and the caching works fine.