Skip to content

Commit

Permalink
#1219: Normalise spaces for csv for field choice operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
susanodd committed Apr 29, 2024
1 parent 974aa87 commit 6a4899c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
14 changes: 14 additions & 0 deletions signbank/csv_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,20 @@ def csv_focusgloss_to_minimalpairs(focusgloss, dataset, language_code, csv_rows)
return csv_rows


def normalize_field_choice(field_choice):
# add spaces around > and +

split_gt = field_choice.split('>')
trimmed_gt = [elt.strip() for elt in split_gt]
joined_gt = ' > '.join(trimmed_gt)

split_plus = joined_gt.split('+')
trimmed_plus = [elt.strip() for elt in split_plus]
joined_plus = ' + '.join(trimmed_plus)

return joined_plus


def choice_fields_choices():
fields_choices = dict()

Expand Down
10 changes: 8 additions & 2 deletions signbank/gloss_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.http import JsonResponse
from guardian.shortcuts import get_objects_for_user
from signbank.tools import get_interface_language_and_default_language_codes
from signbank.csv_interface import normalize_field_choice

import ast

Expand Down Expand Up @@ -229,7 +230,11 @@ def gloss_update_typecheck(changes, language_code):
try:
fieldchoice = FieldChoice.objects.get(field=field_choice_category, name__iexact=new_value)
except (ObjectDoesNotExist, MultipleObjectsReturned):
errors[field.verbose_name.title()] = gettext('NOT FOUND: ') + new_value
normalised_choice = normalize_field_choice(new_value)
try:
fieldchoice = FieldChoice.objects.get(field=field_choice_category, name__iexact=normalised_choice)
except (ObjectDoesNotExist, MultipleObjectsReturned):
errors[field.verbose_name.title()] = gettext('NOT FOUND: ') + new_value
elif isinstance(field, models.ForeignKey) and field.related_model == Handshape:
try:
handshape = Handshape.objects.get(name__iexact=new_value)
Expand Down Expand Up @@ -257,7 +262,8 @@ def gloss_update_do_changes(request, gloss, changes, language_code):
for field, (original_value, new_value) in changes.items():
if isinstance(field, FieldChoiceForeignKey):
field_choice_category = field.field_choice_category
fieldchoice = FieldChoice.objects.get(field=field_choice_category, name__iexact=new_value)
normalised_choice = normalize_field_choice(new_value)
fieldchoice = FieldChoice.objects.get(field=field_choice_category, name__iexact=normalised_choice)
setattr(gloss, field.name, fieldchoice)
changes_done.append((field.name, original_value, new_value))
elif field.__class__.__name__ == 'BooleanField':
Expand Down
17 changes: 12 additions & 5 deletions signbank/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from signbank.csv_interface import (sense_translations_for_language, update_senses_parse,
update_sentences_parse, sense_examplesentences_for_language, get_sense_numbers,
parse_sentence_row, get_senses_to_sentences, csv_sentence_tuples_list_compare,
csv_header_row_glosslist, required_csv_columns, trim_columns_in_row)
csv_header_row_glosslist, required_csv_columns, trim_columns_in_row,
normalize_field_choice)
from signbank.dictionary.models import *
from signbank.dictionary.forms import *
from django.utils.dateformat import format
Expand Down Expand Up @@ -799,11 +800,17 @@ def compare_valuedict_to_gloss(valuedict, gloss_id, my_datasets, nl,
field_choice = FieldChoice.objects.get(name__iexact=new_human_value, field=field.field_choice_category)
new_machine_value = field_choice.machine_value
except ObjectDoesNotExist:
error_string = 'For ' + default_annotationidglosstranslation + ' (' + str(
gloss_id) + '), could not find option ' + new_human_value + ' for ' + human_key
normalised_choice = normalize_field_choice(new_human_value)
try:
field_choice = FieldChoice.objects.get(name__iexact=normalised_choice,
field=field.field_choice_category)
new_machine_value = field_choice.machine_value
except ObjectDoesNotExist:
error_string = 'For ' + default_annotationidglosstranslation + ' (' + str(
gloss_id) + '), could not find option ' + new_human_value + ' for ' + human_key

errors_found += [error_string]
continue
errors_found += [error_string]
continue

elif isinstance(field, models.ForeignKey) and field.related_model == Handshape:
if new_human_value in ['', '0', ' ', None, 'None']:
Expand Down

0 comments on commit 6a4899c

Please sign in to comment.