Skip to content
Snippets Groups Projects
Verified Commit 2923449a authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Monkey-patch Django's JSON encoder to unwrap promises

parent f046e2ff
No related branches found
No related tags found
1 merge request!601Resolve "favicon / icon generation mauls database"
......@@ -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"
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment