def _parse_structures(record): structures = [] recids = [] for author in record.get('authors', []): for affiliation in author.get('affiliations', []): try: recids.append( str(get_recid_from_ref(affiliation['record'])) ) affiliation['recid'] = get_recid_from_ref( affiliation['record'] ) except KeyError: continue try: records = get_es_records('ins', recids) except RequestError: records = [] for record in records: structures.append( _structure_data(record) ) return dedupe_list(structures)
def test_get_es_records_finds_right_results(app): literature = [1498175, 1090628] authors = [983059] results = get_es_records('lit', literature + authors) recids = {result['control_number'] for result in results} assert len(results) == len(literature) assert recids == set(literature)
def references(self): """Reference export for single record in datatables format. :returns: list List of lists where every item represents a datatables row. A row consists of [reference_number, reference, num_citations] """ out = [] references = self.record.get('references') if references: reference_recids = [ str(ref['recid']) for ref in references if ref.get('recid') ] resolved_references = get_es_records( 'lit', reference_recids, _source=[ 'control_number', 'citation_count', 'titles', 'earliest_date', 'authors', 'collaboration', 'corporate_author', 'publication_info' ] ) # Create mapping to keep reference order recid_to_reference = { ref['control_number']: ref for ref in resolved_references } for reference in references: row = [] ref_record = recid_to_reference.get( reference.get('recid'), {} ) if 'reference' in reference: reference.update(reference['reference']) del reference['reference'] if 'publication_info' in reference: reference['publication_info'] = force_list( reference['publication_info'] ) row.append(render_template_to_string( "inspirehep_theme/references.html", record=ref_record, reference=reference )) row.append(ref_record.get('citation_count', '')) out.append(row) return out
def references(self): """Reference export for single record in datatables format. :returns: list List of lists where every item represents a datatables row. A row consists of [reference_number, reference, num_citations] """ out = [] references = self.record.get('references') if references: reference_recids = [ str(ref['recid']) for ref in references if ref.get('recid') ] resolved_references = get_es_records( 'lit', reference_recids, _source=[ 'control_number', 'citation_count', 'titles', 'earliest_date', 'authors', 'collaboration', 'corporate_author', 'publication_info' ] ) # Create mapping to keep reference order recid_to_reference = { ref['control_number']: ref for ref in resolved_references } for reference in references: row = [] ref_record = recid_to_reference.get( reference.get('recid'), {} ) if 'reference' in reference: reference.update(reference['reference']) del reference['reference'] if 'publication_info' in reference: reference['publication_info'] = force_force_list( reference['publication_info'] ) row.append(render_template_to_string( "inspirehep_theme/references.html", record=ref_record, reference=reference )) row.append(ref_record.get('citation_count', '')) out.append(row) return out
def _get_hal_id_map(record): affiliation_records = chain.from_iterable(get_value( record, 'authors.affiliations.record', default=[])) affiliation_recids = [get_recid_from_ref(el) for el in affiliation_records] try: institutions = get_es_records('ins', affiliation_recids) except RequestError: institutions = [] return {el['control_number']: _get_hal_id(el) for el in institutions}
def _get_hal_id_map(record): affiliation_records = chain.from_iterable( get_value(record, 'authors.affiliations.record', default=[])) affiliation_recids = [get_recid_from_ref(el) for el in affiliation_records] try: institutions = get_es_records('ins', affiliation_recids) except RequestError: institutions = [] return {el['control_number']: _get_hal_id(el) for el in institutions}
def get_and_format_references(record): """Format references. .. deprecated:: 2018-06-07 """ out = [] references = record.get('references') if references: reference_recids = [ str(ref['recid']) for ref in references if ref.get('recid') ] resolved_references = get_es_records( 'lit', reference_recids, _source=[ 'authors', 'citation_count', 'collaboration', 'control_number', 'corporate_author', 'earliest_date', 'publication_info', 'titles', ] ) # Create mapping to keep reference order recid_to_reference = { ref['control_number']: ref for ref in resolved_references } for reference in references: row = [] ref_record = recid_to_reference.get( reference.get('recid'), {} ) if 'reference' in reference: reference.update(reference['reference']) del reference['reference'] if 'publication_info' in reference: reference['publication_info'] = force_list( reference['publication_info'] ) row.append(render_template_to_string( 'inspirehep_theme/references.html', record=ref_record, reference=reference )) row.append(ref_record.get('citation_count', '')) out.append(row) return out
def get_and_format_references(record): """Format references. .. deprecated:: 2018-06-07 """ out = [] references = record.get('references') if references: reference_recids = [ str(ref['recid']) for ref in references if ref.get('recid') ] resolved_references = get_es_records('lit', reference_recids, _source=[ 'authors', 'citation_count', 'collaboration', 'control_number', 'corporate_author', 'earliest_date', 'publication_info', 'titles', ]) # Create mapping to keep reference order recid_to_reference = { ref['control_number']: ref for ref in resolved_references } for reference in references: row = [] ref_record = recid_to_reference.get(reference.get('recid'), {}) if 'reference' in reference: reference.update(reference['reference']) del reference['reference'] if 'publication_info' in reference: reference['publication_info'] = force_list( reference['publication_info']) row.append( render_template_to_string('inspirehep_theme/references.html', record=ref_record, reference=reference)) row.append(ref_record.get('citation_count', '')) out.append(row) return out
def _parse_structures(record): structures = [] recids = [] for author in record.get('authors', []): for affiliation in author.get('affiliations', []): try: recids.append(str(get_recid_from_ref(affiliation['record']))) affiliation['recid'] = get_recid_from_ref( affiliation['record']) except KeyError: continue try: records = get_es_records('ins', recids) except RequestError: records = [] for record in records: structures.append(_structure_data(record)) return dedupe_list(structures)
def test_get_es_records_accepts_lists_of_integers(app): records = get_es_records('lit', [4328]) assert len(records) == 1
def serialize(self, pid, record, links_factory=None): """ Serialize a single impact graph from a record. :param pid: Persistent identifier instance. :param record: Record instance. :param links_factory: Factory function for the link generation, which are added to the response. """ out = {} # Add information about current record out['inspire_id'] = record['control_number'] out['title'] = get_title(record) out['year'] = record['earliest_date'].split('-')[0] # Get citations citations = [] record_citations = LiteratureSearch().query( 'match', references__recid=record['control_number'], ).params( size=9999, _source=[ 'control_number', 'citation_count', 'titles', 'earliest_date' ] ).execute().hits for citation in record_citations: try: citation_count = citation.citation_count except AttributeError: citation_count = 0 citations.append({ "inspire_id": citation['control_number'], "citation_count": citation_count, "title": get_title(citation.to_dict()), "year": citation['earliest_date'].split('-')[0] }) out['citations'] = citations # Get references record_references = record.get('references', []) references = [] reference_recids = [ ref['recid'] for ref in record_references if ref.get('recid') ] if reference_recids: record_references = get_es_records( 'lit', reference_recids, _source=[ 'control_number', 'citation_count', 'titles', 'earliest_date' ] ) for reference in record_references: try: citation_count = reference.citation_count except AttributeError: citation_count = 0 references.append({ "inspire_id": reference['control_number'], "citation_count": citation_count, "title": get_title(reference), "year": reference['earliest_date'].split('-')[0] }) out['references'] = references return json.dumps(out)
def test_get_es_records_accepts_lists_of_strings(app): records = get_es_records('lit', ['4328']) assert len(records) == 1
def test_get_es_records_handles_empty_lists(app): get_es_records('lit', []) # Does not raise.
def serialize(self, pid, record, links_factory=None): """ Serialize a single impact graph from a record. :param pid: Persistent identifier instance. :param record: Record instance. :param links_factory: Factory function for the link generation, which are added to the response. """ out = {} # Add information about current record out['inspire_id'] = record['control_number'] out['title'] = get_title(record) out['year'] = record['earliest_date'].split('-')[0] # Get citations citations = [] record_citations = LiteratureSearch().query_from_iq( 'refersto:' + str(record['control_number']) ).params( size=9999, _source=[ 'control_number', 'citation_count', 'titles', 'earliest_date' ] ).execute().hits for citation in record_citations: try: citation_count = citation.citation_count except AttributeError: citation_count = 0 citations.append({ "inspire_id": citation['control_number'], "citation_count": citation_count, "title": get_title(citation.to_dict()), "year": citation['earliest_date'].split('-')[0] }) out['citations'] = citations # Get references record_references = record.get('references', []) references = [] reference_recids = [ ref['recid'] for ref in record_references if ref.get('recid') ] if reference_recids: record_references = get_es_records( 'lit', reference_recids, _source=[ 'control_number', 'citation_count', 'titles', 'earliest_date' ] ) for reference in record_references: try: citation_count = reference.citation_count except AttributeError: citation_count = 0 references.append({ "inspire_id": reference['control_number'], "citation_count": citation_count, "title": get_title(reference), "year": reference['earliest_date'].split('-')[0] }) out['references'] = references return json.dumps(out)
def test_get_es_records_raises_on_empty_list(app): with app.app_context(): with pytest.raises(RequestError): get_es_records('lit', [])
def test_get_es_records_handles_empty_lists(app): assert get_es_records('lit', []) == []