def test_get_dataset_calls_patched_timeout_method(self, mock_requests): # Verify optional timeout parameter is passed to requests by dataverse client. # https://github.com/IQSS/dataverse-client-python/pull/27 dataverse = Dataverse(mock.Mock(), mock.Mock()) dataverse.connection.auth = 'me' dataverse.collection.get.return_value = '123' mock_requests.get.side_effect = Exception('Done Testing') with assert_raises(Exception) as e: get_dataset(dataverse, 'My hdl') assert_is(mock_requests.get.assert_called_once_with('123', auth='me', timeout=settings.REQUEST_TIMEOUT), None) assert_equal(e.exception.message, 'Done Testing')
def dataverse_get_widget_contents(node_addon, **kwargs): data = { 'connected': False, } if not node_addon.complete: return {'data': data}, http.OK doi = node_addon.dataset_doi alias = node_addon.dataverse_alias connection = connect_from_settings_or_401(node_addon.user_settings) dataverse = get_dataverse(connection, alias) dataset = get_dataset(dataverse, doi) if dataset is None: return {'data': data}, http.BAD_REQUEST dataverse_url = 'http://{0}/dataverse/'.format(HOST) + alias dataset_url = 'http://dx.doi.org/' + doi data.update({ 'connected': True, 'dataverse': node_addon.dataverse, 'dataverseUrl': dataverse_url, 'dataset': node_addon.dataset, 'doi': doi, 'datasetUrl': dataset_url, 'citation': dataset.citation, }) return {'data': data}, http.OK
def dataverse_get_widget_contents(node_addon, **kwargs): data = { 'connected': False, } if not node_addon.complete: return {'data': data}, http.OK doi = node_addon.dataset_doi alias = node_addon.dataverse_alias connection = client.connect_from_settings_or_401(node_addon) dataverse = client.get_dataverse(connection, alias) dataset = client.get_dataset(dataverse, doi) if dataset is None: return {'data': data}, http.BAD_REQUEST dataverse_host = node_addon.external_account.oauth_key dataverse_url = 'http://{0}/dataverse/{1}'.format(dataverse_host, alias) dataset_url = 'http://dx.doi.org/' + doi data.update({ 'connected': True, 'dataverse': node_addon.dataverse, 'dataverseUrl': dataverse_url, 'dataset': node_addon.dataset, 'doi': doi, 'datasetUrl': dataset_url, 'citation': dataset.citation, }) return {'data': data}, http.OK
def dataverse_set_config(node_addon, auth, **kwargs): """Saves selected Dataverse and dataset to node settings""" user_settings = node_addon.user_settings user = auth.user if user_settings and user_settings.owner != user: raise HTTPError(http.FORBIDDEN) try: assert_clean(request.json) except AssertionError: # TODO: Test me! raise HTTPError(http.NOT_ACCEPTABLE) alias = request.json.get('dataverse', {}).get('alias') doi = request.json.get('dataset', {}).get('doi') if doi is None or alias is None: return HTTPError(http.BAD_REQUEST) connection = client.connect_from_settings(node_addon) dataverse = client.get_dataverse(connection, alias) dataset = client.get_dataset(dataverse, doi) node_addon.set_folder(dataverse, dataset, auth) return {'dataverse': dataverse.title, 'dataset': dataset.title}, http.OK
def dataverse_publish_dataset(node_addon, auth, **kwargs): node = node_addon.owner publish_both = request.json.get('publish_both', False) now = timezone.now() connection = client.connect_from_settings_or_401(node_addon) dataverse = client.get_dataverse(connection, node_addon.dataverse_alias) dataset = client.get_dataset(dataverse, node_addon.dataset_doi) if publish_both: client.publish_dataverse(dataverse) client.publish_dataset(dataset) # Add a log node.add_log( action='dataverse_dataset_published', params={ 'project': node.parent_id, 'node': node._id, 'dataset': dataset.title, }, auth=auth, log_date=now, ) return {'dataset': dataset.title}, http.OK
def dataverse_publish(node_addon, auth, publish_both=False): node = node_addon.owner user_settings = node_addon.user_settings now = datetime.datetime.utcnow() try: connection = connect_from_settings_or_401(user_settings) except HTTPError as error: if error.code == httplib.UNAUTHORIZED: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) dataset = get_dataset(dataverse, node_addon.dataset_doi) if publish_both: publish_dataverse(dataverse) publish_dataset(dataset) # Add a log node.add_log( action="dataverse_dataset_published", params={"project": node.parent_id, "node": node._primary_key, "dataset": dataset.title}, auth=auth, log_date=now, ) return {"dataset": dataset.title}, httplib.OK
def dataverse_publish_dataset(node_addon, auth, **kwargs): node = node_addon.owner publish_both = request.json.get('publish_both', False) now = datetime.datetime.utcnow() try: connection = connect_from_settings_or_401(node_addon) except HTTPError as error: if error.code == httplib.UNAUTHORIZED: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) dataset = get_dataset(dataverse, node_addon.dataset_doi) if publish_both: publish_dataverse(dataverse) publish_dataset(dataset) # Add a log node.add_log( action='dataverse_dataset_published', params={ 'project': node.parent_id, 'node': node._primary_key, 'dataset': dataset.title, }, auth=auth, log_date=now, ) return {'dataset': dataset.title}, httplib.OK
def dataverse_get_widget_contents(node_addon, **kwargs): data = { 'connected': False, } if not node_addon.complete: return {'data': data}, http.OK doi = node_addon.dataset_doi alias = node_addon.dataverse_alias connection = client.connect_from_settings_or_401(node_addon) dataverse = client.get_dataverse(connection, alias) dataset = client.get_dataset(dataverse, doi) if dataset is None: return {'data': data}, http.BAD_REQUEST dataverse_host = node_addon.external_account.oauth_key dataverse_url = 'http://{0}/dataverse/{1}'.format(dataverse_host, alias) dataset_url = 'https://doi.org/' + doi data.update({ 'connected': True, 'dataverse': node_addon.dataverse, 'dataverseUrl': dataverse_url, 'dataset': node_addon.dataset, 'doi': doi, 'datasetUrl': dataset_url, 'citation': dataset.citation, }) return {'data': data}, http.OK
def dataverse_publish(node_addon, auth, publish_both=False): node = node_addon.owner user_settings = node_addon.user_settings now = datetime.datetime.utcnow() try: connection = connect_from_settings_or_401(user_settings) except HTTPError as error: if error.code == httplib.UNAUTHORIZED: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) dataset = get_dataset(dataverse, node_addon.dataset_doi) if publish_both: publish_dataverse(dataverse) publish_dataset(dataset) # Add a log node.add_log( action='dataverse_dataset_published', params={ 'project': node.parent_id, 'node': node._primary_key, 'dataset': dataset.title, }, auth=auth, log_date=now, ) return {'dataset': dataset.title}, httplib.OK
def dataverse_publish_dataset(node_addon, auth, **kwargs): node = node_addon.owner publish_both = request.json.get('publish_both', False) now = datetime.datetime.utcnow() connection = client.connect_from_settings_or_401(node_addon) dataverse = client.get_dataverse(connection, node_addon.dataverse_alias) dataset = client.get_dataset(dataverse, node_addon.dataset_doi) if publish_both: client.publish_dataverse(dataverse) client.publish_dataset(dataset) # Add a log node.add_log( action='dataverse_dataset_published', params={ 'project': node.parent_id, 'node': node._id, 'dataset': dataset.title, }, auth=auth, log_date=now, ) return {'dataset': dataset.title}, http.OK
def dataverse_hgrid_root(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings default_version = 'latest-published' version = 'latest-published' if not node.can_edit(auth) else default_version # Quit if no dataset linked if not node_addon.complete: return [] can_edit = node.can_edit(auth) permissions = { 'edit': can_edit and not node.is_registration, 'view': node.can_view(auth) } try: connection = connect_from_settings(user_settings) dataverse = get_dataverse(connection, node_addon.dataverse_alias) dataset = get_dataset(dataverse, node_addon.dataset_doi) except SSLError: return [rubeus.build_addon_root( node_addon, node_addon.dataset, permissions=permissions )] # Quit if doi does not produce a dataset if dataset is None: return [] published_files = get_files(dataset, published=True) # Produce draft version or quit if no published version is available if not published_files: if can_edit: version = 'latest' else: return [] urls = { 'publish': node.api_url_for('dataverse_publish_dataset'), 'publishBoth': node.api_url_for('dataverse_publish_both') } return [rubeus.build_addon_root( node_addon, node_addon.dataset, urls=urls, permissions=permissions, dataset=node_addon.dataset, doi=dataset.doi, dataverse=dataverse.title, hasPublishedFiles=bool(published_files), dataverseIsPublished=dataverse.is_published, version=version, )]
def test_get_dataset(self): self.mock_dataset.get_state.return_value = 'DRAFT' self.mock_dataverse.get_dataset_by_doi.return_value = self.mock_dataset s = get_dataset(self.mock_dataverse, 'My hdl') assert_is(self.mock_dataverse.get_dataset_by_doi.assert_called_once_with('My hdl', timeout=settings.REQUEST_TIMEOUT), None) assert_equal(s, self.mock_dataset)
def test_get_dataset(self): self.mock_dataset.get_state.return_value = 'DRAFT' self.mock_dataverse.get_dataset_by_doi.return_value = self.mock_dataset s = get_dataset(self.mock_dataverse, 'My hdl') self.mock_dataverse.get_dataset_by_doi.assert_called_once_with('My hdl') assert_equal(s, self.mock_dataset)
def test_get_dataset(self): self.mock_dataset.get_state.return_value = "DRAFT" self.mock_dataverse.get_dataset_by_doi.return_value = self.mock_dataset s = get_dataset(self.mock_dataverse, "My hdl") self.mock_dataverse.get_dataset_by_doi.assert_called_once_with("My hdl") assert_equal(s, self.mock_dataset)
def test_get_bad_dataset(self): error = UnicodeDecodeError("utf-8", b"", 1, 2, "jeepers") self.mock_dataset.get_state.side_effect = error self.mock_dataverse.get_dataset_by_doi.return_value = self.mock_dataset with assert_raises(HTTPError) as e: s = get_dataset(self.mock_dataverse, "My hdl") self.mock_dataverse.get_dataset_by_doi.assert_called_once_with("My hdl") assert_equal(e.exception.code, 406)
def test_get_deaccessioned_dataset(self): self.mock_dataset.get_state.return_value = "DEACCESSIONED" self.mock_dataverse.get_dataset_by_doi.return_value = self.mock_dataset with assert_raises(HTTPError) as e: s = get_dataset(self.mock_dataverse, "My hdl") self.mock_dataverse.get_dataset_by_doi.assert_called_once_with("My hdl") assert_equal(e.exception.code, 410)
def test_get_bad_dataset(self): error = UnicodeDecodeError('utf-8', b'', 1, 2, 'jeepers') self.mock_dataset.get_state.side_effect = error self.mock_dataverse.get_dataset_by_doi.return_value = self.mock_dataset with assert_raises(HTTPError) as e: s = get_dataset(self.mock_dataverse, 'My hdl') assert_is(self.mock_dataverse.get_dataset_by_doi.assert_called_once_with('My hdl', timeout=settings.REQUEST_TIMEOUT), None) assert_equal(e.exception.code, 406)
def test_get_deaccessioned_dataset(self): self.mock_dataset.get_state.return_value = 'DEACCESSIONED' self.mock_dataverse.get_dataset_by_doi.return_value = self.mock_dataset with assert_raises(HTTPError) as e: s = get_dataset(self.mock_dataverse, 'My hdl') assert_is(self.mock_dataverse.get_dataset_by_doi.assert_called_once_with('My hdl', timeout=settings.REQUEST_TIMEOUT), None) assert_equal(e.exception.code, 410)
def dataverse_set_config(node_addon, auth, **kwargs): """Saves selected Dataverse and dataset to node settings""" user_settings = node_addon.user_settings user = auth.user if user_settings and user_settings.owner != user: raise HTTPError(http.FORBIDDEN) try: assert_clean(request.json) except AssertionError: # TODO: Test me! raise HTTPError(http.NOT_ACCEPTABLE) alias = request.json.get('dataverse').get('alias') doi = request.json.get('dataset').get('doi') if doi is None: return HTTPError(http.BAD_REQUEST) connection = client.connect_from_settings(node_addon) dataverse = client.get_dataverse(connection, alias) dataset = client.get_dataset(dataverse, doi) node_addon.dataverse_alias = dataverse.alias node_addon.dataverse = dataverse.title node_addon.dataset_doi = dataset.doi node_addon.dataset_id = dataset.id node_addon.dataset = dataset.title node = node_addon.owner node.add_log( action='dataverse_dataset_linked', params={ 'project': node.parent_id, 'node': node._primary_key, 'dataset': dataset.title, }, auth=auth, ) node_addon.save() return {'dataverse': dataverse.title, 'dataset': dataset.title}, http.OK
def set_dataverse_and_dataset(node_addon, auth, **kwargs): user_settings = node_addon.user_settings user = auth.user if user_settings and user_settings.owner != user: raise HTTPError(http.FORBIDDEN) try: assert_clean(request.json) except AssertionError: # TODO: Test me! raise HTTPError(http.NOT_ACCEPTABLE) alias = request.json.get("dataverse").get("alias") doi = request.json.get("dataset").get("doi") if doi is None: return HTTPError(http.BAD_REQUEST) connection = client.connect_from_settings(user_settings) dataverse = client.get_dataverse(connection, alias) dataset = client.get_dataset(dataverse, doi) node_addon.dataverse_alias = dataverse.alias node_addon.dataverse = dataverse.title node_addon.dataset_doi = dataset.doi node_addon.dataset_id = dataset.id node_addon.dataset = dataset.title node = node_addon.owner node.add_log( action="dataverse_dataset_linked", params={"project": node.parent_id, "node": node._primary_key, "dataset": dataset.title}, auth=auth, ) node_addon.save() return {"dataverse": dataverse.title, "dataset": dataset.title}, http.OK
def _dataverse_root_folder(node_addon, auth, **kwargs): node = node_addon.owner default_version = 'latest-published' version = 'latest-published' if not node.can_edit(auth) else default_version # Quit if no dataset linked if not node_addon.complete: return [] can_edit = node.can_edit(auth) permissions = { 'edit': can_edit and not node.is_registration, 'view': node.can_view(auth) } try: connection = client.connect_from_settings(node_addon) dataverse = client.get_dataverse(connection, node_addon.dataverse_alias) dataset = client.get_dataset(dataverse, node_addon.dataset_doi) except SSLError: return [rubeus.build_addon_root( node_addon, node_addon.dataset, permissions=permissions, private_key=kwargs.get('view_only', None), )] # Quit if doi does not produce a dataset if dataset is None: return [] published_files = client.get_files(dataset, published=True) # Produce draft version or quit if no published version is available if not published_files: if can_edit: version = 'latest' else: return [] urls = { 'publish': node.api_url_for('dataverse_publish_dataset'), } # determine if there are any changes between the published and draft # versions of the dataset try: dataset.get_metadata('latest-published') dataset_is_published = True dataset_draft_modified = dataset.get_state() == 'DRAFT' except VersionJsonNotFoundError: dataset_is_published = False dataset_draft_modified = True # Get the dataverse host # (stored in oauth_key because dataverse doesn't use that) dataverse_host = node_addon.external_account.oauth_key return [rubeus.build_addon_root( node_addon, node_addon.dataset, urls=urls, permissions=permissions, dataset=node_addon.dataset, doi=dataset.doi, dataverse=dataverse.title, hasPublishedFiles=bool(published_files), dataverseIsPublished=dataverse.is_published, datasetIsPublished=dataset_is_published, datasetDraftModified=dataset_draft_modified, version=version, host=dataverse_host, private_key=kwargs.get('view_only', None), )]
def _dataverse_root_folder(node_addon, auth, **kwargs): node = node_addon.owner default_version = 'latest-published' version = 'latest-published' if not node.can_edit(auth) else default_version # Quit if no dataset linked if not node_addon.complete: return [] can_edit = node.can_edit(auth) permissions = { 'edit': can_edit and not node.is_registration, 'view': node.can_view(auth) } try: connection = client.connect_from_settings(node_addon) dataverse = client.get_dataverse(connection, node_addon.dataverse_alias) dataset = client.get_dataset(dataverse, node_addon.dataset_doi) except SSLError: return [rubeus.build_addon_root( node_addon, node_addon.dataset, permissions=permissions )] # Quit if doi does not produce a dataset if dataset is None: return [] published_files = client.get_files(dataset, published=True) # Produce draft version or quit if no published version is available if not published_files: if can_edit: version = 'latest' else: return [] urls = { 'publish': node.api_url_for('dataverse_publish_dataset'), } # determine if there are any changes between the published and draft # versions of the dataset try: dataset.get_metadata('latest-published') dataset_is_published = True dataset_draft_modified = dataset.get_state() == 'DRAFT' except VersionJsonNotFoundError: dataset_is_published = False dataset_draft_modified = True # Get the dataverse host # (stored in oauth_key because dataverse doesn't use that) dataverse_host = node_addon.external_account.oauth_key return [rubeus.build_addon_root( node_addon, node_addon.dataset, urls=urls, permissions=permissions, dataset=node_addon.dataset, doi=dataset.doi, dataverse=dataverse.title, hasPublishedFiles=bool(published_files), dataverseIsPublished=dataverse.is_published, datasetIsPublished=dataset_is_published, datasetDraftModified=dataset_draft_modified, version=version, host=dataverse_host, )]