Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11168 clean unused glossary languages #12331

Merged
merged 19 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Not yet released.

**Improvements**

* Stale, empty glossaries are now automatically removed.
nijel marked this conversation as resolved.
Show resolved Hide resolved
* :ref`mt-deepl` now supports specifying translation context.

**Bug fixes**
Expand Down
9 changes: 7 additions & 2 deletions weblate/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3323,7 +3323,11 @@ def test_add_plural(self) -> None:
)

def test_delete(self) -> None:
start_count = Translation.objects.count()
def _translation_count():
# ignore glossaries as stale glossaries are also cleaned out
return Translation.objects.filter(component__is_glossary=False).count()

start_count = _translation_count()
self.do_request(
"api:translation-detail", self.translation_kwargs, method="delete", code=403
)
Expand All @@ -3334,7 +3338,8 @@ def test_delete(self) -> None:
superuser=True,
code=204,
)
self.assertEqual(Translation.objects.count(), start_count - 1)

self.assertEqual(_translation_count(), start_count - 1)


class UnitAPITest(APIBaseTest):
Expand Down
34 changes: 32 additions & 2 deletions weblate/glossary/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

from __future__ import annotations

from django.db import transaction

from weblate.lang.models import Language
from weblate.trans.models import Component
from weblate.trans.models import Component, Project, Translation
from weblate.utils.celery import app
from weblate.utils.lock import WeblateLockTimeoutError
from weblate.utils.stats import prefetch_stats


@app.task(
nijel marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -16,7 +19,11 @@
retry_backoff=60,
)
def sync_glossary_languages(pk: int, component: Component | None = None) -> None:
"""Add missing glossary languages."""
"""Add missing glossary languages and delete empty stale glossaries."""
# Delete stale glossaries
cleanup_stale_glossaries(component.project)

# Add missing glossary languages
if component is None:
component = Component.objects.get(pk=pk)

Expand All @@ -40,6 +47,29 @@ def sync_glossary_languages(pk: int, component: Component | None = None) -> None
component.create_translations_task()


@app.task(trail=False, autoretry_for=(Project.DoesNotExist, WeblateLockTimeoutError))
def cleanup_stale_glossaries(project: int | Project) -> None:
if isinstance(project, int):
project = Project.objects.get(pk=project)

languages_in_non_glossary_components: set[int] = set(
Translation.objects.filter(
component__project=project, component__is_glossary=False
).values_list("language_id", flat=True)
)
gersona marked this conversation as resolved.
Show resolved Hide resolved

glossary_translations = prefetch_stats(
Translation.objects.filter(
component__project=project, component__is_glossary=True
).exclude(language__id__in=languages_in_non_glossary_components)
gersona marked this conversation as resolved.
Show resolved Hide resolved
)
gersona marked this conversation as resolved.
Show resolved Hide resolved
for glossary in glossary_translations:
if glossary.stats.translated == 0:
glossary.remove()
transaction.on_commit(glossary.stats.update_parents)
gersona marked this conversation as resolved.
Show resolved Hide resolved
transaction.on_commit(glossary.component.schedule_update_checks)
gersona marked this conversation as resolved.
Show resolved Hide resolved


@app.task(
trail=False,
autoretry_for=(Component.DoesNotExist, WeblateLockTimeoutError),
Expand Down
19 changes: 18 additions & 1 deletion weblate/glossary/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from django.urls import reverse

from weblate.glossary.models import get_glossary_terms, get_glossary_tsv
from weblate.glossary.tasks import sync_terminology
from weblate.glossary.tasks import (
cleanup_stale_glossaries,
sync_terminology,
)
from weblate.lang.models import Language
from weblate.trans.models import Unit
from weblate.trans.tests.test_views import ViewTestCase
from weblate.trans.tests.utils import get_test_file
Expand Down Expand Up @@ -472,3 +476,16 @@ def test_tsv(self) -> None:
lines = list(reader)
self.assertEqual(len(lines), 163)
self.assertTrue(all(len(line) == 2 for line in lines))

def test_stale_glossaries_cleanup(self) -> None:
initial_count = self.glossary_component.translation_set.count()
# delete one translation
german = Language.objects.get(code="de")
self.component.translation_set.get(language=german).remove(self.user)

cleanup_stale_glossaries(self.project.id)

# check that only the one glossary has been deleted
self.assertEqual(
self.glossary_component.translation_set.count(), initial_count - 1
)
6 changes: 5 additions & 1 deletion weblate/trans/models/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,9 @@ def get_export_url(self):
return self.component.get_export_url()

def remove(self, user: User) -> None:
"""Remove translation from the VCS."""
"""Remove translation from the Database and VCS."""
from weblate.glossary.tasks import cleanup_stale_glossaries

author = user.get_author_name()
# Log
self.log_info("removing %s as %s", self.filenames, author)
Expand Down Expand Up @@ -1362,6 +1364,8 @@ def remove(self, user: User) -> None:
author=user,
)

cleanup_stale_glossaries.delay(self.component.project.id)
nijel marked this conversation as resolved.
Show resolved Hide resolved
gersona marked this conversation as resolved.
Show resolved Hide resolved

def handle_store_change(
self,
request: AuthenticatedHttpRequest,
Expand Down
Loading