def fail_if_unauthorized(node_addon, auth, file_id): node = node_addon.owner user_settings = node_addon.user_settings if file_id is None: raise HTTPError(httplib.NOT_FOUND) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) released_file_ids = [f.id for f in get_files(study, released=True)] all_file_ids = [f.id for f in get_files(study)] + released_file_ids if file_id not in all_file_ids: raise HTTPError(httplib.FORBIDDEN) elif not node.can_edit(auth) and file_id not in released_file_ids: raise HTTPError(httplib.UNAUTHORIZED)
def fail_if_unauthorized(node_addon, auth, file_id): node = node_addon.owner user_settings = node_addon.user_settings if file_id is None: raise HTTPError(http.NOT_FOUND) try: connection = connect_from_settings_or_403(user_settings) except HTTPError as error: if error.code == 403: connection = None else: raise dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) released_file_ids = [f.id for f in get_files(study, released=True)] all_file_ids = [f.id for f in get_files(study)] + released_file_ids if file_id not in all_file_ids: raise HTTPError(http.FORBIDDEN) elif not node.can_edit(auth) and file_id not in released_file_ids: raise HTTPError(http.UNAUTHORIZED)
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 dataverse_hgrid_data_contents(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings state = request.args.get('state') default_state = 'released' state = 'released' if not node.can_edit(auth) else state or default_state released = state == 'released' can_edit = node.can_edit(auth) and not node.is_registration and not released can_view = node.can_view(auth) connection = connect_from_settings(user_settings) if node_addon.study_hdl is None or connection is None: return [] dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) # Quit if hdl does not produce a study if study is None: return [] info = [] for f in get_files(study, released): item = { 'addon': 'dataverse', 'provider': 'dataverse', rubeus.KIND: 'file', 'name': f.name, 'path': f.name, 'file_id': f.id, 'ext': os.path.splitext(f.name)[1], 'urls': { 'view': node.web_url_for('dataverse_view_file', path=f.id), 'download': node.web_url_for('dataverse_download_file', path=f.id), 'delete': node.api_url_for('dataverse_delete_file', path=f.id), }, 'permissions': { 'view': can_view, 'edit': can_edit, }, } info.append(item) return {'data': info}
def dataverse_hgrid_data_contents(node_addon, auth, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings state = request.args.get('state') default_state = 'released' state = 'released' if not node.can_edit(auth) else state or default_state released = state == 'released' can_edit = node.can_edit( auth) and not node.is_registration and not released can_view = node.can_view(auth) connection = connect_from_settings(user_settings) if node_addon.study_hdl is None or connection is None: return [] dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) # Quit if hdl does not produce a study if study is None: return [] info = [] for f in get_files(study, released): item = { 'addon': 'dataverse', 'provider': 'dataverse', rubeus.KIND: 'file', 'name': f.name, 'path': f.name, 'file_id': f.id, 'ext': os.path.splitext(f.name)[1], 'urls': { 'view': node.web_url_for('dataverse_view_file', path=f.id), 'download': node.web_url_for('dataverse_download_file', path=f.id), 'delete': node.api_url_for('dataverse_delete_file', path=f.id), }, 'permissions': { 'view': can_view, 'edit': can_edit, }, } info.append(item) return {'data': info}
def test_get_files_published(self): published = True get_files(self.mock_dataset, published) self.mock_dataset.get_files.assert_called_once_with("latest-published")
def test_get_files(self): published = False get_files(self.mock_dataset, published) self.mock_dataset.get_files.assert_called_once_with("latest")
def dataverse_hgrid_root(node_addon, auth, state=None, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings default_state = 'released' state = 'released' if not node.can_edit(auth) else state or default_state connection = connect_from_settings(user_settings) # Quit if no study linked if node_addon.study_hdl is None or connection is None: return [] dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) # Quit if hdl does not produce a study if study is None: return [] released_files = get_files(study, released=True) authorized = node.can_edit(auth) # Produce draft version or quit if no released version is available if not released_files: if authorized: state = 'draft' else: return [] study_name = node_addon.study if len(study_name) > 23: study_name = u'{0}...'.format(study_name[:20]) permissions = { 'edit': node.can_edit(auth) and not node.is_registration, 'view': node.can_view(auth) } urls = { 'upload': node.api_url_for('dataverse_upload_file'), 'fetch': node.api_url_for('dataverse_hgrid_data_contents', state=state), 'state': node.api_url_for('dataverse_root_folder_public'), 'release': node.api_url_for('dataverse_release_study'), } buttons = [ rubeus.build_addon_button('<i class="fa fa-globe"></i> Release Study', 'releaseStudy') ] if state == 'draft' else None return [ rubeus.build_addon_root( node_addon, study_name, urls=urls, permissions=permissions, buttons=buttons, study=study_name, doi=study.doi, dataverse=dataverse.title, citation=study.citation, hasReleasedFiles=bool(released_files), state=state, ) ]
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, )]
def dataverse_hgrid_root(node_addon, auth, state=None, **kwargs): node = node_addon.owner user_settings = node_addon.user_settings default_state = 'released' state = 'released' if not node.can_edit(auth) else state or default_state connection = connect_from_settings(user_settings) # Quit if no study linked if node_addon.study_hdl is None or connection is None: return [] dataverse = get_dataverse(connection, node_addon.dataverse_alias) study = get_study(dataverse, node_addon.study_hdl) # Quit if hdl does not produce a study if study is None: return [] released_files = get_files(study, released=True) authorized = node.can_edit(auth) # Produce draft version or quit if no released version is available if not released_files: if authorized: state = 'draft' else: return [] study_name = node_addon.study if len(study_name) > 23: study_name = u'{0}...'.format(study_name[:20]) permissions = { 'edit': node.can_edit(auth) and not node.is_registration, 'view': node.can_view(auth) } urls = { 'upload': node.api_url_for('dataverse_upload_file'), 'fetch': node.api_url_for('dataverse_hgrid_data_contents', state=state), 'state': node.api_url_for('dataverse_root_folder_public'), 'release': node.api_url_for('dataverse_release_study'), } buttons = [rubeus.build_addon_button( '<i class="fa fa-globe"></i> Release Study', 'releaseStudy')] if state == 'draft' else None return [rubeus.build_addon_root( node_addon, study_name, urls=urls, permissions=permissions, buttons=buttons, study=study_name, doi=study.doi, dataverse=dataverse.title, citation=study.citation, hasReleasedFiles=bool(released_files), state=state, )]
def test_get_files(self): released = True get_files(self.mock_study, released) self.mock_study.get_files.assert_called_once_with(released)
def test_get_files_published(self): published = True get_files(self.mock_dataset, published) self.mock_dataset.get_files.assert_called_once_with('latest-published')
def test_get_files(self): published = False get_files(self.mock_dataset, published) self.mock_dataset.get_files.assert_called_once_with('latest')