Skip to content

Commit

Permalink
Merge branch 'fix-experiments-autocomplete' into implement-autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
Iuliana Voinea committed Dec 11, 2017
2 parents 32460aa + 7203878 commit 6b5853e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ define([
this.dataEngine = new Bloodhound({
name: 'experiments',
remote: {
url: '/api/experiments?q=experimentautocomplete:%QUERY*',
url: '/api/experiments/_suggest?experiment=%QUERY',
filter: function(response) {
return response.hits.hits.sort(function (x, y) {
var x_title = x.metadata.legacy_name,
y_title = y.metadata.legacy_name;
return response.experiment[0].options.sort(function (x, y) {
var x_title = x.text,
y_title = y.text;

return x_title.localeCompare(y_title);
});
Expand All @@ -49,7 +49,7 @@ define([
this.$element = $element;

var suggestionTemplate = Hogan.compile(
'<strong>{{ display_name }}</strong><br>'
'<strong>{{ text }}</strong><br>'
);

this.$element.typeahead({
Expand All @@ -67,15 +67,15 @@ define([
}.bind(this));
}.bind(this),
displayKey: function(data) {
return data.metadata.legacy_name;
return data.text;
},
templates: {
empty: function(data) {
return 'Cannot find this experiment in our database.';
},
suggestion: function(data) {
data.metadata.display_name = data.metadata.legacy_name;
return suggestionTemplate.render.call(suggestionTemplate, data.metadata);

return suggestionTemplate.render.call(suggestionTemplate, data);
}.bind(this)
}
});
Expand Down
6 changes: 3 additions & 3 deletions inspirehep/modules/records/mappings/records/experiments.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@
},
"type": "object"
},
"experimentautocomplete": {
"type": "string"
"experiment_suggest": {
"payloads": true,
"type": "completion"
},
"experiment_suggest": {
"payloads": true,
Expand Down Expand Up @@ -188,7 +189,6 @@
"type": "date"
},
"legacy_name": {
"copy_to": "experimentautocomplete",
"type": "string"
},
"long_name": {
Expand Down
37 changes: 37 additions & 0 deletions inspirehep/modules/records/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def enhance_after_index(sender, json, *args, **kwargs):
populate_affiliation_suggest(sender, json, *args, **kwargs)
populate_author_count(sender, json, *args, **kwargs)
populate_earliest_date(sender, json, *args, **kwargs)
populate_experiment_suggest(sender, json, *args, **kwargs)
populate_inspire_document_type(sender, json, *args, **kwargs)
populate_name_variations(sender, json, *args, **kwargs)
populate_title_suggest(sender, json, *args, **kwargs)
Expand Down Expand Up @@ -508,6 +509,42 @@ def populate_earliest_date(sender, json, *args, **kwargs):
json['earliest_date'] = result


def populate_experiment_suggest(sender, json, *args, **kwargs):
"""Populates experiment_suggest field of experiment records."""

# FIXME: Use a dedicated method when #1355 will be resolved.
if 'experiments.json' not in json.get('$schema'):
return

experiment_paths = [
'accelerator.value',
'collaboration.value',
'experiment.short_name',
'experiment.value',
'institutions.value',
'legacy_name',
'long_name',
'name_variants',
]

input_values = [el for el in chain.from_iterable(
[force_list(get_value(json, path)) for path in experiment_paths]) if el]

legacy_name = json.get('legacy_name', '')

record = get_value(json, 'self.$ref', '')

json.update({
'experiment_suggest': {
'input': input_values,
'output': legacy_name,
'payload': {
'$ref': record,
},
},
})


def populate_name_variations(sender, json, *args, **kwargs):
"""Generate name variations for each signature of a Literature record."""
if 'hep.json' not in json.get('$schema'):
Expand Down
79 changes: 79 additions & 0 deletions tests/unit/records/test_records_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,85 @@ def test_populate_earliest_date_from_preprint_date():
assert expected == result


def test_populate_experiment_suggest():
schema = load_schema('experiments')
legacy_name_schema = schema['properties']['legacy_name']
long_name_schema = schema['properties']['long_name']
name_variants_schema = schema['properties']['name_variants']
collaboration_schema = schema['properties']['collaboration']
accelerator_schema = schema['properties']['accelerator']
experiment_schema = schema['properties']['experiment']
institutions_schema = schema['properties']['institutions']

record = {
'$schema': 'http://foo/experiments.json',
'self': {'$ref': 'https://localhost:5000/api/experiments/bar'},
'legacy_name': 'foo',
'long_name': 'foobarbaz',
'name_variants': [
'bar',
'baz',
],
'collaboration': {
'value': 'D0',
},
'accelerator': {
'value': 'LHC',
},
'experiment': {
'short_name': 'SHINE',
'value': 'NA61',
},
'institutions': [
{
'value': 'ICN',
},
],
}

assert validate(record['legacy_name'], legacy_name_schema) is None
assert validate(record['long_name'], long_name_schema) is None
assert validate(record['name_variants'], name_variants_schema) is None
assert validate(record['collaboration'], collaboration_schema) is None
assert validate(record['accelerator'], accelerator_schema) is None
assert validate(record['institutions'], institutions_schema) is None
assert validate(record['experiment'], experiment_schema) is None

populate_experiment_suggest(None, record)

expected = {
'input': [
'LHC',
'D0',
'SHINE',
'NA61',
'ICN',
'foo',
'foobarbaz',
'bar',
'baz',
],
'output': 'foo',
'payload': {
'$ref': 'https://localhost:5000/api/experiments/bar',
}
}

result = record['experiment_suggest']

assert expected == result


def test_populate_experiment_suggest_does_nothing_if_record_is_not_experiment():
json_dict = {
'$schema': 'http://foo/bar.json',
}

populate_experiment_suggest(None, json_dict)

assert 'experiment_suggest' not in json_dict


def test_populate_earliest_date_from_thesis_info_date():
schema = load_schema('hep')
subschema = schema['properties']['thesis_info']
Expand Down

0 comments on commit 6b5853e

Please sign in to comment.