From 2923449a488b358ffd19a74cd2f566b922c67982 Mon Sep 17 00:00:00 2001 From: Dominik George <dominik.george@teckids.org> Date: Sun, 16 May 2021 11:14:57 +0200 Subject: [PATCH] Monkey-patch Django's JSON encoder to unwrap promises --- aleksis/core/settings.py | 3 +++ aleksis/core/util/core_helpers.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/aleksis/core/settings.py b/aleksis/core/settings.py index 5d840d0c5..06e7ee839 100644 --- a/aleksis/core/settings.py +++ b/aleksis/core/settings.py @@ -10,8 +10,11 @@ from .util.core_helpers import ( lazy_get_favicons, lazy_preference, merge_app_settings, + monkey_patch, ) +monkey_patch() + IN_PYTEST = "PYTEST_CURRENT_TEST" in os.environ or "TOX_ENV_DIR" in os.environ ENVVAR_PREFIX_FOR_DYNACONF = "ALEKSIS" diff --git a/aleksis/core/util/core_helpers.py b/aleksis/core/util/core_helpers.py index 9ad3b70c3..9c5f93a1f 100644 --- a/aleksis/core/util/core_helpers.py +++ b/aleksis/core/util/core_helpers.py @@ -261,3 +261,16 @@ def queryset_rules_filter( def unread_notifications_badge(request: HttpRequest) -> int: """Generate badge content with the number of unread notifications.""" return request.user.person.unread_notifications_count + + +def monkey_patch() -> None: # noqa + """Monkey-patch dependencies for special behaviour.""" + # Unwrap promises in JSON serializer instead of stringifying + import django.core.serializers.json + from django.utils.functional import Promise + class DjangoJSONEncoder(json.DjangoJSONEncoder): + def default(self, o: Any) -> Any: + if isinstance(o, Promise) and hasattr(o, copy): + return super().default(o.copy()) + return super().default(o) + json.DjangoJSONEncoder = DjangoJSONEncoder -- GitLab