Skip to content

Commit

Permalink
Merge pull request #1249 from Signbank/api_interface
Browse files Browse the repository at this point in the history
API update lemma and annotation fields
  • Loading branch information
susanodd authored May 29, 2024
2 parents 6d2a5cf + a677b94 commit 6457103
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 41 deletions.
1 change: 0 additions & 1 deletion signbank/abstract_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def create_gloss_columns_to_value_dict_keys(dataset, language_code):
required_columns = dict()
required_columns['Dataset'] = 'dataset'
dataset_languages = dataset.translation_languages.all()
lang_attr_name = 'name_' + DEFAULT_KEYWORDS_LANGUAGE['language_code_2char']
for language in dataset_languages:
required_columns[gettext("Annotation ID Gloss") + " (" + language.name + ")"] = (
'annotation_id_gloss_' + getattr(language, 'language_code_2char'))
Expand Down
50 changes: 42 additions & 8 deletions signbank/api_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from signbank.dictionary.forms import *
from django.utils.translation import override, gettext_lazy as _, activate
from signbank.settings.server_specific import LANGUAGES, LEFT_DOUBLE_QUOTE_PATTERNS, RIGHT_DOUBLE_QUOTE_PATTERNS
from signbank.api_token import hash_token
from signbank.abstract_machine import get_interface_language_api

from django.core.exceptions import ObjectDoesNotExist

Expand Down Expand Up @@ -80,8 +82,24 @@ def api_fields(dataset, language_code='en', advanced=False):

def get_fields_data_json(request, datasetid):

from signbank.abstract_machine import get_interface_language_api
interface_language_code = get_interface_language_api(request, request.user)
results = dict()
auth_token_request = request.headers.get('Authorization', '')
interface_language_code = request.headers.get('Accept-Language', 'en')
if interface_language_code not in settings.MODELTRANSLATION_LANGUAGES:
interface_language_code = 'en'
activate(interface_language_code)
if auth_token_request:
auth_token = auth_token_request.split('Bearer ')[-1]
hashed_token = hash_token(auth_token)
signbank_token = SignbankAPIToken.objects.filter(api_token=hashed_token).first()
if not signbank_token:
results['errors'] = [gettext("Your Authorization Token does not match anything.")]
return JsonResponse(results)
username = signbank_token.signbank_user.username
user = User.objects.get(username=username)
else:
user = request.user
interface_language_code = get_interface_language_api(request, user)

sequence_of_digits = True
for i in datasetid:
Expand All @@ -98,7 +116,7 @@ def get_fields_data_json(request, datasetid):
# ignore the database in the url if necessary
dataset = Dataset.objects.get(id=settings.DEFAULT_DATASET_PK)

if request.user.has_perm('dictionary.change_gloss'):
if user.has_perm('dictionary.change_gloss'):
api_fields_2023 = api_fields(dataset, interface_language_code, advanced=True)
else:
api_fields_2023 = api_fields(dataset, interface_language_code, advanced=False)
Expand All @@ -110,8 +128,24 @@ def get_fields_data_json(request, datasetid):

def get_gloss_data_json(request, datasetid, glossid):

from signbank.abstract_machine import get_interface_language_api
interface_language_code = get_interface_language_api(request, request.user)
results = dict()
auth_token_request = request.headers.get('Authorization', '')
interface_language_code = request.headers.get('Accept-Language', 'en')
if interface_language_code not in settings.MODELTRANSLATION_LANGUAGES:
interface_language_code = 'en'
activate(interface_language_code)
if auth_token_request:
auth_token = auth_token_request.split('Bearer ')[-1]
hashed_token = hash_token(auth_token)
signbank_token = SignbankAPIToken.objects.filter(api_token=hashed_token).first()
if not signbank_token:
results['errors'] = [gettext("Your Authorization Token does not match anything.")]
return JsonResponse(results)
username = signbank_token.signbank_user.username
user = User.objects.get(username=username)
else:
user = request.user
interface_language_code = get_interface_language_api(request, user)

sequence_of_digits = True
for i in datasetid:
Expand All @@ -124,8 +158,8 @@ def get_gloss_data_json(request, datasetid, glossid):

dataset_id = int(datasetid)
dataset = Dataset.objects.filter(id=dataset_id).first()
if not dataset or not request.user.is_authenticated:
# ignore the database in the url if necessary
if not dataset or not user.is_authenticated:
# ignore the dataset in the url if necessary
dataset = Dataset.objects.get(id=settings.DEFAULT_DATASET_PK)

sequence_of_digits = True
Expand All @@ -143,7 +177,7 @@ def get_gloss_data_json(request, datasetid, glossid):
if not gloss:
return JsonResponse({})

