Skip to content

Commit ca2836a

Browse files
committed
refactor loop and cut down on comments
1 parent c46afbd commit ca2836a

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

kitsune/wiki/utils.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -174,64 +174,49 @@ def get_featured_articles(product=None, topics=None, locale=settings.WIKI_DEFAUL
174174
def get_visible_document_or_404(
175175
user, look_for_translation_via_parent=False, return_parent_if_no_translation=False, **kwargs
176176
):
177-
"""
178-
Get the document specified by the keyword arguments and visible to the given user, or 404.
179-
"""
180177
try:
181178
return Document.objects.get_visible(user, **kwargs)
182179
except Document.DoesNotExist:
183180
pass
184181

185-
if not (locale := kwargs.get("locale")):
182+
locale = kwargs.get("locale")
183+
if not locale:
186184
raise Http404
187185

188186
slug = kwargs.get("slug")
189187

190-
if slug and look_for_translation_via_parent:
191-
# Find documents with this slug in other locales
192-
other_docs_qs = (
193-
Document.objects.filter(slug=slug).exclude(locale=locale).select_related("parent")
194-
)
188+
if not (slug and look_for_translation_via_parent):
189+
raise Http404
195190

191+
other_docs_qs = (
192+
Document.objects.filter(slug=slug).exclude(locale=locale).select_related("parent")
193+
)
194+
for doc in other_docs_qs:
196195
if locale == settings.WIKI_DEFAULT_LANGUAGE:
197-
# We're in default locale, look for translations in other locales
198-
for doc in other_docs_qs:
199-
parent_doc = doc.parent or doc
200-
201-
# If the parent is in the default locale, return it directly
202-
if parent_doc.locale == settings.WIKI_DEFAULT_LANGUAGE:
203-
return parent_doc
204-
205-
# Otherwise check if it has a translation in the requested locale
206-
translation = parent_doc.translated_to(locale, visible_for_user=user)
207-
if translation:
208-
return translation
196+
parent_doc = doc.parent or doc
197+
if parent_doc.locale == settings.WIKI_DEFAULT_LANGUAGE:
198+
return parent_doc
199+
translation = parent_doc.translated_to(locale, visible_for_user=user)
200+
if translation:
201+
return translation
209202
else:
210-
# Looking for a non-default locale document
211-
for doc in other_docs_qs:
212-
# Only consider documents that are translations
213-
if not doc.parent:
214-
continue
215-
216-
# Check if this parent has a translation in our requested locale
217-
translation = doc.parent.translated_to(locale, visible_for_user=user)
218-
if translation:
219-
return translation
203+
if not doc.parent:
204+
continue
205+
translation = doc.parent.translated_to(locale, visible_for_user=user)
206+
if translation:
207+
return translation
220208

209+
# Don't try final fallback if not looking for translations or in default language
221210
if not look_for_translation_via_parent or locale == settings.WIKI_DEFAULT_LANGUAGE:
222211
raise Http404
223212

224-
# Try to find a translation via the parent
225-
kwargs.update(locale=settings.WIKI_DEFAULT_LANGUAGE)
213+
kwargs["locale"] = settings.WIKI_DEFAULT_LANGUAGE
226214
parent = get_object_or_404(Document.objects.visible(user, **kwargs))
227-
228215
translation = parent.translated_to(locale, visible_for_user=user)
229216
if translation:
230217
return translation
231-
232218
if return_parent_if_no_translation:
233219
return parent
234-
235220
raise Http404
236221

237222

0 commit comments

Comments
 (0)