Skip to content

Commit 91c5293

Browse files
authored
simplify and fix get_visible_document_or_404 (#6710)
1 parent db0a2c0 commit 91c5293

File tree

2 files changed

+144
-27
lines changed

2 files changed

+144
-27
lines changed

kitsune/wiki/tests/test_utils.py

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from kitsune.dashboards.models import WikiDocumentVisits
1212
from kitsune.products.tests import ProductFactory, TopicFactory
1313
from kitsune.sumo.tests import TestCase
14-
from kitsune.users.tests import UserFactory
14+
from kitsune.users.tests import GroupFactory, UserFactory
1515
from kitsune.wiki.tests import (
1616
ApprovedRevisionFactory,
1717
DocumentFactory,
@@ -852,3 +852,132 @@ def test_unapproved_revision(self):
852852
creator = unapproved_doc.revisions.first().creator
853853
doc = get_visible_document_or_404(creator, locale="en-US", slug="unapproved")
854854
self.assertEqual(doc, unapproved_doc)
855+
856+
def test_cross_locale_lookup_with_restricted_visibility(self):
857+
g1 = GroupFactory(name="group1")
858+
859+
g1_user = UserFactory(groups=[g1])
860+
861+
en_doc = ApprovedRevisionFactory(
862+
document__locale="en-US",
863+
document__slug="restricted-test-document",
864+
document__restrict_to_groups=[g1],
865+
document__allow_discussion=False,
866+
is_ready_for_localization=True,
867+
).document
868+
869+
de_doc = TranslatedRevisionFactory(
870+
document__locale="de",
871+
document__slug="restricted-test-document-de",
872+
document__allow_discussion=False,
873+
document__parent=en_doc,
874+
based_on=en_doc.current_revision,
875+
).document
876+
877+
fr_doc = TranslatedRevisionFactory(
878+
document__locale="fr",
879+
document__slug="restricted-test-document-fr",
880+
document__allow_discussion=False,
881+
document__parent=en_doc,
882+
based_on=en_doc.current_revision,
883+
).document
884+
885+
with self.assertRaises(Http404):
886+
get_visible_document_or_404(
887+
self.user,
888+
locale="en-US",
889+
slug="restricted-test-document-de",
890+
look_for_translation_via_parent=True,
891+
)
892+
893+
with self.assertRaises(Http404):
894+
get_visible_document_or_404(
895+
self.user,
896+
locale="en-US",
897+
slug="restricted-test-document-fr",
898+
look_for_translation_via_parent=True,
899+
)
900+
901+
with self.assertRaises(Http404):
902+
get_visible_document_or_404(
903+
self.user,
904+
locale="de",
905+
slug="restricted-test-document",
906+
look_for_translation_via_parent=True,
907+
)
908+
909+
with self.assertRaises(Http404):
910+
get_visible_document_or_404(
911+
self.user,
912+
locale="de",
913+
slug="restricted-test-document-fr",
914+
look_for_translation_via_parent=True,
915+
)
916+
917+
doc = get_visible_document_or_404(
918+
g1_user,
919+
locale="en-US",
920+
slug="restricted-test-document-de",
921+
look_for_translation_via_parent=True,
922+
)
923+
self.assertEqual(doc, en_doc)
924+
925+
doc = get_visible_document_or_404(
926+
g1_user,
927+
locale="en-US",
928+
slug="restricted-test-document-fr",
929+
look_for_translation_via_parent=True,
930+
)
931+
self.assertEqual(doc, en_doc)
932+
933+
doc = get_visible_document_or_404(
934+
g1_user,
935+
locale="de",
936+
slug="restricted-test-document",
937+
look_for_translation_via_parent=True,
938+
)
939+
self.assertEqual(doc, de_doc)
940+
941+
doc = get_visible_document_or_404(
942+
g1_user,
943+
locale="de",
944+
slug="restricted-test-document-fr",
945+
look_for_translation_via_parent=True,
946+
)
947+
self.assertEqual(doc, de_doc)
948+
949+
doc = get_visible_document_or_404(
950+
g1_user,
951+
locale="fr",
952+
slug="restricted-test-document-de",
953+
look_for_translation_via_parent=True,
954+
)
955+
self.assertEqual(doc, fr_doc)
956+
957+
# All of the kwargs should be respected.
958+
with self.assertRaises(Http404):
959+
get_visible_document_or_404(
960+
g1_user,
961+
locale="en-US",
962+
slug="restricted-test-document-de",
963+
allow_discussion=True,
964+
look_for_translation_via_parent=True,
965+
)
966+
967+
with self.assertRaises(Http404):
968+
get_visible_document_or_404(
969+
g1_user,
970+
locale="de",
971+
slug="restricted-test-document",
972+
allow_discussion=True,
973+
look_for_translation_via_parent=True,
974+
)
975+
976+
with self.assertRaises(Http404):
977+
get_visible_document_or_404(
978+
g1_user,
979+
locale="de",
980+
slug="restricted-test-document-fr",
981+
allow_discussion=True,
982+
look_for_translation_via_parent=True,
983+
)

kitsune/wiki/utils.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -183,44 +183,32 @@ def get_visible_document_or_404(
183183
except Document.DoesNotExist:
184184
pass
185185

186-
if not (locale := kwargs.get("locale")):
186+
if not (look_for_translation_via_parent and (locale := kwargs.get("locale"))):
187187
raise Http404
188188

189-
slug = kwargs.get("slug")
190-
191-
if not (slug and look_for_translation_via_parent):
192-
raise Http404
193-
194-
other_docs_qs = (
195-
Document.objects.filter(slug=slug).exclude(locale=locale).select_related("parent")
196-
)
189+
# Make the same query without the locale.
190+
other_docs_qs = Document.objects.visible(
191+
user, **{k: v for k, v in kwargs.items() if k != "locale"}
192+
).select_related("parent")
197193

198194
for doc in other_docs_qs:
199195
base_doc = doc.parent or doc
200-
if base_doc.locale == settings.WIKI_DEFAULT_LANGUAGE:
201-
if base_doc.locale == locale:
202-
return base_doc
203-
translation = base_doc.translated_to(locale, visible_for_user=user)
204-
if translation:
205-
return translation
206-
207-
# Don't try final fallback if not looking for translations or in default language
208-
if not look_for_translation_via_parent or locale == settings.WIKI_DEFAULT_LANGUAGE:
196+
if base_doc.locale == locale:
197+
return base_doc
198+
elif (base_doc.locale == settings.WIKI_DEFAULT_LANGUAGE) and (
199+
translation := base_doc.translated_to(locale, visible_for_user=user)
200+
):
201+
return translation
202+
203+
if not (return_parent_if_no_translation and (locale != settings.WIKI_DEFAULT_LANGUAGE)):
209204
raise Http404
210205

211206
kwargs["locale"] = settings.WIKI_DEFAULT_LANGUAGE
212207
try:
213-
parent = Document.objects.get_visible(user, **kwargs)
208+
return Document.objects.get_visible(user, **kwargs)
214209
except Document.DoesNotExist:
215210
raise Http404
216211

217-
translation = parent.translated_to(locale, visible_for_user=user)
218-
if translation:
219-
return translation
220-
if return_parent_if_no_translation:
221-
return parent
222-
raise Http404
223-
224212

225213
def get_visible_revision_or_404(user, **kwargs):
226214
return get_object_or_404(Revision.objects.visible(user, **kwargs))

0 commit comments

Comments
 (0)