diff --git a/inspirehep/config.py b/inspirehep/config.py index 31be3b1a77..98325fd648 100644 --- a/inspirehep/config.py +++ b/inspirehep/config.py @@ -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=( diff --git a/inspirehep/modules/records/mappings/records/hep.json b/inspirehep/modules/records/mappings/records/hep.json index be945bc74b..f19dd8d6ea 100644 --- a/inspirehep/modules/records/mappings/records/hep.json +++ b/inspirehep/modules/records/mappings/records/hep.json @@ -969,6 +969,10 @@ }, "type": "object" }, + "report_number_suggest": { + "payloads": true, + "type": "completion" + }, "self": { "properties": { "$ref": { diff --git a/inspirehep/modules/records/receivers.py b/inspirehep/modules/records/receivers.py index 49b0ce8eb0..21b30334d0 100644 --- a/inspirehep/modules/records/receivers.py +++ b/inspirehep/modules/records/receivers.py @@ -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) @@ -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, }, }, @@ -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, } }, }) @@ -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) @@ -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, } }, }) @@ -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'): @@ -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', []) @@ -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, }, }, }) diff --git a/tests/unit/records/test_records_receivers.py b/tests/unit/records/test_records_receivers.py index f796ae852a..8f398d6082 100644 --- a/tests/unit/records/test_records_receivers.py +++ b/tests/unit/records/test_records_receivers.py @@ -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, ) @@ -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'] @@ -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'] @@ -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'] @@ -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']