From 4f8a9fc06a141e80985b5de8fc1147ff0ff8e440 Mon Sep 17 00:00:00 2001 From: Sergi Blanco-Cuaresma Date: Mon, 7 Jan 2019 20:51:20 +0100 Subject: [PATCH] Only emit event if the citing bibcode exists in ADS --- ADSCitationCapture/tasks.py | 14 +++++------ ADSCitationCapture/tests/test_tasks.py | 34 +++++++++++++------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/ADSCitationCapture/tasks.py b/ADSCitationCapture/tasks.py index 6f310fc..dbb8670 100644 --- a/ADSCitationCapture/tasks.py +++ b/ADSCitationCapture/tasks.py @@ -87,13 +87,9 @@ def task_process_new_citation(citation_change, force=False): # Get citations from the database and transform the stored bibcodes into their canonical ones as registered in Solr. original_citations = db.get_citations_by_bibcode(app, citation_target_bibcode) citations = api.get_canonical_bibcodes(app, original_citations) - # Clean before adding the current citation - citations = [c for c in citations if c != citation_change.citing and c != canonical_citing_bibcode] # Add canonical bibcode of current detected citation - if canonical_citing_bibcode: + if canonical_citing_bibcode and canonical_citing_bibcode not in citations: citations.append(canonical_citing_bibcode) - else: - citations.append(citation_change.citing) logger.debug("Calling 'task_output_results' with '%s'", citation_change) task_output_results.delay(citation_change, parsed_metadata, citations) logger.debug("Calling 'task_emit_event' with '%s'", citation_change) @@ -197,8 +193,12 @@ def task_emit_event(citation_change, parsed_metadata): is_link_alive = parsed_metadata and parsed_metadata.get("link_alive", False) is_software = parsed_metadata and parsed_metadata.get("doctype", "").lower() == "software" if is_software and is_link_alive: - emitted = webhook.emit_event(app.conf['ADS_WEBHOOK_URL'], app.conf['ADS_WEBHOOK_AUTH_TOKEN'], citation_change) - #emitted = True + canonical_citing_bibcode = api.get_canonical_bibcode(app, citation_change.citing) + if canonical_citing_bibcode: + # Citing source exists in ADS + citation_change.citing = canonical_citing_bibcode + emitted = webhook.emit_event(app.conf['ADS_WEBHOOK_URL'], app.conf['ADS_WEBHOOK_AUTH_TOKEN'], citation_change) + #emitted = True if emitted: logger.debug("Emitted '%s'", citation_change) diff --git a/ADSCitationCapture/tests/test_tasks.py b/ADSCitationCapture/tests/test_tasks.py index 9abb221..3dddc6d 100644 --- a/ADSCitationCapture/tests/test_tasks.py +++ b/ADSCitationCapture/tests/test_tasks.py @@ -41,7 +41,7 @@ def test_process_new_citation_changes_doi(self): patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -76,7 +76,7 @@ def test_process_updated_citation_changes_doi(self): patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -92,7 +92,7 @@ def test_process_updated_citation_changes_doi(self): self.assertFalse(fetch_metadata.called) self.assertFalse(parse_metadata.called) self.assertFalse(url_is_alive.called) - self.assertFalse(get_canonical_bibcode.called) + self.assertTrue(get_canonical_bibcode.called) self.assertTrue(get_canonical_bibcodes.called) self.assertTrue(get_citations_by_bibcode.called) self.assertFalse(store_citation_target.called) @@ -111,7 +111,7 @@ def test_process_deleted_citation_changes_doi(self): patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -127,7 +127,7 @@ def test_process_deleted_citation_changes_doi(self): self.assertFalse(fetch_metadata.called) self.assertFalse(parse_metadata.called) self.assertFalse(url_is_alive.called) - self.assertFalse(get_canonical_bibcode.called) + self.assertTrue(get_canonical_bibcode.called) self.assertTrue(get_canonical_bibcodes.called) self.assertTrue(get_citations_by_bibcode.called) self.assertFalse(store_citation_target.called) @@ -146,7 +146,7 @@ def test_process_new_citation_changes_doi_when_target_exists_citation_doesnt(sel patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -181,7 +181,7 @@ def test_process_updated_citation_changes_doi_when_target_exists_citation_doesnt patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -216,7 +216,7 @@ def test_process_deleted_citation_changes_doi_when_target_exists_citation_doesnt patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -251,7 +251,7 @@ def test_process_new_citation_changes_doi_when_target_and_citation_exist(self): patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -286,7 +286,7 @@ def test_process_updated_citation_changes_doi_when_citation_doesnt_exist(self): patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -321,7 +321,7 @@ def test_process_deleted_citation_changes_doi_when_citation_doesnt_exist(self): patch.object(doi, 'fetch_metadata', return_value=self.mock_data[doi_id]['raw']) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value=self.mock_data[doi_id]['parsed']) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -362,7 +362,7 @@ def test_process_citation_changes_ascl(self): patch.object(doi, 'fetch_metadata', return_value=None) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value={}) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -403,7 +403,7 @@ def test_process_citation_changes_url(self): patch.object(doi, 'fetch_metadata', return_value=None) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value={}) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -445,7 +445,7 @@ def test_process_citation_changes_malformed_url(self): patch.object(doi, 'fetch_metadata', return_value=None) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value={}) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -486,7 +486,7 @@ def test_process_citation_changes_empty(self): patch.object(doi, 'fetch_metadata', return_value=None) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value={}) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -520,7 +520,7 @@ def test_process_new_citation_changes_doi_unparsable_http_response(self): patch.object(doi, 'fetch_metadata', return_value="Unparsable response") as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value={}) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \ @@ -555,7 +555,7 @@ def test_process_new_citation_changes_doi_http_error(self): patch.object(doi, 'fetch_metadata', return_value=None) as fetch_metadata, \ patch.object(doi, 'parse_metadata', return_value={}) as parse_metadata, \ patch.object(url, 'is_alive', return_value=True) as url_is_alive, \ - patch.object(api, 'get_canonical_bibcode', return_value=None) as get_canonical_bibcode, \ + patch.object(api, 'get_canonical_bibcode', return_value=citation_changes.changes[0].citing) as get_canonical_bibcode, \ patch.object(api, 'get_canonical_bibcodes', return_value=[]) as get_canonical_bibcodes, \ patch.object(db, 'get_citations_by_bibcode', return_value=[]) as get_citations_by_bibcode, \ patch.object(db, 'store_citation_target', return_value=True) as store_citation_target, \