Repository: WeblateOrg/weblate

Issue:
Translation Memory Maintenance - Delete Entries
It would be good to be able to maintain the TM in one way or the other.

Perhaps 
- [ ] Search & Replace in TM
- [ ] Search & Delete entries in TM

Or perhpas, as a "simple" (?) starting point, giving the translator the option to delete single entries from the TM when they see the result list in **Automatic Suggestions**. Like perhaps:

- [ ] Delete single entry in **Automatic Suggestions** view:
![image](https://user-images.githubusercontent.com/26718215/130028004-4e00f2f1-0d30-4ddb-a904-d351711ee610.png)




Discussion:
This issue has been added to the backlog. It is not scheduled on the Weblate roadmap, but it eventually might be implemented.

In case you need this feature soon, please consider helping or push it by [funding the development](https://weblate.org/support/).
Let's stick with the basic stuff for now - add delete option to "Automatic suggestions" tab. That will allow deleting wrong entries without adding a complex user interface.

The question is: Who should be able to remove the strings - should that be separate permission? Anybody who can access the memory? Admins only?

I'd place the delete button next to existing action buttons, to make the UI consistent.
Very good next step. Thank you. I would say anyone with TM / memory permission. That would be sufficient.
It is the translators who will encounter those "wrong segments". And they should be able to delete them.
Trust! ;-)
@nijel I made the PR for adding delete option to "Automatic suggestions" tab. I have some questions:

We are getting translations [here](https://github.com/WeblateOrg/weblate/blob/main/weblate/machinery/views.py#L331) and we are using [this](https://github.com/WeblateOrg/weblate/blob/main/weblate/machinery/base.py#L348) method for translating the strings and caching them.

What flow should be there to delete the translations here in the base.py and not to have the translation again on page reload?

To be more specific, Should I remove the `Unit` from translating for the specific language?


As for the removal, I think best would be a separate endpoint which would get just `id` of `Memory` object - in the end, it's the only backend where deletion is doable. Maybe it's better to expose this properly in the API and utilize the API here.

So for the removal, I'd probably go this way:

- Add API endpoint for translation memory entries
- To make it simpler for now, it can only support listing and removal
- The frontend would call the API and remove the row from results upon successful removal
Via API would be awesome. This would enable querying and listing of TM entries in general. Nice to have. 
The removal should happen by Memory id.

It is currently not present in the response, it can be added here (`result.id`):

https://github.com/WeblateOrg/weblate/blob/8b97ddaf6b709ffc90485069443c03852378df8e/weblate/memory/machine.py#L68-L74

The client side code to handle this is here:

https://github.com/WeblateOrg/weblate/blob/8b97ddaf6b709ffc90485069443c03852378df8e/weblate/static/editor/full.js#L247-L300
I've just touched related code in dc8266f08def4b03070b17f31712afa8e702d67f and I think the correct approach would be to include removal URL in the response and let the client side handle the removal via doing DELETE requests on these endpoints. This way the interface is generic for possible extensions to other services.

Something like this:

```patch
--- weblate/memory/machine.py
+++ weblate/memory/machine.py
@@ -72,4 +72,5 @@ class WeblateMemory(MachineTranslation):
                 "origin": result.get_origin_display(),
                 "source": result.source,
                 "show_quality": True,
+                "delete_url": reverse("api:memory-detail"),
             }
```

Resolving patch:
diff --git a/weblate/memory/machine.py b/weblate/memory/machine.py
--- a/weblate/memory/machine.py
+++ b/weblate/memory/machine.py
@@ -17,6 +17,8 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 
+from django.urls import reverse
+
 from weblate.machinery.base import MachineTranslation, get_machinery_language
 from weblate.memory.models import Memory
 
@@ -72,4 +74,7 @@ def download_translations(
                 "origin": result.get_origin_display(),
                 "source": result.source,
                 "show_quality": True,
+                "delete_url": reverse("api:memory-detail", kwargs={"pk": result.id})
+                if user is not None and user.has_perm("memory.delete", result)
+                else None,
             }


Test patch:
diff --git a/weblate/memory/tests.py b/weblate/memory/tests.py
--- a/weblate/memory/tests.py
+++ b/weblate/memory/tests.py
@@ -73,6 +73,24 @@ def test_machine(self):
                     "source": "Hello",
                     "text": "Ahoj",
                     "show_quality": True,
+                    "delete_url": None,
+                }
+            ],
+        )
+
+        self.user.is_superuser = True
+        self.user.save()
+        self.assertEqual(
+            machine_translation.translate(unit, user=self.user, search="Hello"),
+            [
+                {
+                    "quality": 100,
+                    "service": "Weblate Translation Memory",
+                    "origin": "File: test",
+                    "source": "Hello",
+                    "text": "Ahoj",
+                    "show_quality": True,
+                    "delete_url": f"/api/memory/{Memory.objects.first().pk}/",
                 }
             ],
         )