Skip to content

Instantly share code, notes, and snippets.

@mathjazz
Created May 31, 2017 15:30
Show Gist options
  • Select an option

  • Save mathjazz/721c27e510537b657bd6125ec2370c40 to your computer and use it in GitHub Desktop.

Select an option

Save mathjazz/721c27e510537b657bd6125ec2370c40 to your computer and use it in GitHub Desktop.
from pontoon.base.models import *
from bulk_update.helper import bulk_update
tm_entries_to_update = []
"""
Generate a dict of TM entries where translation ForeignKey is null
- key: serialized TM entry data
- value: reference to a TM entry
"""
tm_entries_dict = {}
tm_entries_without_translation = TranslationMemoryEntry.objects.filter(translation__isnull=True)
tm_entries_without_translation_list = list(tm_entries_without_translation)
tm_entries_without_translation_values = list(tm_entries_without_translation.values_list("entity", "locale", "source", "target"))
for i, value in enumerate(tm_entries_without_translation_values):
tm_entries_dict[unicode(value[0]) + "\x04" + unicode(value[1]) + "\x04" + unicode(value[2]) + "\x04" + unicode(value[3])] = tm_entries_without_translation_list[i]
"""
Loop over all translations without TM entries, find matching TM entries and assign translations to them
"""
translations_not_in_tm = Translation.objects.filter(approved=True, memory_entries__isnull=True)
translations_not_in_tm_pks = list(translations_not_in_tm.values_list("pk"))
translations_not_in_tm_values = list(translations_not_in_tm.values_list("entity", "locale", "entity__string", "string"))
for i, value in enumerate(translations_not_in_tm_values):
try:
tm_entry = tm_entries_dict[unicode(value[0]) + "\x04" + unicode(value[1]) + "\x04" + unicode(value[2]) + "\x04" + unicode(value[3])]
tm_entry.translation__pk = translations_not_in_tm_pks[i][0]
tm_entries_to_update.append(tm_entry)
except KeyError:
pass
bulk_update(tm_entries_to_update, update_fields=['translation'], batch_size=10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment