Skip to content

Commit

Permalink
Fix possibility to cancel translation process (fixes #1164)
Browse files Browse the repository at this point in the history
  • Loading branch information
timobrembeck committed Feb 17, 2022
1 parent cc47f15 commit 0ae16d2
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 164 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ UNRELEASED
* [ [#1172](https://github.com/digitalfabrik/integreat-cms/issues/1172) ] Fix filtering for locations in event list
* [ [#1185](https://github.com/digitalfabrik/integreat-cms/issues/1185) ] Fix feedback API
* [ [#1188](https://github.com/digitalfabrik/integreat-cms/issues/1188) ] Fix error in broken link checker
* [ [#1184](https://github.com/digitalfabrik/integreat-cms/issues/1184) ] Allow user to embed live content from current region
* [ [#1164](https://github.com/digitalfabrik/integreat-cms/issues/1164) ] Fix possibility to cancel translation process


2022.2.0-beta
Expand Down
12 changes: 6 additions & 6 deletions integreat_cms/cms/models/abstract_content_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,22 @@ def best_translation(self):
"""
return self.backend_translation or self.default_translation

def get_translation_state(self, language):
def get_translation_state(self, language_slug):
"""
This function returns the current state of a translation in the given language.
:param language: The desired :class:`~integreat_cms.cms.models.languages.language.Language`
:type language: ~integreat_cms.cms.models.languages.language.Language
:param language_slug: The slug of the desired :class:`~integreat_cms.cms.models.languages.language.Language`
:type language_slug: str
:return: A string describing the state of the translation, one of :data:`~integreat_cms.cms.constants.translation_status.CHOICES`
:rtype: str
"""
translation = self.get_translation(language.slug)
translation = self.get_translation(language_slug)
if not translation:
return translation_status.MISSING
if translation.currently_in_translation:
return translation_status.IN_TRANSLATION
source_language = self.region.get_source_language(language.slug)
source_language = self.region.get_source_language(language_slug)
if not source_language:
return translation_status.UP_TO_DATE
source_translation = self.get_translation(source_language.slug)
Expand All @@ -244,7 +244,7 @@ def translation_states(self):
:rtype: dict
"""
return {
node.slug: (node.language, self.get_translation_state(node))
node.slug: (node.language, self.get_translation_state(node.slug))
for node in self.region.language_tree
if node.active
}
Expand Down
11 changes: 10 additions & 1 deletion integreat_cms/cms/templates/_form_language_tabs.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@
<a class="block py-2 px-4" href="{% build_url target=target region_slug=request.region.slug language_slug=other_language.slug content_field=content_field content_id=instance.id %}">
{% endif %}
{% if other_status == translation_status.IN_TRANSLATION %}
<span title="{% trans 'Currently in translation' %}">
<span {% if other_language == language %} id="currently-in-translation-state" {% endif %} title="{% trans 'Currently in translation' %}">
<i data-feather="clock"></i>
</span>
{# For the current language, provide fallback icons in case translation process is cancelled #}
{% if other_language == language %}
<span id="reset-translation-state-{{ translation_status.OUTDATED }}" title="{% trans 'Translation outdated' %}" class="hidden">
<i data-feather="alert-triangle"></i>
</span>
<span id="reset-translation-state-{{ translation_status.UP_TO_DATE }}" title="{% trans 'Translation up-to-date' %}" class="hidden">
<i data-feather="check"></i>
</span>
{% endif %}
{% elif other_status == translation_status.OUTDATED %}
<span title="{% trans 'Translation outdated' %}">
<i data-feather="alert-triangle"></i>
Expand Down
31 changes: 14 additions & 17 deletions integreat_cms/cms/templates/pages/page_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,22 @@ <h1 class="heading">
{% include "_form_language_tabs.html" with target='edit_page' instance=page_form.instance content_field="page_id" %}
<div class="w-full mb-4 rounded border-2 border-blue-500 bg-white flex-auto">
<div class="w-full p-4">
{% if page_translation_form.instance.currently_in_translation %}
<div id="currently-in-translation-warning">
<i data-feather="alert-triangle" class="text-red-500"></i>
<label class="inline-block mb-2 font-bold">{% trans 'Warning' %}:</label>
{% trans 'Translation in progress' %}
(<a href="#"
data-cancel-translation-process
data-url="{% url 'cancel_translation_process_ajax' region_slug=request.region.slug language_slug=page_translation_form.instance.language.slug page_id=page.id %}"
id="cancel-translation"
class="text-blue-500 hover:underline">{% trans 'Cancel translation process' %}</a>)
<br>
</div>
{% endif %}
<div class="flex justify-between mr-2">
<label for="{{ page_translation_form.title.id_for_label }}"
data-slugify-url="{% url 'slugify_ajax' region_slug=request.region.slug language_slug=language.slug model_type='page' %}{% if page_form.instance.id %}?model_id={{ page_form.instance.id }}{% endif %}">
data-slugify-url="{% url 'slugify_ajax' region_slug=request.region.slug language_slug=language.slug model_type='page' %}{% if page_form.instance.id %}?model_id={{ page_form.instance.id }}{% endif %}">
{{ page_translation_form.title.label }}
</label>
{% if page_translation_form.instance.id %}
Expand Down Expand Up @@ -94,22 +107,6 @@ <h1 class="heading">
<i data-feather="copy"></i>
</a><br>
{% endif %}
{% if page_translation_form.instance.currently_in_translation %}
<div id="trans-warn">
<i data-feather="alert-triangle" class="text-red-800"></i>
<label class="inline-block mb-2 font-bold">{% trans 'Warning' %}:</label>
{% trans 'Translation in progress' %}
<a href="#"
data-unset-translation-state
data-unset-translation-state-id="{{ page.id }}"
data-unset-translation-state-language-code="{{ page_translation_form.instance.language.slug }}"
id="undo-translation"
class="italic text-purple-500"
data-url="{% url 'post_translation_state_ajax' region_slug=request.region.slug %}">
{% trans 'Abort translation process' %}
</a><br>
</div>
{% endif %}
{% endif %}
<div id="link-container" class="flex items-center">
<label for="{{ page_translation_form.slug.id_for_label }}" class="mr-2 whitespace-nowrap">
Expand Down
6 changes: 3 additions & 3 deletions integreat_cms/cms/urls/protected.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,9 @@
),
),
path(
"post-translation-state/",
pages.post_translation_state_ajax,
name="post_translation_state_ajax",
"cancel-translation-process/<slug:language_slug>/<int:page_id>/",
pages.cancel_translation_process_ajax,
name="cancel_translation_process_ajax",
),
path(
"search-poi/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_context_data(self, **kwargs):
for language in region.active_languages:
language_coverage_data = Counter()
for page in pages:
translation_state = page.get_translation_state(language)
translation_state = page.get_translation_state(language.slug)
language_coverage_data[translation_state] += 1

if translation_state == translation_status.OUTDATED:
Expand Down
2 changes: 1 addition & 1 deletion integreat_cms/cms/views/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
get_page_order_table_ajax,
get_new_page_order_table_ajax,
render_mirrored_page_field,
post_translation_state_ajax,
cancel_translation_process_ajax,
)
from .page_sbs_view import PageSideBySideView
from .page_revision_view import PageRevisionView
Expand Down
38 changes: 27 additions & 11 deletions integreat_cms/cms/views/pages/page_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from treebeard.exceptions import InvalidPosition, InvalidMoveToDescendant

from ....api.decorators import json_response
from ....xliff.utils import pages_to_xliff_file
from ...constants import text_directions
from ...decorators import permission_required
Expand Down Expand Up @@ -357,32 +358,47 @@ def download_xliff(request, region_slug, language_slug):

@require_POST
@permission_required("cms.change_page")
@json_response
# pylint: disable=unused-argument
def post_translation_state_ajax(request, region_slug):
def cancel_translation_process_ajax(request, region_slug, language_slug, page_id):
"""
This view is called for manually unseting the translation process
This view is called for manually unsetting the translation process
:param request: ajax request
:type request: ~django.http.HttpRequest
:param region_slug: The slug of the current region
:type region_slug: str
:param language_slug: The slug of the current language
:type language_slug: str
:param page_id: The id of the requested page
:type page_id: int
:return: on success returns language of updated translation
:rtype: ~django.http.JsonResponse
"""
decoded_json = json.loads(request.body.decode("utf-8"))
target_language = decoded_json["language"]
page_id = decoded_json["pageId"]
translation_state = decoded_json["translationState"]
region = request.region
page = get_object_or_404(region.pages, id=page_id)
page_translation = page.get_translation(target_language.slug)
if page_translation:
page_translation.update(currently_in_translation=translation_state)
return JsonResponse({"language": target_language})
page_translation = page.get_translation(language_slug)
if not page_translation:
return JsonResponse(
{
"error": f"Page {page} does not have a translation for language '{language_slug}'"
},
status=404,
)
page_translation.currently_in_translation = False
page_translation.save()
# Get new (respectively old) translation state
translation_state = page.get_translation_state(language_slug)
return JsonResponse(
{"error": _("Could not update page translation state")}, status=400
{
"success": f"Cancelled translation process for page {page} and language {page_translation.language}",
"languageSlug": page_translation.language.slug,
"translationState": translation_state,
}
)


Expand Down

0 comments on commit 0ae16d2

Please sign in to comment.