Repository: mdn/kuma

Issue:
ISE when trying to submit a translation
**Summary**
_What is the problem?_
Submitting a translation in Italian for https://wiki.developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/Publishing_your_website$translate?tolocale=it fails and gives an ISE

**Steps To Reproduce (STR)**
1. Went to https://wiki.developer.mozilla.org/en-US/docs/Learn/Getting_started_with_the_web/Publishing_your_website$translate?tolocale=it
2. Added / Changed a bunch of words
3. Clicked "Publish"


**Actual behavior**
An ISE / error 500 happened, preventing to save the work.


**Expected behavior**
The localization is save, no error, the reader is able to read a new document


**Additional context**

* First report here: https://discourse.mozilla.org/t/50-internal-server-error-while-saving/58804
* I was not able to reproduce this on another locale (pl) for this page



Discussion:
For those with access, I think it's this error: https://sentry.prod.mozaws.net/operations/mdn-prod/issues/7440553/?query=is:unresolved
Actually, I suspect this is a dupe of https://github.com/mdn/kuma/issues/6766
I have to re-read that one one more time to get my brain back into it. 

Resolving patch:
diff --git a/kuma/wiki/views/translate.py b/kuma/wiki/views/translate.py
--- a/kuma/wiki/views/translate.py
+++ b/kuma/wiki/views/translate.py
@@ -3,6 +3,7 @@
 from csp.decorators import csp_update
 from django.conf import settings
 from django.core.exceptions import ObjectDoesNotExist
+from django.db.models import Q
 from django.http import Http404, JsonResponse
 from django.shortcuts import get_object_or_404, redirect, render
 from django.utils.safestring import mark_safe
@@ -192,10 +193,10 @@ def translate(request, document_slug, document_locale):
 
                 # If the document you're about to save already exists, as a
                 # soft-delete, then really delete it first.
-                for soft_deleted_document in Document.deleted_objects.filter(
-                    locale=doc_form.cleaned_data["locale"],
-                    slug=doc_form.cleaned_data["slug"],
-                ):
+                previously_deleted_documents = Document.deleted_objects.filter(
+                    locale=doc_form.cleaned_data["locale"]
+                ).filter(Q(slug=doc_form.cleaned_data["slug"]) | Q(parent=parent_doc))
+                for soft_deleted_document in previously_deleted_documents:
                     soft_deleted_document.delete(purge=True)
 
                 doc = doc_form.save(parent=parent_doc)


Test patch:
diff --git a/kuma/wiki/tests/test_views_translate.py b/kuma/wiki/tests/test_views_translate.py
--- a/kuma/wiki/tests/test_views_translate.py
+++ b/kuma/wiki/tests/test_views_translate.py
@@ -73,9 +73,10 @@ def test_translate_post(root_doc, trans_doc_client):
     )
 
 
-def test_translate_over_deleted_document(root_doc, trans_doc_client):
+def test_translate_over_deleted_document_same_slug(root_doc, trans_doc_client):
     """Trying to create a new translation when there already existed a document
-    for that locale, but it was soft-deleted.
+    for that locale, but it was soft-deleted. Where "already existed" means that the
+    previous soft-delete had the same slug.
     If this is the case, it should fully delete the soft-deleted document and
     proceed with the new translation.
 
@@ -83,7 +84,7 @@ def test_translate_over_deleted_document(root_doc, trans_doc_client):
     worked.
     """
     # Create the soft-delete document that would otherwise "be in the way".
-    Document.objects.create(slug=root_doc.slug, locale="fr").delete()
+    Document.objects.create(slug=root_doc.slug, locale="fr", parent=root_doc).delete()
 
     data = {
         "slug": root_doc.slug,
@@ -101,3 +102,35 @@ def test_translate_over_deleted_document(root_doc, trans_doc_client):
 
     assert not Document.deleted_objects.filter(slug=root_doc.slug, locale="fr")
     assert Document.objects.filter(slug=root_doc.slug, locale="fr")
+
+
+def test_translate_over_deleted_document_different_slug(root_doc, trans_doc_client):
+    """Trying to create a new translation when there already existed a document
+    for that locale, but it was soft-deleted. Where "already existed" means that the
+    previous soft-delete shared the same parent.
+    If this is the case, it should fully delete the soft-deleted document and
+    proceed with the new translation.
+
+    Much of this is to assert that the fix in https://github.com/mdn/kuma/issues/6673
+    worked.
+    """
+    # Create the soft-delete document that would otherwise "be in the way".
+    french_slug = root_doc.slug + "frenchness"
+    Document.objects.create(slug=french_slug, locale="fr", parent=root_doc).delete()
+
+    data = {
+        "slug": root_doc.slug,
+        "title": root_doc.title,
+        "content": root_doc.current_revision.content,
+        "form-type": "both",
+        "toc_depth": 1,
+    }
+
+    url = reverse("wiki.translate", args=(root_doc.slug,))
+    url += "?tolocale=fr"
+
+    response = trans_doc_client.post(url, data, HTTP_HOST=settings.WIKI_HOST)
+    assert response.status_code == 302
+
+    assert not Document.deleted_objects.filter(slug=french_slug, locale="fr")
+    assert Document.objects.filter(slug=root_doc.slug, locale="fr")