Repository: mdn/kuma

Issue:
内部服务器错误(Internal Server Error)
**Summary**
_What is the problem?_

Each time I try to submit the translation,It shows error in specific page--`/docs/Learn/CSS/Building_blocks/Selectors`

The problem never happend when I try to submit in other pages.
**Steps To Reproduce (STR)**
_How can we reproduce the problem?_

1. try to translate the page /docs/Learn/CSS/Building_blocks/Selectors into zh-CN
2. which would open page https://wiki.developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/Selectors$translate?tolocale=zh-CN
3. submit and it jumped to the "内部服务器错误"page.(Internal Server Error)


**Actual behavior**
_What actually happened?_
内部服务器错误Internal Server Error

**Expected behavior**
_What did you expect to happen?_
It should jumped to the page revised.

**Additional context**
_Is there anything else we should know?_
None,thanks.


Discussion:
also as _Internal Server Error_ in English
I can reproduce on staging.
Timebox investigate the cause. 

Non-attribute err has no summary?
The explanation is that https://wiki.developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/Selectors is deleted. Technically, soft-deleted. 
```
>>> Document.deleted_objects.filter(slug='Learn/CSS/Building_blocks/Selectors', locale='zh-CN')
<QuerySet [<Document: /zh-CN/docs/Learn/CSS/Building_blocks/Selectors (CSS selectors)>]>
>>>
```

If you for some reason is reading https://developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/Selectors it actually falls back on the en-US text. 

You can see that it was deleted in September 2019 (by SphinxKnight)
https://wiki.developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/Selectors

So, @VemboLiu Can you share how you even got to that?
Actually, I think I know what's going on. 
You're on https://developer.mozilla.org/zh-CN/docs/Learn/CSS/Building_blocks/Selectors and see all the English text and you decide you want to translate it to Chinese (Simplified?). You click the language drop-down menu and select the bottom option (添加翻译) ("Add translation")
That takes you to https://wiki.developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors$locales and there you click on "中文 (简体) (zh-CN)"  and from there it takes you to https://wiki.developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors$translate?tolocale=zh-CN and you make your edits and press the "Publish" button. And that's when the error happens. 

So @VemboLiu you're not doing anything wrong. It's our fault! Sorry. 
What should happen is that before it creates this new translation it needs to notice if there was a previous soft-delete of it and just completely delete it (hard delete). 

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
@@ -189,6 +189,15 @@ def translate(request, document_slug, document_locale):
             # If we are submitting the whole form, we need to check that
             # the Revision is valid before saving the Document.
             if doc_form.is_valid() and (which_form == "doc" or rev_form.is_valid()):
+
+                # 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"],
+                ):
+                    soft_deleted_document.delete(purge=True)
+
                 doc = doc_form.save(parent=parent_doc)
 
                 if which_form == "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
@@ -71,3 +71,33 @@ def test_translate_post(root_doc, trans_doc_client):
         len(Document.objects.filter(title=root_doc.title + " Redirect 1", locale="fr"))
         == 0
     )
+
+
+def test_translate_over_deleted_document(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.
+    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".
+    Document.objects.create(slug=root_doc.slug, locale="fr").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=root_doc.slug, locale="fr")
+    assert Document.objects.filter(slug=root_doc.slug, locale="fr")