diff --git a/setup.py b/setup.py index 4aafc687a..32e083c55 100644 --- a/setup.py +++ b/setup.py @@ -42,7 +42,7 @@ core_deps = [ "filetype>=1.0.7,<2", "Flask-Assets>=2.0,<3", "Flask-Babel>=2.0,<3", - "Flask-Login>=0.6,<0.7", # breaking changes can happen on minor version increases + "Flask-Login>=0.6.2,<0.7", # breaking changes can happen on minor version increases "Flask-Limiter>=2.6,<3", "flask>=2.2,<2.3", # breaking changes can happen on minor version increases (with deprecation warnings) "frozendict>=2.0,<3", diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 53b3d6b98..21407398f 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -1344,7 +1344,7 @@ class Server: from octoprint.server.util.flask import ( OctoPrintFlaskRequest, OctoPrintFlaskResponse, - OctoPrintJsonEncoder, + OctoPrintJsonProvider, OctoPrintSessionInterface, PrefixAwareJinjaEnvironment, ReverseProxiedEnvironment, @@ -1361,7 +1361,7 @@ class Server: app.debug = self._debug # setup octoprint's flask json serialization/deserialization - app.json_encoder = OctoPrintJsonEncoder + app.json = OctoPrintJsonProvider(app) s = settings() @@ -1957,6 +1957,7 @@ class Server: from octoprint.server.util.webassets import MemoryManifest # noqa: F401 + util.flask.fix_webassets_convert_item_to_flask_url() util.flask.fix_webassets_filtertool() base_folder = self._settings.getBaseFolder("generated") diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index b63d7295b..9e61920d8 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -14,6 +14,7 @@ from typing import Union import flask import flask.json +import flask.json.provider import flask.sessions import flask.templating import flask_assets @@ -45,7 +46,6 @@ def enable_additional_translations(default_locale="en", additional_folders=None) import flask_babel from babel import Locale, support - from flask import _request_ctx_stack if additional_folders is None: additional_folders = [] @@ -92,10 +92,9 @@ def enable_additional_translations(default_locale="en", additional_folders=None) object if used outside of the request or if a translation cannot be found. """ - ctx = _request_ctx_stack.top - if ctx is None: + if flask.g is None: return None - translations = getattr(ctx, "babel_translations", None) + translations = getattr(flask.g, "babel_translations", None) if translations is None: locale = flask_babel.get_locale() translations = support.Translations() @@ -138,7 +137,7 @@ def enable_additional_translations(default_locale="en", additional_folders=None) # core translations dirs = additional_folders + [ - os.path.join(ctx.app.root_path, "translations") + os.path.join(flask.current_app.root_path, "translations") ] for dirname in dirs: core_translations = support.Translations.load(dirname, [locale]) @@ -152,7 +151,7 @@ def enable_additional_translations(default_locale="en", additional_folders=None) logger.debug(f"No translations for locale {locale} in core folders") translations = translations.merge(core_translations) - ctx.babel_translations = translations + flask.g.babel_translations = translations return translations flask_babel.Babel.list_translations = fixed_list_translations @@ -197,6 +196,42 @@ def fix_webassets_filtertool(): FilterTool._wrap_cache = fixed_wrap_cache +def fix_webassets_convert_item_to_flask_url(): + import flask_assets + + def fixed_convert_item_to_flask_url(self, ctx, item, filepath=None): + from flask import url_for + + directory, rel_path, endpoint = self.split_prefix(ctx, item) + + if filepath is not None: + filename = filepath[len(directory) + 1 :] + else: + filename = rel_path + + flask_ctx = None + if not flask.has_request_context(): # fixed, was _request_ctx.top + flask_ctx = ctx.environment._app.test_request_context() + flask_ctx.push() + try: + url = url_for(endpoint, filename=filename) + # In some cases, url will be an absolute url with a scheme and hostname. + # (for example, when using werkzeug's host matching). + # In general, url_for() will return a http url. During assets build, we + # we don't know yet if the assets will be served over http, https or both. + # Let's use // instead. url_for takes a _scheme argument, but only together + # with external=True, which we do not want to force every time. Further, + # this _scheme argument is not able to render // - it always forces a colon. + if url and url.startswith("http:"): + url = url[5:] + return url + finally: + if flask_ctx: + flask_ctx.pop() + + flask_assets.FlaskResolver.convert_item_to_flask_url = fixed_convert_item_to_flask_url + + def fix_flask_jsonify(): def fixed_jsonify(*args, **kwargs): """Backported from https://github.com/pallets/flask/blob/7e714bd28b6e96d82b2848b48cf8ff48b517b09b/flask/json/__init__.py#L257""" @@ -1808,9 +1843,10 @@ def collect_plugin_assets(preferred_stylesheet="css"): ##~~ JSON encoding -class OctoPrintJsonEncoder(flask.json.JSONEncoder): - def default(self, obj): +class OctoPrintJsonProvider(flask.json.provider.DefaultJSONProvider): + @staticmethod + def default(object_): try: - return JsonEncoding.encode(obj) + return JsonEncoding.encode(object_) except TypeError: - return flask.json.JSONEncoder.default(self, obj) + return flask.json.provider.DefaultJSONProvider.default(object_) diff --git a/src/octoprint/server/views.py b/src/octoprint/server/views.py index 936836ce7..63a8cf8e7 100644 --- a/src/octoprint/server/views.py +++ b/src/octoprint/server/views.py @@ -1678,7 +1678,7 @@ def _get_all_assets(): def _get_all_translationfiles(locale, domain): - from flask import _request_ctx_stack + from flask import current_app def get_po_path(basedir, locale, domain): return os.path.join(basedir, locale, "LC_MESSAGES", f"{domain}.po") @@ -1701,8 +1701,7 @@ def _get_all_translationfiles(locale, domain): po_files.append(get_po_path(dirname, locale, domain)) # core translations - ctx = _request_ctx_stack.top - base_path = os.path.join(ctx.app.root_path, "translations") + base_path = os.path.join(current_app.root_path, "translations") dirs = [user_base_path, base_path] for dirname in dirs: