Skip to content

Commit d387b86

Browse files
committed
Pretranslate: skip existing pretranslations, include fuzzy, attribute to system users
1 parent 0e80c9c commit d387b86

2 files changed

Lines changed: 41 additions & 23 deletions

File tree

‎pontoon/batch/actions.py‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from django.db.models import QuerySet
3+
from django.db.models import Q, QuerySet
44
from django.utils import timezone
55

66
from pontoon.actionlog.models import ActionLog
@@ -12,6 +12,7 @@
1212
TranslationMemoryEntry,
1313
)
1414
from pontoon.base.models.translation import TranslationQuerySet
15+
from pontoon.base.user_utils import get_pretranslation_authors
1516
from pontoon.batch import utils
1617
from pontoon.messaging.notifications import send_badge_notification
1718
from pontoon.pretranslation.pretranslate import get_pretranslation
@@ -414,13 +415,12 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
414415
Enable teams to pretranslate an entire project or a subset of strings on projects
415416
for which pretranslation is not enabled by the admins.
416417
"""
418+
pt_authors = get_pretranslation_authors()
417419

418-
entity_pks_with_translation = set(
419-
Translation.objects.filter(
420-
locale=locale,
421-
entity__in=entities,
422-
approved=True,
423-
).values_list("entity__pk", flat=True)
420+
excluded_entity_pks = set(
421+
Translation.objects.filter(locale=locale, entity__in=entities)
422+
.filter(Q(approved=True) | Q(user__in=pt_authors.values()))
423+
.values_list("entity__pk", flat=True)
424424
)
425425

426426
already_active_entity_pks = set(
@@ -431,9 +431,7 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
431431
).values_list("entity__pk", flat=True)
432432
)
433433

434-
eligible_entities = entities.exclude(pk__in=entity_pks_with_translation)
435-
436-
before_level = badges_translation_level(user)
434+
eligible_entities = entities.exclude(pk__in=excluded_entity_pks)
437435

438436
translations_to_create = []
439437

@@ -447,7 +445,7 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
447445
invalid_entity_pks.append(entity.pk)
448446
continue
449447

450-
string, engine = pretranslation
448+
string, author_key = pretranslation
451449

452450
try:
453451
_, value, properties = parse_source_string_to_json(
@@ -470,7 +468,7 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
470468
fuzzy=False,
471469
pretranslated=True,
472470
active=entity.pk not in already_active_entity_pks,
473-
user=user,
471+
user=pt_authors[author_key],
474472
value=value,
475473
properties=properties,
476474
)
@@ -488,23 +486,14 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
488486
actions_to_log = [
489487
ActionLog(
490488
action_type=ActionLog.ActionType.TRANSLATION_CREATED,
491-
performed_by=user,
489+
performed_by=t.user,
492490
translation=t,
493491
)
494492
for t in changed_translations
495493
]
496494

497495
ActionLog.objects.bulk_create(actions_to_log)
498496

499-
after_level = badges_translation_level(user)
500-
501-
badge_update = {}
502-
503-
if after_level > before_level:
504-
badge_update["level"] = after_level
505-
badge_update["name"] = "Translation Champion"
506-
send_badge_notification(user, badge_update["name"], badge_update["level"])
507-
508497
changed_translation_pks = [t.pk for t in changed_translations]
509498

510499
return {
@@ -516,7 +505,6 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
516505
else None,
517506
"changed_translation_pks": changed_translation_pks,
518507
"invalid_translation_pks": invalid_entity_pks,
519-
"badge_update": badge_update,
520508
}
521509

522510

‎pontoon/batch/tests/test_utils.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from fluent.syntax import FluentParser, FluentSerializer
66

77
from pontoon.base.models import Entity, Locale, Translation
8+
from pontoon.base.user_utils import get_pretranslation_authors
89
from pontoon.batch.actions import (
910
copy_translation_from_locale,
1011
pretranslate_translations,
@@ -245,3 +246,32 @@ def test_pretranslate_translations_skips_approved_includes_suggestions(
245246
).exists()
246247

247248
assert result["count"] == 2
249+
250+
251+
@pytest.mark.django_db
252+
@patch("pontoon.batch.actions.get_pretranslation")
253+
def test_pretranslate_translations_skips_existing_pretranslations(
254+
get_pretranslation_mock,
255+
):
256+
get_pretranslation_mock.return_value = ("key = pretranslated value", "gt")
257+
258+
project = ProjectFactory(slug="project5", name="Project5")
259+
resource = ResourceFactory(project=project, path="resource.ftl", format="fluent")
260+
entity = EntityFactory(resource=resource, string="key1 = value1")
261+
locale = Locale.objects.get(code="en-ZA")
262+
gt_user = get_pretranslation_authors()["gt"]
263+
264+
TranslationFactory(
265+
entity=entity,
266+
locale=locale,
267+
string="existing pretranslation",
268+
pretranslated=True,
269+
user=gt_user,
270+
)
271+
272+
result = pretranslate_translations(
273+
UserFactory(), locale, Entity.objects.filter(pk=entity.pk)
274+
)
275+
276+
assert Translation.objects.filter(entity=entity).count() == 1
277+
assert result["count"] == 0

0 commit comments

Comments
 (0)