Skip to content

Commit

Permalink
Remove and replace affautocomplete field with affiliation_suggest
Browse files Browse the repository at this point in the history
Signed-off-by: Iuliana Voinea <[email protected]>
  • Loading branch information
Iuliana Voinea committed Dec 11, 2017
1 parent b941038 commit e7d7b33
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 44 deletions.
30 changes: 17 additions & 13 deletions inspirehep/modules/forms/static/js/forms/affiliations_typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ define([
this.dataEngine = new Bloodhound({
name: 'affiliations',
remote: {
url: '/api/institutions?q=affautocomplete:%QUERY*',
url: '/api/institutions/_suggest?affiliation:%QUERY',
filter: function(response) {
return $.map(response.hits.hits, function(el) { return el });
return $.map(response.affiliation[0].options, function(el) { return el.payload });
}
},
datumTokenizer: function() {},
Expand All @@ -47,9 +47,10 @@ define([
var suggestionTemplate = Hogan.compile(
'<strong>{{ legacy_ICN }}</strong><br>' +
'<small>' +
'{{#ICN}}{{#show_future_name}}Alternative name: {{future_name}}<br>{{/show_future_name}}{{/ICN}}' +
'{{#department}}{{ department }}<br>{{/department}}' +
'{{#institution}}{{ institution }}{{/institution}}' +
'{{#ICN}}{{#show_future_name}}Alternative name: {{future_name}}<br>{{/show_future_name}}{{/ICN}}' +
'{{#department}}{{ department }}<br>{{/department}}' +
'{{#institution}}{{ institution }}{{/institution}}' +
'{{#institution_name}}{{ institution_name }}{{/institution_name}}' +
'</small>'
);

Expand All @@ -68,29 +69,32 @@ define([
}.bind(this));
}.bind(this),
displayKey: function(data) {
return data.metadata.legacy_ICN;
return data.legacy_ICN;
},
templates: {
empty: function(data) {
return 'Cannot find this affiliation in our database.';
},
suggestion: function(data) {
function _getICN() {
if (!data.metadata.ICN) {
if (!data.ICN) {
return;
}
for (let i=0; i<data.metadata.ICN.length; i++) {
if (data.metadata.ICN[i] !== data.metadata.legacy_ICN) {
return data.metadata.ICN[i];
for (let i=0; i<data.ICN.length; i++) {
if (data.ICN[i] !== data.legacy_ICN) {
return data.ICN[i];
}
}
}
let ICN = _getICN();
if (ICN) {
data.metadata.show_future_name = true;
data.metadata.future_name = ICN;
data.show_future_name = true;
data.future_name = ICN;
}
return suggestionTemplate.render.call(suggestionTemplate, data.metadata);
if (data.institution_names && data.institution_names.length > 0) {
data.institution_name = data.institution_names[0];
}
return suggestionTemplate.render.call(suggestionTemplate, data);
}.bind(this)
}
});
Expand Down
10 changes: 1 addition & 9 deletions inspirehep/modules/records/mappings/records/institutions.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"type": "string"
},
"ICN": {
"copy_to": "affautocomplete",
"type": "string"
},
"_collections": {
Expand Down Expand Up @@ -58,9 +57,6 @@
},
"type": "object"
},
"affautocomplete": {
"type": "string"
},
"affiliation_suggest": {
"payloads": true,
"type": "completion"
Expand Down Expand Up @@ -119,11 +115,9 @@
"institution_hierarchy": {
"properties": {
"acronym": {
"copy_to": "affautocomplete",
"type": "string"
},
"name": {
"copy_to": "affautocomplete",
"name": {,
"type": "string"
}
},
Expand All @@ -133,7 +127,6 @@
"type": "string"
},
"legacy_ICN": {
"copy_to": "affautocomplete",
"type": "string"
},
"legacy_creation_date": {
Expand All @@ -146,7 +139,6 @@
"type": "string"
},
"value": {
"copy_to": "affautocomplete",
"type": "string"
}
},
Expand Down
44 changes: 22 additions & 22 deletions tests/unit/records/test_records_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
assign_uuid,
populate_abstract_source_suggest,
populate_affiliation_suggest,
populate_bookautocomplete,
populate_book_suggest,
populate_book_series_suggest,
populate_collaboration_suggest,
populate_conference_suggest,
Expand All @@ -46,7 +46,7 @@
)


def test_populate_bookautocomplete_from_authors():
def test_populate_book_suggest_from_authors():
schema = load_schema('hep')
authors_schema = schema['properties']['authors']
document_type_schema = schema['properties']['document_type']
Expand All @@ -68,7 +68,7 @@ def test_populate_bookautocomplete_from_authors():
assert validate(record['document_type'], document_type_schema) is None
assert validate(record['self'], self_schema) is None

populate_bookautocomplete(None, record)
populate_book_suggest(None, record)

expected = {
'input': [
Expand All @@ -82,12 +82,12 @@ def test_populate_bookautocomplete_from_authors():
'title': [],
},
}
result = record['bookautocomplete']
result = record['book_suggest']

assert expected == result


def test_populate_bookautocomplete_from_titles():
def test_populate_book_suggest_from_titles():
schema = load_schema('hep')
document_type_schema = schema['properties']['document_type']
self_schema = schema['properties']['self']
Expand All @@ -109,7 +109,7 @@ def test_populate_bookautocomplete_from_titles():
assert validate(record['self'], self_schema) is None
assert validate(record['titles'], titles_schema) is None

populate_bookautocomplete(None, record)
populate_book_suggest(None, record)

expected = {
'input': [
Expand All @@ -123,12 +123,12 @@ def test_populate_bookautocomplete_from_titles():
],
},
}
result = record['bookautocomplete']
result = record['book_suggest']

assert expected == result


def test_populate_bookautocomplete_from_imprints_dates():
def test_populate_book_suggest_from_imprints_dates():
schema = load_schema('hep')
document_type_schema = schema['properties']['document_type']
self_schema = schema['properties']['self']
Expand All @@ -150,7 +150,7 @@ def test_populate_bookautocomplete_from_imprints_dates():
assert validate(record['imprints'], imprints_schema) is None
assert validate(record['self'], self_schema) is None

populate_bookautocomplete(None, record)
populate_book_suggest(None, record)

expected = {
'input': [
Expand All @@ -162,12 +162,12 @@ def test_populate_bookautocomplete_from_imprints_dates():
'title': [],
},
}
result = record['bookautocomplete']
result = record['book_suggest']

assert expected == result


def test_populate_bookautocomplete_from_imprints_publishers():
def test_populate_book_suggest_from_imprints_publishers():
schema = load_schema('hep')
document_type_schema = schema['properties']['document_type']
self_schema = schema['properties']['self']
Expand All @@ -189,7 +189,7 @@ def test_populate_bookautocomplete_from_imprints_publishers():
assert validate(record['imprints'], imprints_schema) is None
assert validate(record['self'], self_schema) is None

populate_bookautocomplete(None, record)
populate_book_suggest(None, record)

expected = {
'input': [
Expand All @@ -201,12 +201,12 @@ def test_populate_bookautocomplete_from_imprints_publishers():
'title': [],
},
}
result = record['bookautocomplete']
result = record['book_suggest']

assert expected == result


def test_populate_bookautocomplete_from_isbns_values():
def test_populate_book_suggest_from_isbns_values():
schema = load_schema('hep')
document_type_schema = schema['properties']['document_type']
self_schema = schema['properties']['self']
Expand All @@ -228,7 +228,7 @@ def test_populate_bookautocomplete_from_isbns_values():
assert validate(record['isbns'], isbns_schema) is None
assert validate(record['self'], self_schema) is None

populate_bookautocomplete(None, record)
populate_book_suggest(None, record)

expected = {
'input': [
Expand All @@ -240,20 +240,20 @@ def test_populate_bookautocomplete_from_isbns_values():
'title': [],
},
}
result = record['bookautocomplete']
result = record['book_suggest']

assert expected == result


def test_populate_bookautocomplete_does_nothing_if_record_is_not_literature():
def test_populate_book_suggest_does_nothing_if_record_is_not_literature():
record = {'$schema': 'http://localhost:5000/schemas/records/other.json'}

populate_bookautocomplete(None, record)
populate_book_suggest(None, record)

assert 'bookautocomplete' not in record
assert 'book_suggest' not in record


def test_populate_bookautocomplete_does_nothing_if_record_is_not_a_book():
def test_populate_book_suggest_does_nothing_if_record_is_not_a_book():
schema = load_schema('hep')
authors_schema = schema['properties']['authors']
document_type_schema = schema['properties']['document_type']
Expand All @@ -275,9 +275,9 @@ def test_populate_bookautocomplete_does_nothing_if_record_is_not_a_book():
assert validate(record['document_type'], document_type_schema) is None
assert validate(record['self'], self_schema) is None

populate_bookautocomplete(None, record)
populate_book_suggest(None, record)

assert 'bookautocomplete' not in record
assert 'book_suggest' not in record


def test_populate_book_series_suggest():
Expand Down

0 comments on commit e7d7b33

Please sign in to comment.