@@ -174,64 +174,49 @@ def get_featured_articles(product=None, topics=None, locale=settings.WIKI_DEFAUL
174
174
def get_visible_document_or_404 (
175
175
user , look_for_translation_via_parent = False , return_parent_if_no_translation = False , ** kwargs
176
176
):
177
- """
178
- Get the document specified by the keyword arguments and visible to the given user, or 404.
179
- """
180
177
try :
181
178
return Document .objects .get_visible (user , ** kwargs )
182
179
except Document .DoesNotExist :
183
180
pass
184
181
185
- if not (locale := kwargs .get ("locale" )):
182
+ locale = kwargs .get ("locale" )
183
+ if not locale :
186
184
raise Http404
187
185
188
186
slug = kwargs .get ("slug" )
189
187
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
195
190
191
+ other_docs_qs = (
192
+ Document .objects .filter (slug = slug ).exclude (locale = locale ).select_related ("parent" )
193
+ )
194
+ for doc in other_docs_qs :
196
195
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
209
202
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
220
208
209
+ # Don't try final fallback if not looking for translations or in default language
221
210
if not look_for_translation_via_parent or locale == settings .WIKI_DEFAULT_LANGUAGE :
222
211
raise Http404
223
212
224
- # Try to find a translation via the parent
225
- kwargs .update (locale = settings .WIKI_DEFAULT_LANGUAGE )
213
+ kwargs ["locale" ] = settings .WIKI_DEFAULT_LANGUAGE
226
214
parent = get_object_or_404 (Document .objects .visible (user , ** kwargs ))
227
-
228
215
translation = parent .translated_to (locale , visible_for_user = user )
229
216
if translation :
230
217
return translation
231
-
232
218
if return_parent_if_no_translation :
233
219
return parent
234
-
235
220
raise Http404
236
221
237
222
0 commit comments