Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

models: Force the translated role into a translated string.

Alex Vandiver012113c54264
diff --git a/zerver/logging_handlers.py b/zerver/logging_handlers.py
index 24316b8f735..6086ef9f86a 100644
--- a/zerver/logging_handlers.py
+++ b/zerver/logging_handlers.py
@@ -9,6 +9,7 @@ from urllib.parse import SplitResult
from django.conf import settings
from django.http import HttpRequest
+from django.utils.translation import override as override_language
from django.views.debug import get_exception_reporter_filter
from sentry_sdk import capture_exception
from typing_extensions import Protocol, runtime_checkable
@@ -46,7 +47,10 @@ def add_request_metadata(report: Dict[str, Any], request: HttpRequest) -> None:
else:
user_full_name = user_profile.full_name
user_email = user_profile.email
- user_role = user_profile.get_role_name()
+ with override_language(settings.LANGUAGE_CODE):
+ # str() to force the lazy-translation to apply now,
+ # since it won't serialize into the worker queue.
+ user_role = str(user_profile.get_role_name())
except Exception:
# Unexpected exceptions here should be handled gracefully
traceback.print_exc()
diff --git a/zproject/sentry.py b/zproject/sentry.py
index 8d615c2ce42..9d29270d1fa 100644
--- a/zproject/sentry.py
+++ b/zproject/sentry.py
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING, Optional
import sentry_sdk
+from django.utils.translation import override as override_language
from sentry_sdk.integrations import Integration
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.logging import ignore_logger
@@ -21,13 +22,18 @@ def add_context(event: 'Event', hint: 'Hint') -> Optional['Event']:
# Ignore GeneratorExit, KeyboardInterrupt, and SystemExit exceptions
if not isinstance(exc_value, Exception):
return None
+ from django.conf import settings
+
from zerver.models import get_user_profile_by_id
with capture_internal_exceptions():
user_info = event.get("user", {})
if user_info.get("id"):
user_profile = get_user_profile_by_id(user_info["id"])
user_info["realm"] = user_profile.realm.string_id or 'root'
- user_info["role"] = user_profile.get_role_name()
+ with override_language(settings.LANGUAGE_CODE):
+ # str() to force the lazy-translation to apply now,
+ # since it won't serialize into json for Sentry otherwise
+ user_info["role"] = str(user_profile.get_role_name())
return event
def setup_sentry(dsn: Optional[str], *integrations: Integration) -> None: