Skip to content

Commit

Permalink
A first implementation of report_number_suggest
Browse files Browse the repository at this point in the history
So far, even if a record has several report numbers associated with it,
the output will contain only the first one.

Signed-off-by: Iuliana Voinea <[email protected]>
  • Loading branch information
Iuliana Voinea committed Dec 8, 2017
1 parent 283a369 commit e9fdb00
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 27 deletions.
5 changes: 4 additions & 1 deletion inspirehep/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@
)),
collaboration_name=dict(completion=dict(
field='collaborations.collaboration_suggest'
))
)),
report_number=dict(completion=dict(
field='report_number_suggest'
)),
),
list_route='/literature/',
item_route=(
Expand Down
4 changes: 4 additions & 0 deletions inspirehep/modules/records/mappings/records/hep.json
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,10 @@
},
"type": "object"
},
"report_number_suggest": {
"payloads": true,
"type": "completion"
},
"self": {
"properties": {
"$ref": {
Expand Down
43 changes: 32 additions & 11 deletions inspirehep/modules/records/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def enhance_after_index(sender, json, *args, **kwargs):
populate_collaboration_suggest(sender, json, *args, **kwargs)
populate_conference_suggest(sender, json, *args, **kwargs)
populate_experiment_suggest(sender, json, *args, **kwargs)
populate_report_number_suggest(sender, json, *args, **kwargs)
populate_abstract_source_suggest(sender, json, *args, **kwargs)
populate_affiliation_suggest(sender, json, *args, **kwargs)
populate_author_count(sender, json, *args, **kwargs)
Expand Down Expand Up @@ -174,14 +175,14 @@ def populate_bookautocomplete(sender, json, *args, **kwargs):
input_values.extend(titles)
input_values = [el for el in input_values if el]

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

json.update({
'bookautocomplete': {
'input': input_values,
'payload': {
'authors': authors,
'id': record if record else '',
'id': record,
'title': titles,
},
},
Expand Down Expand Up @@ -234,14 +235,14 @@ def populate_conference_suggest(sender, json, *args, **kwargs):
return

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

json.update({
'conference_suggest': {
'input': cnum,
'output': cnum,
'payload': {
'$ref': record if record else '',
'$ref': record,
}
},
})
Expand All @@ -255,7 +256,7 @@ def populate_experiment_suggest(sender, json, *args, **kwargs):
legacy_name = json.get('legacy_name', '')
long_name = json.get('long_name', '')
name_variants = json.get('name_variants', '')
record = get_value(json, 'self.$ref')
record = get_value(json, 'self.$ref', '')

input_values = []
input_values.append(legacy_name)
Expand All @@ -268,7 +269,7 @@ def populate_experiment_suggest(sender, json, *args, **kwargs):
'input': input_values,
'output': legacy_name,
'payload': {
'$ref': record if record else '',
'$ref': record,
}
},
})
Expand Down Expand Up @@ -363,6 +364,26 @@ def _recursive_find_refs(json_root):
_recursive_find_refs(json)


def populate_report_number_suggest(sender, json, *args, **kwargs):
"""Populate the ``report_number_suggest`` field of Literature records."""
if 'hep.json' not in json.get('$schema'):
return

report_numbers = get_value(json, 'report_numbers.value', [])

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

json.update({
'report_number_suggest': {
'input': report_numbers,
'output': report_numbers[0] if report_numbers else '',
'payload': {
'$ref': record,
}
},
})


def populate_abstract_source_suggest(sender, json, *args, **kwargs):
"""Populate the ``abstract_source_suggest`` field in Literature records."""
if 'hep.json' not in json.get('$schema'):
Expand All @@ -386,7 +407,7 @@ def populate_title_suggest(sender, json, *args, **kwargs):
if 'journals.json' not in json.get('$schema'):
return

journal_title = get_value(json, 'journal_title.title', default='')
journal_title = get_value(json, 'journal_title.title', '')
short_title = json.get('short_title', '')
title_variants = json.get('title_variants', [])

Expand All @@ -396,15 +417,15 @@ def populate_title_suggest(sender, json, *args, **kwargs):
input_values.extend(title_variants)
input_values = [el for el in input_values if el]

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

json.update({
'title_suggest': {
'input': input_values,
'output': short_title if short_title else '',
'output': short_title,
'payload': {
'full_title': journal_title if journal_title else '',
'$ref': record if record else '',
'full_title': journal_title,
'$ref': record,
},
},
})
Expand Down
96 changes: 81 additions & 15 deletions tests/unit/records/test_records_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
populate_experiment_suggest,
populate_inspire_document_type,
populate_recid_from_ref,
populate_report_number_suggest,
populate_title_suggest,
populate_author_count,
)
Expand Down Expand Up @@ -488,11 +489,11 @@ def test_populate_experiment_suggest_from_legacy_name():
populate_experiment_suggest(None, record)

expected = {
'input': ['foo'],
'output': 'foo',
'payload': {
'$ref': 'http://localhost:5000/api/experiments/bar'
},
'input': ['foo'],
'output': 'foo',
'payload': {
'$ref': 'http://localhost:5000/api/experiments/bar'
},
}

result = record['experiment_suggest']
Expand All @@ -517,11 +518,11 @@ def test_populate_experiment_suggest_from_legacy_name_from_long_name():
populate_experiment_suggest(None, record)

expected = {
'input': ['foo', 'bar'],
'output': 'foo',
'payload': {
'$ref': 'http://localhost:5000/api/experiments/bar'
},
'input': ['foo', 'bar'],
'output': 'foo',
'payload': {
'$ref': 'http://localhost:5000/api/experiments/bar'
},
}

result = record['experiment_suggest']
Expand Down Expand Up @@ -549,11 +550,11 @@ def test_populate_experiment_suggest_from_legacy_name_from_name_variants():
populate_experiment_suggest(None, record)

expected = {
'input': ['foo', 'bar', 'baz'],
'output': 'foo',
'payload': {
'$ref': 'http://localhost:5000/api/experiments/bar'
},
'input': ['foo', 'bar', 'baz'],
'output': 'foo',
'payload': {
'$ref': 'http://localhost:5000/api/experiments/bar'
},
}

result = record['experiment_suggest']
Expand Down Expand Up @@ -978,6 +979,71 @@ def test_populate_recid_from_ref_handles_deleted_records():
assert json_dict['deleted_recids'] == [1, 2]


def test_populate_report_number_suggest():
schema = load_schema('hep')
subschema = schema['properties']['report_numbers']

record = {
'$schema': 'http://localhost:5000/schemas/records/hep.json',
'report_numbers': [
{
'value': 'foo',
},
{
'value': 'bar',
},
{
'value': 'baz',
},
],
'self': {
'$ref': 'http://localhost:5000/api/literature/bar',
},
}
assert validate(record['report_numbers'], subschema) is None

populate_report_number_suggest(None, record)

expected = {
'input': ['foo', 'bar', 'baz'],
'output': 'foo',
'payload': {
'$ref': 'http://localhost:5000/api/literature/bar'
},
}
result = record['report_number_suggest']

assert expected == result


def test_populate_report_number_suggest_does_nothing_if_record_is_not_literature():
schema = load_schema('hep')
subschema = schema['properties']['report_numbers']

record = {
'$schema': 'http://localhost:5000/schemas/records/other.json',
'report_numbers': [
{
'value': 'foo',
},
{
'value': 'bar',
},
{
'value': 'baz',
},
],
'self': {
'$ref': 'http://localhost:5000/api/literature/bar',
},
}
assert validate(record['report_numbers'], subschema) is None

populate_report_number_suggest(None, record)

assert 'report_number_suggest' not in record


def test_populate_abstract_source_suggest():
schema = load_schema('hep')
subschema = schema['properties']['abstracts']
Expand Down

0 comments on commit e9fdb00

Please sign in to comment.