Skip to content

Commit c9285ff

Browse files
authored
Merge pull request #4825 from kobotoolbox/fix-nlp-translation-editing
Fix NLP translation editing
2 parents b38e778 + 3330804 commit c9285ff

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

kobo/apps/subsequences/actions/translation.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,17 @@ def load_params(self, params):
3737

3838
def has_change(self, orecord, erecord):
3939
for language in self.languages:
40-
olang = orecord.get(language, False)
41-
elang = erecord.get(language, False)
42-
if olang is False or elang is False:
40+
olang = orecord.get(language, {})
41+
elang = erecord.get(language, {})
42+
if not elang:
43+
# This language is neither being edited nor deleted (deletion
44+
# would send an "edit" with value ⌫ , aka `BaseAction.DELETE`)
45+
continue
46+
if not olang:
47+
# A new language is always a change
4348
return True
44-
if self.record_repr(olang) != self.record_repr(elang):
49+
if olang.get('value') != elang.get('value'):
50+
# An existing language has translation text that has changed
4551
return True
4652
return False
4753

@@ -180,8 +186,21 @@ def engines(self):
180186
yield (manual_name, manual_engine)
181187

182188
def record_repr(self, record):
189+
# TODO: Make sure this method is sensible. Some places, e.g.
190+
# `BaseAction.is_auto_request()`, expect this method to return a
191+
# single string; however, multiple translations cannot be represented
192+
# adequately this way.
193+
# Cope with this by returning a single string if there is only one
194+
# translation, matching the previous behavior. If there is more than
195+
# one translation, return a dictionary of
196+
# `{'lang1': 'translation1','lang2': 'translation2', …}`.
197+
183198
if len(record.keys()) == 1:
184199
return [*record.values()][0].get('value')
200+
return {
201+
lang: lang_record.get('value')
202+
for lang, lang_record in record.items()
203+
}
185204

186205
def auto_request_repr(self, erecord):
187206
lang_code = [*erecord.values()][0]['languageCode']

kobo/apps/subsequences/tests/test_submission_extras_api_post.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_translation_revisions_stored_properly(self):
8484
summ = tx_instance.compile_revised_record({}, edits=first_post)
8585
assert summ['q1']['translation']['tx1']['value'] == 'VAL1'
8686
assert len(summ['q1']['translation']['tx1']['revisions']) == 0
87-
summ1 = deepcopy(summ)
87+
8888
second_post = {
8989
'q1': {
9090
'translation': {
@@ -94,6 +94,14 @@ def test_translation_revisions_stored_properly(self):
9494
}
9595
}
9696
}
97+
summ1 = tx_instance.compile_revised_record(
98+
deepcopy(summ), edits=second_post
99+
)
100+
assert summ1['q1']['translation']['tx1']['value'] == 'VAL2'
101+
assert len(summ1['q1']['translation']['tx1']['revisions']) == 1
102+
assert (
103+
summ1['q1']['translation']['tx1']['revisions'][0]['value'] == 'VAL1'
104+
)
97105

98106
def test_transx_requires_change_asset_permission(self):
99107
"""

0 commit comments

Comments
 (0)