Skip to content

Commit

Permalink
Merge pull request #1366 from Signbank/gloss_update_api
Browse files Browse the repository at this point in the history
#1360: Fixed check on whether senses are being updated by API.
  • Loading branch information
susanodd authored Nov 1, 2024
2 parents da9f135 + 535b3ae commit b1ab12b
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions signbank/gloss_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ def check_fields_can_be_updated(value_dict, dataset, language_code):
for field in value_dict.keys():
if field not in api_fields_2024 and field not in language_fields:
errors[field] = _("Field update not available")
if field == "Senses":
new_senses, formatting_error_senses = convert_string_to_dict_of_list_of_lists(value_dict[field])
if formatting_error_senses:
errors[field] = formatting_error_senses

return errors


Expand Down Expand Up @@ -306,6 +311,8 @@ def detect_type_related_problems_for_gloss_update(changes, dataset, language_cod
continue
if field in language_fields:
continue
if field == "Senses":
continue
if isinstance(field, FieldChoiceForeignKey):
field_choice_category = field.field_choice_category
try:
Expand Down Expand Up @@ -493,6 +500,23 @@ def gloss_update_do_changes(user, gloss, changes, language_code):
revision.save()


def get_original_senses_value(gloss):
senses_dict = dict()
for language in gloss.dataset.translation_languages.all():
senses_dict[language.language_code_2char] = []
for sense in gloss.senses.all():
for language in gloss.dataset.translation_languages.all():
if sense.senseTranslations.filter(language=language).exists():
sensetranslation = sense.senseTranslations.get(language=language)
translations = sensetranslation.translations.all().order_by('index')
if translations:
keywords_list = [str(trans.translation.text) for trans in translations if
trans.translation.text != '']
if keywords_list:
senses_dict[language.language_code_2char].append(keywords_list)
return senses_dict


def gloss_update(gloss, update_fields_dict, language_code):

dataset = gloss.lemma.dataset
Expand All @@ -503,9 +527,7 @@ def gloss_update(gloss, update_fields_dict, language_code):
for language_field in language_fields:
gloss_dict_language_field = human_readable_to_json[language_field]
combined_fields.append(gloss_dict_language_field)

gloss_data_dict = gloss.get_fields_dict(combined_fields, language_code)

fields_to_update = dict()
for human_readable_field, new_field_value in update_fields_dict.items():
if not new_field_value:
Expand All @@ -516,6 +538,15 @@ def gloss_update(gloss, update_fields_dict, language_code):
gloss_field = human_readable_to_internal[human_readable_field]
fields_to_update[gloss_field] = (original_value, new_field_value)
continue
if human_readable_field == 'Senses':
original_value = get_original_senses_value(gloss)
new_senses, errors = convert_string_to_dict_of_list_of_lists(new_field_value)
if errors:
# already checked at a previous step
continue
if original_value != new_senses:
fields_to_update[human_readable_field] = (original_value, new_senses)
continue
elif human_readable_field not in gloss_data_dict.keys():
# new value
original_value = ''
Expand Down Expand Up @@ -618,13 +649,6 @@ def api_update_gloss(request, datasetid, glossid):
results['updatestatus'] = "Failed"
return JsonResponse(results, safe=False)

if 'Senses' in value_dict:
_, errors = convert_string_to_dict_of_list_of_lists(value_dict['Senses'])
if errors:
results['errors'] = [errors]
results['updatestatus'] = "Failed"
return JsonResponse(results, safe=False)

gloss_update_do_changes(request.user, gloss, fields_to_update, interface_language_code)

results['errors'] = {}
Expand Down

0 comments on commit b1ab12b

Please sign in to comment.