Repository: mdn/kuma

Issue:
Redirect to parent on deleted documents that can't be found
**Summary**
Take https://developer.mozilla.org/es/docs/Tools/Page_Inspector/Atajos_teclado  for example. 
It's a translation of https://developer.mozilla.org/es/docs/Tools/Page_Inspector/Keyboard_shortcuts but the only thing they've translated is the document title and document slug. 
If we delete this document now, on the grounds that it's actually not translated and potentially out of date with the risk of sending Spanish speaking MDN users to see old content. Then, what will happen is two things:

1. It'll cease to be included in sitemap XML files (for `es`). A good thing!
2. People will get a `404 Not Found` on that URL if they, had it bookmarked or linked to.

**Rationale**
If we don't gracefully deal with deletion of untranslated translations, where the slug is translated, we're running the risk of making sadness happen. 


**Audience**
Hmm..


**Proposal**
We leverage the soft-deleted documents! The page would still exist in our database (until it's purged, aka. Yari) so we can look it up like this:
```python
# somewhere in kuma/wiki/views/document.py

def react_document(request, document_slug, document_locale):
    ...
    doc, fallback_reason = _get_doc_and_fallback_reason(document_locale, document_slug)
    if doc is None:
        fallback_doc, fallback_reason, redirect_url = _default_locale_fallback(
            request, document_slug, document_locale
        )
       if fallback_doc:
            redirect(redirect_url) 
      else:
         
          # THIS IS WHAT I'M PROPOSING
          deleted_document = Document.deleted_objects.get(locale=document_locale, slug=document_slug):
          if deleted_document.parent:
                 return redirect(deleted_document.parent.get_absolute_url())
         # /END OF PROPOSAL

         raise Http404

    ...
```


**Additional context**
This will really help the effort to get rid of unmaintained translations that are of really low quality. Be that from not actually being translated or just horribly out of date for some other reason. 




Discussion:

Resolving patch:
diff --git a/kuma/wiki/views/document.py b/kuma/wiki/views/document.py
--- a/kuma/wiki/views/document.py
+++ b/kuma/wiki/views/document.py
@@ -247,6 +247,14 @@ def _default_locale_fallback(request, document_slug, document_locale):
     return fallback_doc, fallback_reason, redirect_url
 
 
+def _get_deleted_parent_redirect_url(document_locale, document_slug):
+    for document in Document.deleted_objects.filter(
+        locale=document_locale, slug=document_slug, parent__isnull=False
+    ):
+        if document.parent:
+            return document.parent.get_absolute_url()
+
+
 def _get_doc_and_fallback_reason(document_locale, document_slug):
     """
     Attempt to fetch a Document at the given locale and slug, and
@@ -885,6 +893,18 @@ def react_document(request, document_slug, document_locale):
             if redirect_url is not None:
                 return redirect(redirect_url)
         else:
+            # It could be that the document you're trying to view was deleted and
+            # the reason we're not finding a fallback is because the slug
+            # doesn't match.
+            # E.g. you're trying to view `/sv-SE/docs/Foö/Bår` but that document
+            # was soft-deleted and its parent was `/en-US/docs/Foo/Bar`
+            if document_locale != settings.LANGUAGE_CODE:  # Not in English!
+                redirect_url = _get_deleted_parent_redirect_url(
+                    document_locale, document_slug
+                )
+                if redirect_url:
+                    return redirect(redirect_url)
+
             raise Http404
 
     # We found a Document. Now we need to figure out how we're going


Test patch:
diff --git a/kuma/wiki/tests/test_views_document.py b/kuma/wiki/tests/test_views_document.py
--- a/kuma/wiki/tests/test_views_document.py
+++ b/kuma/wiki/tests/test_views_document.py
@@ -902,3 +902,24 @@ def test_wiki_only_query_params(
     assert response.status_code == status
     if status == 301:
         assert_redirect_to_wiki(response, url)
+
+
+def test_deleted_parent_redirect_url(client, root_doc):
+    """A non-en-US document, gets deleted and you try to view. Instead of a 404,
+    it at least redirects to the parent's URL."""
+    doc = Document.objects.create(
+        locale="sv-SE",
+        slug="Rot",
+        title="Rot Dokument",
+        rendered_html="<p>...</p>",
+        parent=root_doc,
+    )
+
+    response = client.get(doc.get_absolute_url())
+    assert response.status_code == 200
+
+    doc.delete()
+
+    response = client.get(doc.get_absolute_url())
+    assert response.status_code == 302
+    assert response["location"] == root_doc.get_absolute_url()