11import logging
22
3- from django .db .models import QuerySet
3+ from django .db .models import Q , QuerySet
44from django .utils import timezone
55
66from pontoon .actionlog .models import ActionLog
1212 TranslationMemoryEntry ,
1313)
1414from pontoon .base .models .translation import TranslationQuerySet
15+ from pontoon .base .user_utils import get_pretranslation_authors
1516from pontoon .batch import utils
1617from pontoon .messaging .notifications import send_badge_notification
1718from 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
0 commit comments