if request.user.has_perm('dictionary.change_gloss'):
if user.has_perm('dictionary.change_gloss'):
api_fields_2023 = api_fields(dataset, interface_language_code, advanced=True)
else:
api_fields_2023 = api_fields(dataset, interface_language_code, advanced=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
var glossid = '{{gloss.id}}';
var csrf_token = '{{csrf_token}}';
var gloss_fields = {{gloss_fields|safe}};
var language_2chars = {{language_2chars|safe}};
</script>

<script type="text/javascript">
Expand Down Expand Up @@ -49,6 +50,23 @@
}
function update_gloss() {
var update = { };
for (var i=0; i < language_2chars.length; i++) {
var lang2char = language_2chars[i];
var lemma_field = 'lemma_id_gloss_' + lang2char;
var lemma_field_lookup = '#'+lemma_field;
var lemma_field_key = $(lemma_field_lookup).attr("name");
var lemma_field_value = $(lemma_field_lookup).val();
if (lemma_field_value != '') {
update[lemma_field_key] = lemma_field_value;
}
var annotation_field = 'annotation_id_gloss_' + lang2char;
var annotation_field_lookup = '#'+annotation_field;
var annotation_field_key = $(annotation_field_lookup).attr("name");
var annotation_field_value = $(annotation_field_lookup).val();
if (annotation_field_value != '') {
update[annotation_field_key] = annotation_field_value;
}
}
for (var field in gloss_fields) {
var field_lookup = '#field_input_'+field;
var field_key = $(field_lookup).attr("name");
Expand All @@ -58,11 +76,13 @@
}
update[field_key] = field_value;
}
var update_json = JSON.stringify(update);
console.log(update_json);
$.ajax({
url : url + "/dictionary/api_update_gloss/" + datasetid + '/' + glossid + '/',
type: 'POST',
headers: { 'Accept-Language': language_code },
data: JSON.stringify(update),
data: update_json,
datatype: "json",
async: false,
success : show_update_results
Expand Down Expand Up @@ -99,6 +119,32 @@ <h3>{% trans "Gloss Fields" %}</h3>
<p>{% trans "It makes use of the csrf token in the javascript AJAX setup." %}</p>
<div>
<table class='table' style='width: 1200px;'>
{% for dataset_lang in dataset_languages %}
<tr>
<th id="add_gloss_lemma_header_{{dataset_lang.language_code_2char}}" style="width:200px;">
{% trans "Lemma ID Gloss" %} ({{ dataset_lang.name }})
</th>
<td id="add_gloss_lemma_value_{{dataset_lang.language_code_2char}}">
<input id="lemma_id_gloss_{{dataset_lang.language_code_2char}}"
name='{% trans "Lemma ID Gloss" %} ({{ dataset_lang.name }})'
maxlength="30" type="text"
size="50" >
</td>
</tr>
{% endfor %}
{% for dataset_lang in dataset_languages %}
<tr>
<th id="add_gloss_annotation_header_{{dataset_lang.language_code_2char}}" style="width:300px;">
{% trans "Annotation ID Gloss" %} ({{ dataset_lang.name }})
</th>
<td id="add_gloss_annotation_value_{{dataset_lang.language_code_2char}}">
<input id="annotation_id_gloss_{{dataset_lang.language_code_2char}}"
name='{% trans "Annotation ID Gloss" %} ({{ dataset_lang.name }})'
maxlength="30" type="text"
size="50" >
</td>
</tr>
{% endfor %}
{% for field, field_verbose in gloss_fields.items %}
<tr>
<th id="field_label_{{field}}">
Expand Down
9 changes: 6 additions & 3 deletions signbank/dictionary/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2599,7 +2599,7 @@ def gloss_revision_history(request,gloss_pk):
else:
# this shouldn't happen
field_name_qualification = ''
elif revision.field_name == 'Sense':
elif revision.field_name in ['Sense', 'Senses', 'senses']:
if revision.old_value and not revision.new_value:
# this translation exists in the interface of Gloss Edit View
delete_command = str(_('Delete'))
Expand Down Expand Up @@ -3099,12 +3099,15 @@ def test_am_update_gloss(request, datasetid, glossid):

activate(interface_language_code)
fieldnames = FIELDS['main'] + FIELDS['phonology'] + FIELDS['semantics'] + ['inWeb', 'isNew', 'excludeFromEcv', 'senses']
gloss_fields = { fname: Gloss.get_field(fname).verbose_name.title()
for fname in fieldnames if fname in Gloss.get_field_names() }
gloss_fields = {fname: Gloss.get_field(fname).verbose_name.title()
for fname in fieldnames if fname in Gloss.get_field_names()}

language_2chars = [str(language.language_code_2char) for language in dataset.translation_languages.all()]

return render(request, 'dictionary/virtual_machine_gloss_update_api.html',
{'selected_datasets': selected_datasets,
'dataset': dataset,
'language_2chars': language_2chars,
'gloss': gloss,
'gloss_fields': gloss_fields,
'dataset_languages': dataset_languages,
Expand Down
Loading

0 comments on commit 6457103

Please sign in to comment.