typing: Access LANGUAGE_CODE via `django.utils.translation.get_language`.

We no longer need to access the internal `LANGUAGE_CODE` attribute by
using `django.utils.translation.get_language`.

A test case overriding the translation is added to ensure the password
reset form sending to users requested from a wrong domain is properly
translated.

This is a part of django-stubs refactorings.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-05-30 00:18:57 -04:00 committed by Tim Abbott
parent 417e1b5e81
commit 27be27560b
3 changed files with 26 additions and 3 deletions

View File

@ -13,6 +13,7 @@ from django.core.validators import validate_email
from django.http import HttpRequest
from django.urls import reverse
from django.utils.http import urlsafe_base64_encode
from django.utils.translation import get_language
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy
from markupsafe import Markup as mark_safe
@ -382,7 +383,8 @@ class ZulipPasswordResetForm(PasswordResetForm):
)
if active_accounts_in_other_realms:
context["active_accounts_in_other_realms"] = active_accounts_in_other_realms
language = request.LANGUAGE_CODE
language = get_language()
send_email(
"zerver/emails/password_reset",
to_emails=[email],

View File

@ -14,7 +14,9 @@ from django.core.exceptions import ValidationError
from django.http import HttpRequest, HttpResponse
from django.test import Client, override_settings
from django.urls import reverse
from django.utils import translation
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from confirmation import settings as confirmation_settings
from confirmation.models import (
@ -378,9 +380,10 @@ class PasswordResetTest(ZulipTestCase):
[message] = outbox
self.assertEqual(self.email_envelope_from(message), settings.NOREPLY_EMAIL_ADDRESS)
# The email might be sent in different languages for i18n testing
self.assertRegex(
self.email_display_from(message),
rf"^Zulip Account Security <{self.TOKENIZED_NOREPLY_REGEX}>\Z",
rf'^{_("Zulip Account Security")} <{self.TOKENIZED_NOREPLY_REGEX}>\Z',
)
self.assertIn(f"{subdomain}.testserver", message.extra_headers["List-Id"])
@ -659,6 +662,23 @@ class PasswordResetTest(ZulipTestCase):
self.assertNotIn("reset your password", body)
self.assertNotIn("deactivated", body)
def test_wrong_subdomain_i18n(self) -> None:
user_profile = self.example_user("hamlet")
email = user_profile.delivery_email
# Send a password reset request with a different language to a wrong subdomain
result = self.client_post(
"/accounts/password/reset/",
{"email": email},
HTTP_ACCEPT_LANGUAGE="de",
subdomain="lear",
)
self.assertEqual(result.status_code, 302)
with translation.override("de"):
body = self.get_reset_mail_body("lear")
self.assertIn("hat ein neues Passwort", body)
def test_invalid_subdomain(self) -> None:
email = self.example_email("hamlet")

View File

@ -12,6 +12,7 @@ from django.db.models import Q
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils.translation import get_language
from django.utils.translation import gettext as _
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
@ -581,7 +582,7 @@ def send_confirm_registration_email(
"zerver/emails/confirm_registration",
to_emails=[email],
from_address=FromAddress.tokenized_no_reply_address(),
language=request.LANGUAGE_CODE if request is not None else None,
language=get_language() if request is not None else None,
context={
"create_realm": (realm is None),
"activate_url": activation_url,