Skip to content

Commit

Permalink
Unit: Add support for removing strings
Browse files Browse the repository at this point in the history
Fixes #1974
  • Loading branch information
nijel authored and kodiakhq[bot] committed Sep 23, 2020
1 parent 5115a28 commit 619b8b0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Not yet released.
* Support markdown in contributor agreement.
* Improved source strings tracking.
* Improved JSON, YAML and CSV formats compatibility.
* Added support for removing strings.

Weblate 4.2.2
-------------
Expand Down
4 changes: 4 additions & 0 deletions weblate/templates/translate.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
{% include "snippets/component/state.html" with object=unit.translation.component %}

{% perm 'unit.edit' unit as user_can_translate %}
{% perm 'unit.delete' unit as user_can_delete %}
{% perm 'suggestion.add' unit as user_can_suggest %}
{% perm 'suggestion.accept' unit as user_can_accept_suggestion %}
{% perm 'suggestion.vote' unit.translation as user_can_vote_suggestion %}
Expand Down Expand Up @@ -170,6 +171,9 @@ <h4 class="panel-title">
{% crispy form %}
</div>
<div class="panel-footer">
{% if user_can_delete %}
<a href="{% url "delete-unit" unit_id=unit.pk %}" class="pull-right flip btn btn-danger link-post">{% trans "Remove" %}</a>
{% endif %}
<button class="btn btn-primary" type="submit" name="save" tabindex="150"
{% if locked or not user_can_translate %}
disabled="disabled"
Expand Down
28 changes: 26 additions & 2 deletions weblate/trans/tests/test_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@

"""Test for translation views."""


import time

from django.urls import reverse

from weblate.trans.models import Change
from weblate.trans.models import Change, Component
from weblate.trans.tests.test_views import ViewTestCase
from weblate.utils.hash import hash_to_checksum
from weblate.utils.state import STATE_FUZZY, STATE_READONLY, STATE_TRANSLATED
Expand Down Expand Up @@ -317,6 +316,31 @@ def add(key):
response = add("")
self.assertContains(response, "Error in parameter key")

def test_remove_unit(self):
self.assertEqual(self.component.stats.all, 16)
unit = self.get_unit()
# Deleting translation unit
response = self.client.post(reverse("delete-unit", kwargs={"unit_id": unit.pk}))
self.assertEqual(response.status_code, 403)
# Lack of permissions
response = self.client.post(
reverse("delete-unit", kwargs={"unit_id": unit.source_unit.pk})
)
self.assertEqual(response.status_code, 403)
# Make superuser
self.user.is_superuser = True
self.user.save()
# Deleting translation unit
response = self.client.post(reverse("delete-unit", kwargs={"unit_id": unit.pk}))
self.assertEqual(response.status_code, 403)
# Actual removal
response = self.client.post(
reverse("delete-unit", kwargs={"unit_id": unit.source_unit.pk})
)
self.assertEqual(response.status_code, 302)
component = Component.objects.get(pk=self.component.pk)
self.assertEqual(component.stats.all, 12)


class EditIphoneTest(EditTest):
has_plurals = False
Expand Down
13 changes: 13 additions & 0 deletions weblate/trans/views/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,16 @@ def new_unit(request, project, component, lang):
messages.success(request, _("New string has been added."))

return redirect(translation)


@login_required
@require_POST
def delete_unit(request, unit_id):
"""Delete unit."""
unit = get_object_or_404(Unit, pk=unit_id)

if not request.user.has_perm("unit.delete", unit):
raise PermissionDenied()

unit.translation.delete_unit(request, unit)
return redirect(unit.translation)
5 changes: 5 additions & 0 deletions weblate/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@
weblate.trans.views.files.upload_translation,
name="upload_translation",
),
path(
"unit/<int:unit_id>/delete/",
weblate.trans.views.edit.delete_unit,
name="delete-unit",
),
path(
"new-unit/<name:project>/<name:component>/<name:lang>/",
weblate.trans.views.edit.new_unit,
Expand Down

0 comments on commit 619b8b0

Please sign in to comment.