Skip to content

Commit

Permalink
glossary: add terminology entries directly
Browse files Browse the repository at this point in the history
- avoids expensive sync_terminology call on adding every glossary entry
- makes added entries credited to the adding user

Fixes WeblateOrg#9661
  • Loading branch information
nijel committed Aug 1, 2023
1 parent 8fb25cc commit a8de330
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Not yet released.
* Rendering strings with plurals, placeholders or alternative translations.
* User API now includes last sign in date.
* User API token is now hidden for privacy reasons by default.
* Faster adding terms to glossary.

**Bug fixes**

Expand Down
29 changes: 23 additions & 6 deletions weblate/glossary/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,26 +315,43 @@ def test_get_single(self):
{"thank"},
)

def test_add(self):
"""Test for adding term from translate page."""
def do_add_unit(self, **kwargs):
unit = self.get_unit("Thank you for using Weblate.")
# Add term
response = self.client.post(
reverse("js-add-glossary", kwargs={"unit_id": unit.pk}),
{"source": "source", "target": "překlad", "translation": self.glossary.pk},
{
"source": "source",
"target": "překlad",
"translation": self.glossary.pk,
**kwargs,
},
)
content = json.loads(response.content.decode())
self.assertEqual(content["responseCode"], 200)

def test_add(self):
"""Test for adding term from translate page."""
start = Unit.objects.count()
self.do_add_unit()
# Should be added to the source and translation only
self.assertEqual(Unit.objects.count(), start + 2)

def test_add_terminology(self):
start = Unit.objects.count()
self.do_add_unit(terminology=1)
# Should be added to all languages
self.assertEqual(Unit.objects.count(), start + 4)

def test_add_duplicate(self):
self.test_add()
self.test_add()
self.do_add_unit()
self.do_add_unit()

def test_terminology(self):
start = Unit.objects.count()

# Add single term
self.test_add()
self.do_add_unit()

# Verify it has been added to single language (+ source)
unit = self.glossary_component.source_translation.unit_set.get(source="source")
Expand Down
21 changes: 13 additions & 8 deletions weblate/trans/models/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,20 +1373,28 @@ def add_unit( # noqa: C901
auto_context: bool = False,
is_batch_update: bool = False,
skip_existing: bool = False,
sync_terminology: bool = True,
state: int | None = None,
):
if isinstance(source, list):
source = join_plural(source)
user = request.user if request else None
component = self.component
if self.is_source:
translations = [self]
translations.extend(
component.translation_set.exclude(id=self.id).select_related("language")
translations = (
self,
*component.translation_set.exclude(id=self.id).select_related(
"language"
),
)
elif component.is_glossary and "terminology" in Flags(extra_flags):
translations = (
component.source_translation,
*component.translation_set.exclude(
id=component.source_translation.id
).select_related("language"),
)
else:
translations = [component.source_translation, self]
translations = (component.source_translation, self)
has_template = component.has_template()
source_unit = None
result = None
Expand Down Expand Up @@ -1503,8 +1511,6 @@ def add_unit( # noqa: C901
component.update_variants(
updated_units=Unit.objects.filter(pk__in=unit_ids)
)
if sync_terminology:
component.schedule_sync_terminology()
component.invalidate_cache()
component_post_update.send(sender=self.__class__, component=component)
self.was_new = 1
Expand Down Expand Up @@ -1600,7 +1606,6 @@ def sync_terminology(self):
"",
is_batch_update=True,
skip_existing=True,
sync_terminology=False,
)
self.was_new += 1
self.notify_new(None)
Expand Down

0 comments on commit a8de330

Please sign in to comment.