Skip to content

Commit db0a2c0

Browse files
authored
Locale switcher from parent (#6708)
not in the default locale This covers a case where the user is on a document that is not in the default locale (in our case en-US) and is not a translation. If the user tries to use the locale switcher to go to English, since there is no parent as we are not on a translation, the user should get a 404.
1 parent 32ddb95 commit db0a2c0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

kitsune/wiki/tests/test_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,24 @@ def test_nonexistent_document(self):
821821
self.user, locale="de", slug="doesnt-exist", look_for_translation_via_parent=True
822822
)
823823

824+
def test_parent_document_in_non_default_locale_no_english_version(self):
825+
"""Test that accessing English version of a non-default locale parent
826+
document returns 404."""
827+
828+
ApprovedRevisionFactory(
829+
document__locale="de",
830+
document__slug="german-only-parent",
831+
document__parent=None,
832+
)
833+
834+
with self.assertRaises(Http404):
835+
get_visible_document_or_404(
836+
self.user,
837+
locale="en-US",
838+
slug="german-only-parent",
839+
look_for_translation_via_parent=True,
840+
)
841+
824842
def test_unapproved_revision(self):
825843
"""Test that users can't see documents without approved revisions."""
826844
unapproved_doc = RevisionFactory(

kitsune/wiki/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,17 @@ def get_visible_document_or_404(
203203
translation = base_doc.translated_to(locale, visible_for_user=user)
204204
if translation:
205205
return translation
206-
else:
207-
translation = base_doc.translated_to(locale, visible_for_user=user)
208-
if translation:
209-
return translation
210206

211207
# Don't try final fallback if not looking for translations or in default language
212208
if not look_for_translation_via_parent or locale == settings.WIKI_DEFAULT_LANGUAGE:
213209
raise Http404
214210

215211
kwargs["locale"] = settings.WIKI_DEFAULT_LANGUAGE
216-
parent = get_object_or_404(Document.objects.visible(user, **kwargs))
212+
try:
213+
parent = Document.objects.get_visible(user, **kwargs)
214+
except Document.DoesNotExist:
215+
raise Http404
216+
217217
translation = parent.translated_to(locale, visible_for_user=user)
218218
if translation:
219219
return translation

0 commit comments

Comments
 (0)