def test_build_addon_root_has_correct_upload_limits(self): self.node_settings.config.max_file_size = 10 self.node_settings.config.high_max_file_size = 20 node = self.project user = self.project.creator auth = Auth(user) permissions = { 'view': node.can_view(auth), 'edit': node.can_edit(auth) and not node.is_registration, } result = rubeus.build_addon_root( self.node_settings, self.node_settings.bucket, permissions=permissions, user=user ) assert_equal(result['accept']['maxSize'], self.node_settings.config.max_file_size) # user now has elevated upload limit user.system_tags.append('high_upload_limit') user.save() result = rubeus.build_addon_root( self.node_settings, self.node_settings.bucket, permissions=permissions, user=user ) assert_equal( result['accept']['maxSize'], self.node_settings.config.high_max_file_size )
def test_build_addon_root_has_correct_upload_limits(self): self.node_settings.config.max_file_size = 10 self.node_settings.config.high_max_file_size = 20 node = self.project user = self.project.creator auth = Auth(user) permissions = { 'view': node.can_view(auth), 'edit': node.can_edit(auth) and not node.is_registration, } result = rubeus.build_addon_root(self.node_settings, self.node_settings.bucket, permissions=permissions, user=user) assert_equal(result['accept']['maxSize'], self.node_settings.config.max_file_size) # user now has elevated upload limit user.add_system_tag('high_upload_limit') user.save() result = rubeus.build_addon_root(self.node_settings, self.node_settings.bucket, permissions=permissions, user=user) assert_equal(result['accept']['maxSize'], self.node_settings.config.high_max_file_size)
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_hgrid_dummy_overrides(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) expected = { "isPointer": False, "provider": "s3", "addonFullname": node_settings.config.full_name, "iconUrl": node_settings.config.icon_url, "name": "Amazon S3: {0}".format(node_settings.bucket), "kind": "folder", "permissions": {"view": node.can_view(user), "edit": node.can_edit(user) and not node.is_registration}, "urls": {}, "accept": { "maxSize": node_settings.config.max_file_size, "acceptedFiles": node_settings.config.accept_extensions, }, "isAddonRoot": True, "extra": None, "buttons": None, "nodeId": node._id, "nodeUrl": node.url, "nodeApiUrl": node.api_url, } permissions = {"view": node.can_view(user), "edit": node.can_edit(user) and not node.is_registration} assert_equal( rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions, urls={}), expected )
def test_hgrid_dummy_fail(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) rv = { 'isPointer': False, 'addon': 's3', 'addonFullname': node_settings.config.full_name, 'iconUrl': node_settings.config.icon_url, 'name': 'Amazon Simple Storage Service: {0}'.format( node_settings.bucket ), 'kind': 'folder', 'permissions': { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, }, 'urls': { 'fetch': node.api_url + 's3/hgrid/', 'upload': node.api_url + 's3/upload/' }, 'accept': { 'maxSize': node_settings.config.max_file_size, 'acceptedFiles': node_settings.config.accept_extensions }, 'isAddonRoot': True, } permissions = { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, } assert_not_equals(rubeus.build_addon_root( node_settings, node_settings.bucket, permissions=permissions), rv)
def bitbucket_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = BitbucketClient(access_token=node_settings.external_account.oauth_key) node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): repo = connection.repo(node_settings.user, node_settings.repo) if not repo: # TODO: Add warning message logger.error('Could not access Bitbucket repo') return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except (NotFoundError, Exception): # TODO: Show an alert or change Bitbucket configuration? logger.error('Bitbucket repo not found') return ref = None if branch is None else ref_to_params(branch, sha) name_tpl = '{user}/{repo}'.format( user=node_settings.user, repo=node_settings.repo ) permissions = { 'edit': False, 'view': True, 'private': node_settings.is_private } urls = { 'upload': None, 'fetch': node_settings.owner.api_url + 'bitbucket/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'bitbucket/hgrid/root/', 'zip': node_settings.owner.api_url + 'bitbucket/zipball/' + (ref or ''), 'repo': 'https://bitbucket.com/{0}/{1}/branch/'.format(node_settings.user, node_settings.repo) } branch_names = [each['name'] for each in branches] if not branch_names: branch_names = [branch] # if repo un-init-ed then still add default branch to list of branches return [rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=branch_names, defaultBranch=branch, private_key=kwargs.get('view_only', None), )]
def gitlab_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitLabClient(external_account=node_settings.external_account) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public or node.is_contributor(auth.user): try: repo = connection.repo(node_settings.repo_id) except NotFoundError: logger.error('Could not access GitLab repo') return None try: branch, sha, branches = get_refs(node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection) except (NotFoundError, GitLabError): logger.error('GitLab repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions(node_settings, auth, connection, branch, sha, repo=repo) else: ref = '' can_edit = False permissions = { 'edit': can_edit, 'view': True, 'private': node_settings.is_private } urls = { 'upload': node_settings.owner.api_url + 'gitlab/file/' + ref, 'fetch': node_settings.owner.api_url + 'gitlab/hgrid/' + ref, 'branch': node_settings.owner.api_url + 'gitlab/hgrid/root/' + ref, 'zip': 'https://{0}/{1}/repository/archive.zip?branch={2}'.format(node_settings.external_account.oauth_secret, repo.path_with_namespace, ref), 'repo': 'https://{0}/{1}/tree/{2}'.format(node_settings.external_account.oauth_secret, repo.path_with_namespace, ref) } branch_names = [each.name for each in branches] if not branch_names: branch_names = [branch] # if repo un-init-ed then still add default branch to list of branches return [rubeus.build_addon_root( node_settings, repo.path_with_namespace, urls=urls, permissions=permissions, branches=branch_names, private_key=kwargs.get('view_only', None), default_branch=repo.default_branch, )]
def figshare_hgrid_data(node_settings, auth, parent=None, **kwargs): node = node_settings.owner if node_settings.figshare_type == 'project': item = Figshare.from_settings(node_settings.user_settings).project( node_settings, node_settings.figshare_id) else: item = Figshare.from_settings(node_settings.user_settings).article( node_settings, node_settings.figshare_id) if not node_settings.figshare_id or not node_settings.has_auth or not item: return #TODO Test me #Throw error if neither node_settings.figshare_title = item.get( 'title') or item['items'][0]['title'] node_settings.save() return [ rubeus.build_addon_root( node_settings, u'{0}:{1}'.format(node_settings.figshare_title or 'Unnamed', node_settings.figshare_id), permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, ) ]
def gitlab_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitLabClient(external_account=node_settings.external_account) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public or node.is_contributor(auth.user): try: repo = connection.repo(node_settings.repo_id) except NotFoundError: logger.error('Could not access GitLab repo') return None try: branch, sha, branches = get_refs(node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection) except (NotFoundError, GitLabError): logger.error('GitLab repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions(node_settings, auth, connection, branch, sha, repo=repo) else: ref = '' can_edit = False permissions = { 'edit': can_edit, 'view': True, 'private': node_settings.is_private } urls = { 'upload': node_settings.owner.api_url + 'gitlab/file/' + ref, 'fetch': node_settings.owner.api_url + 'gitlab/hgrid/' + ref, 'branch': node_settings.owner.api_url + 'gitlab/hgrid/root/' + ref, 'zip': 'https://{0}/{1}/repository/archive.zip?branch={2}'.format(node_settings.external_account.oauth_secret, repo['path_with_namespace'], ref), 'repo': 'https://{0}/{1}/tree/{2}'.format(node_settings.external_account.oauth_secret, repo['path_with_namespace'], ref) } branch_names = [each['name'] for each in branches] if not branch_names: branch_names = [branch] # if repo un-init-ed then still add default branch to list of branches return [rubeus.build_addon_root( node_settings, repo['path_with_namespace'], urls=urls, permissions=permissions, branches=branch_names, private_key=kwargs.get('view_only', None), default_branch=repo['default_branch'], )]
def test_hgrid_dummy(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) # FIXME: These tests are very brittle. expected = { "isPointer": False, "provider": "s3", "addonFullname": node_settings.config.full_name, "iconUrl": node_settings.config.icon_url, "name": "Amazon S3: {0}".format(node_settings.bucket), "kind": "folder", "accept": { "maxSize": node_settings.config.max_file_size, "acceptedFiles": node_settings.config.accept_extensions, }, "isAddonRoot": True, "extra": None, "buttons": None, "nodeId": node._id, "nodeUrl": node.url, "nodeApiUrl": node.api_url, } permissions = {"view": node.can_view(user), "edit": node.can_edit(user) and not node.is_registration} expected["permissions"] = permissions actual = rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions) assert actual["urls"]["fetch"] assert actual["urls"]["upload"] del actual["urls"] assert_equals(actual, expected)
def s3_hgrid_data(node_settings, auth, **kwargs): # Dont display if not properly configured if not node_settings.complete: return node = node_settings.owner return [ rubeus.build_addon_root( node_settings, node_settings.bucket, permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, ) ]
def s3_hgrid_data(node_settings, auth, **kwargs): # Quit if no bucket if not node_settings.bucket or not node_settings.user_settings or not node_settings.user_settings.has_auth: return node = node_settings.owner return [ rubeus.build_addon_root( node_settings, node_settings.bucket, permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, ) ]
def test_hgrid_dummy(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) # FIXME: These tests are very brittle. expected = { 'isPointer': False, 'provider': 's3', 'addonFullname': node_settings.config.full_name, 'iconUrl': node_settings.config.icon_url, 'name': 'Amazon Simple Storage Service: {0}'.format(node_settings.bucket), 'kind': 'folder', 'accept': { 'maxSize': node_settings.config.max_file_size, 'acceptedFiles': node_settings.config.accept_extensions }, 'isAddonRoot': True, 'extra': None, 'buttons': None, 'nodeId': node._id, 'nodeUrl': node.url, 'nodeApiUrl': node.api_url, } permissions = { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, } expected['permissions'] = permissions actual = rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions) assert actual['urls']['fetch'] assert actual['urls']['upload'] del actual['urls'] assert_equals(actual, expected)
def test_hgrid_dummy_node_urls(self): node_settings = self.node_settings user = Auth(self.project.creator) node = self.project expected = { 'isPointer': False, 'provider': 's3', 'addonFullname': node_settings.config.full_name, 'iconUrl': node_settings.config.icon_url, 'name': 'Amazon Simple Storage Service: {0}'.format(node_settings.bucket), 'kind': 'folder', 'permissions': { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, }, 'urls': { 'fetch': node.api_url_for('s3_hgrid_data_contents'), 'upload': node.api_url_for('s3_upload'), }, 'accept': { 'maxSize': node_settings.config.max_file_size, 'acceptedFiles': node_settings.config.accept_extensions }, 'isAddonRoot': True, 'extra': None, 'buttons': None, 'nodeId': node._id, 'nodeUrl': node.url, 'nodeApiUrl': node.api_url, } permissions = { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, } assert_equals( rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions), expected)
def test_build_addon_root_for_anonymous_vols_shows_path(self): private_link = PrivateLinkFactory() private_link.nodes.add(self.project) private_link.save() project_viewer = UserFactory() result = rubeus.build_addon_root(self.node_settings, self.node_settings.bucket, user=project_viewer, private_key=private_link.key) assert result['name'] == 'Amazon S3: {0}'.format( self.node_settings.bucket)
def osf_storage_root(addon_config, node_settings, auth, **kwargs): """Build HGrid JSON for root node. Note: include node URLs for client-side URL creation for uploaded files. """ node = node_settings.owner root = rubeus.build_addon_root( node_settings=node_settings, name='', permissions=auth, user=auth.user, nodeUrl=node.url, nodeApiUrl=node.api_url, ) return [root]
def _root_folder(node_settings, auth, **kwargs): """Return the Rubeus/HGrid-formatted response for the root folder only.""" # Quit if node settings does not have authentication if not node_settings.has_auth or not node_settings.folder_id: return None node = node_settings.owner root = rubeus.build_addon_root( node_settings=node_settings, name=node_settings.fetch_folder_name(), permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, ) return [root]
def test_build_addon_root_for_anonymous_vols_hides_path(self): private_anonymous_link = PrivateLinkFactory(anonymous=True) private_anonymous_link.nodes.add(self.project) private_anonymous_link.save() project_viewer = UserFactory() result = rubeus.build_addon_root( self.node_settings, self.node_settings.bucket, user=project_viewer, private_key=private_anonymous_link.key ) assert result['name'] == 'Amazon S3'
def googledrive_addon_folder(node_settings, auth, **kwargs): """Return the Rubeus/HGrid-formatted response for the root folder only.""" # Quit if node settings does not have authentication if not node_settings.has_auth or not node_settings.folder_id: return None node = node_settings.owner root = rubeus.build_addon_root( node_settings=node_settings, name=node_settings.folder_name, permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, ) return [root]
def test_osf_storage_root(self): auth = Auth(self.project.creator) result = views.osf_storage_root(self.node_settings, auth=auth) node = self.project expected = rubeus.build_addon_root( node_settings=self.node_settings, name='', permissions=auth, user=auth.user, nodeUrl=node.url, nodeApiUrl=node.api_url, ) root = result[0] assert_equal(root, expected)
def _root_folder(node_settings, auth, **kwargs): """Return the Rubeus/HGrid-formatted response for the root folder only.""" # Quit if node settings does not have authentication if not node_settings.has_auth or not node_settings.folder_id: return None node = node_settings.owner root = rubeus.build_addon_root( node_settings=node_settings, name=node_settings.fetch_folder_name(), permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, private_key=kwargs.get('view_only', None), ) return [root]
def osf_storage_root(node_settings, auth, **kwargs): """Build HGrid JSON for root node. Note: include node URLs for client-side URL creation for uploaded files. """ node = node_settings.owner root = rubeus.build_addon_root( node_settings=node_settings, name='', permissions=auth, urls={ 'upload': node.api_url_for('osf_storage_request_upload_url'), 'fetch': node.api_url_for('osf_storage_hgrid_contents'), }, nodeUrl=node.url, nodeApiUrl=node.api_url, ) return [root]
def figshare_hgrid_data(node_settings, auth, parent=None, **kwargs): node = node_settings.owner if node_settings.figshare_type == 'project': item = Figshare.from_settings(node_settings.user_settings).project(node_settings, node_settings.figshare_id) else: item = Figshare.from_settings(node_settings.user_settings).article(node_settings, node_settings.figshare_id) if not node_settings.figshare_id or not node_settings.has_auth or not item: return #TODO Test me #Throw error if neither node_settings.figshare_title = item.get('title') or item['items'][0]['title'] node_settings.save() return [ rubeus.build_addon_root( node_settings, u'{0}:{1}'.format(node_settings.figshare_title or 'Unnamed', node_settings.figshare_id), permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, ) ]
def test_hgrid_dummy_overrides(self): node_settings = self.node_settings node_settings.config.urls = None node = self.project user = Auth(self.project.creator) rv = { 'isPointer': False, 'addon': 's3', 'addonFullname': node_settings.config.full_name, 'iconUrl': node_settings.config.icon_url, 'name': 'Amazon Simple Storage Service: {0}'.format(node_settings.bucket), 'kind': 'folder', 'permissions': { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, }, 'urls': {}, 'accept': { 'maxSize': node_settings.config.max_file_size, 'acceptedFiles': node_settings.config.accept_extensions }, 'isAddonRoot': True, 'extra': None, 'buttons': None, } permissions = { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, } assert_equals( rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions, urls={}), rv)
def figshare_root_folder(node_settings, auth, **kwargs): """Return the Rubeus/HGrid-formatted response for the root folder only. Identical to the generic_views.root_folder except adds root_folder_type to exported data. Fangorn needs root_folder_type to decide whether to display the 'Create Folder' button. """ # Quit if node settings does not have authentication if not node_settings.has_auth or not node_settings.folder_id: return None node = node_settings.owner return [rubeus.build_addon_root( node_settings=node_settings, name=node_settings.fetch_folder_name(), permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, rootFolderType=node_settings.folder_path, private_key=kwargs.get('view_only', None), )]
def dropbox_addon_folder(node_settings, auth, **kwargs): """Return the Rubeus/HGrid-formatted response for the root folder only.""" # Quit if node settings does not have authentication if not node_settings.has_auth or not node_settings.folder: return None node = node_settings.owner path = clean_path(node_settings.folder) root = rubeus.build_addon_root( node_settings=node_settings, name=node_settings.folder, permissions=auth, nodeUrl=node.url, nodeApiUrl=node.api_url, urls={ 'upload': node.api_url_for('dropbox_upload', path=path), 'fetch': node.api_url_for('dropbox_hgrid_data_contents', path=path) } ) return [root]
def test_hgrid_dummy(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) # FIXME: These tests are very brittle. expected = { 'isPointer': False, 'provider': 's3', 'addonFullname': node_settings.config.full_name, 'iconUrl': node_settings.config.icon_url, 'name': 'Amazon S3: {0}'.format( node_settings.bucket ), 'kind': 'folder', 'accept': { 'maxSize': node_settings.config.max_file_size, 'acceptedFiles': node_settings.config.accept_extensions }, 'isAddonRoot': True, 'extra': None, 'buttons': None, 'nodeId': node._id, 'nodeUrl': node.url, 'nodeApiUrl': node.api_url, } permissions = { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, } expected['permissions'] = permissions actual = rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions) assert actual['urls']['fetch'] assert actual['urls']['upload'] del actual['urls'] assert_equals(actual, expected)
def test_hgrid_dummy_overrides(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) expected = { 'isPointer': False, 'provider': 's3', 'addonFullname': node_settings.config.full_name, 'iconUrl': node_settings.config.icon_url, 'name': 'Amazon S3: {0}'.format( node_settings.bucket ), 'kind': 'folder', 'permissions': { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, }, 'urls': {}, 'accept': { 'maxSize': node_settings.config.max_file_size, 'acceptedFiles': node_settings.config.accept_extensions }, 'isAddonRoot': True, 'extra': None, 'buttons': None, 'nodeId': node._id, 'nodeUrl': node.url, 'nodeApiUrl': node.api_url, } permissions = { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, } assert_equal( rubeus.build_addon_root( node_settings, node_settings.bucket, permissions=permissions, urls={} ), expected )
def _weko_root_folder(node_addon, auth, **kwargs): # Quit if no indices linked if not node_addon.complete: return [] connection = client.connect_from_settings(weko_settings, node_addon) index = client.get_index_by_id(connection, node_addon.index_id) if index is None: return [] return [ rubeus.build_addon_root( node_addon, node_addon.index_title, permissions={ 'view': True, 'edit': True }, private_key=kwargs.get('view_only', None), ) ]
def test_hgrid_dummy_fail(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) rv = { "isPointer": False, "addon": "s3", "addonFullname": node_settings.config.full_name, "iconUrl": node_settings.config.icon_url, "name": "Amazon S3: {0}".format(node_settings.bucket), "kind": "folder", "permissions": {"view": node.can_view(user), "edit": node.can_edit(user) and not node.is_registration}, "urls": {"fetch": node.api_url + "s3/hgrid/", "upload": node.api_url + "s3/upload/"}, "accept": { "maxSize": node_settings.config.max_file_size, "acceptedFiles": node_settings.config.accept_extensions, }, "isAddonRoot": True, "nodeId": node._id, "nodeUrl": node.url, "nodeApiUrl": node.api_url, } permissions = {"view": node.can_view(user), "edit": node.can_edit(user) and not node.is_registration} assert_not_equals(rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions), rv)
def test_hgrid_dummy_fail(self): node_settings = self.node_settings node = self.project user = Auth(self.project.creator) rv = { 'isPointer': False, 'addon': 's3', 'addonFullname': node_settings.config.full_name, 'iconUrl': node_settings.config.icon_url, 'name': 'Amazon S3: {0}'.format(node_settings.bucket), 'kind': 'folder', 'permissions': { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, }, 'urls': { 'fetch': node.api_url + 's3/hgrid/', 'upload': node.api_url + 's3/upload/' }, 'accept': { 'maxSize': node_settings.config.max_file_size, 'acceptedFiles': node_settings.config.accept_extensions }, 'isAddonRoot': True, 'nodeId': node._id, 'nodeUrl': node.url, 'nodeApiUrl': node.api_url, } permissions = { 'view': node.can_view(user), 'edit': node.can_edit(user) and not node.is_registration, } assert_not_equals( rubeus.build_addon_root(node_settings, node_settings.bucket, permissions=permissions), rv)
def bitbucket_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = BitbucketClient( access_token=node_settings.external_account.oauth_key) node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): repo = connection.repo(node_settings.user, node_settings.repo) if not repo: # TODO: Add warning message logger.error('Could not access Bitbucket repo') return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except (NotFoundError, Exception): # TODO: Show an alert or change Bitbucket configuration? logger.error('Bitbucket repo not found') return ref = None if branch is None else ref_to_params(branch, sha) name_tpl = '{user}/{repo}'.format(user=node_settings.user, repo=node_settings.repo) permissions = { 'edit': False, 'view': True, 'private': node_settings.is_private } urls = { 'upload': None, 'fetch': node_settings.owner.api_url + 'bitbucket/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'bitbucket/hgrid/root/', 'zip': node_settings.owner.api_url + 'bitbucket/zipball/' + (ref or ''), 'repo': 'https://bitbucket.com/{0}/{1}/branch/'.format(node_settings.user, node_settings.repo) } branch_names = [each['name'] for each in branches] if not branch_names: branch_names = [ branch ] # if repo un-init-ed then still add default branch to list of branches return [ rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=branch_names, defaultBranch=branch, private_key=kwargs.get('view_only', None), ) ]
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 github_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitHub.from_settings(node_settings.user_settings) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): try: repo = connection.repo(node_settings.user, node_settings.repo) except NotFoundError: # TODO: Test me @jmcarp # TODO: Add warning message logger.error('Could not access GitHub repo') return None if repo.private: return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except NotFoundError: # TODO: Show an alert or change GitHub configuration? logger.error('GitHub repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions( node_settings, auth, connection, branch, sha, repo=repo, ) name_append = github_branch_widget(branches, owner=node_settings.user, repo=node_settings.repo, branch=branch, sha=sha) else: ref = None can_edit = False name_append = None name_tpl = '{user}/{repo}'.format( user=node_settings.user, repo=node_settings.repo ) permissions = { 'edit': can_edit, 'view': True } urls = { 'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''), 'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'github/hgrid/root/', 'zip': node_settings.owner.api_url + 'github/zipball/' + (ref or ''), 'repo': github_repo_url(owner=node_settings.user, repo=node_settings.repo, branch=branch) } buttons = [ rubeus.build_addon_button('<i class="icon-download-alt"></i>', 'githubDownloadZip', "Download Zip"), rubeus.build_addon_button('<i class="icon-external-link"></i>', 'githubVisitRepo', "Visit Repository"), ] return [rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, extra=name_append, buttons=buttons, )]
def github_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitHubClient(external_account=node_settings.external_account) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): try: repo = connection.repo(node_settings.user, node_settings.repo) except NotFoundError: # TODO: Test me @jmcarp # TODO: Add warning message logger.error('Could not access GitHub repo') return None if repo.private: return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except (NotFoundError, GitHubError): # TODO: Show an alert or change GitHub configuration? logger.error('GitHub repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions( node_settings, auth, connection, branch, sha, repo=repo, ) else: ref = None can_edit = False name_tpl = '{user}/{repo}'.format( user=node_settings.user, repo=node_settings.repo ) permissions = { 'edit': can_edit, 'view': True, 'private': node_settings.is_private } urls = { 'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''), 'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'github/hgrid/root/', 'zip': node_settings.owner.api_url + 'github/zipball/' + (ref or ''), 'repo': "https://github.com/{0}/{1}/tree/{2}".format(node_settings.user, node_settings.repo, branch) } branch_names = [each.name for each in branches] if not branch_names: branch_names = [branch] # if repo un-init-ed then still add default branch to list of branches return [rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=branch_names, defaultBranch=branch, )]
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, 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 try: host_custom_publish_text = client.get_custom_publish_text(connection) except OperationFailedError: host_custom_publish_text = '' 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, hostCustomPublishText=host_custom_publish_text, private_key=kwargs.get('view_only', None), )]
def github_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitHub.from_settings(node_settings.user_settings) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): try: repo = connection.repo(node_settings.user, node_settings.repo) except NotFoundError: # TODO: Test me @jmcarp # TODO: Add warning message logger.error('Could not access GitHub repo') return None if repo.private: return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except NotFoundError: # TODO: Show an alert or change GitHub configuration? logger.error('GitHub repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions( node_settings, auth, connection, branch, sha, repo=repo, ) else: ref = None can_edit = False name_tpl = '{user}/{repo}'.format(user=node_settings.user, repo=node_settings.repo) permissions = {'edit': can_edit, 'view': True} urls = { 'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''), 'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'github/hgrid/root/', 'zip': node_settings.owner.api_url + 'github/zipball/' + (ref or ''), 'repo': github_repo_url(owner=node_settings.user, repo=node_settings.repo, branch=branch) } return [ rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=[each.name for each in branches], defaultBranch=branch, ) ]
def github_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitHubClient(external_account=node_settings.external_account) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): try: repo = connection.repo(node_settings.user, node_settings.repo) except NotFoundError: logger.error('Could not access GitHub repo') return None except GitHubError: return if repo.private: return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get('branch'), sha=kwargs.get('sha'), connection=connection, ) except (NotFoundError, GitHubError): # TODO: Show an alert or change GitHub configuration? logger.error('GitHub repo not found') return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions( node_settings, auth, connection, branch, sha, repo=repo, ) else: ref = None can_edit = False name_tpl = '{user}/{repo}'.format( user=node_settings.user, repo=node_settings.repo ) permissions = { 'edit': can_edit, 'view': True, 'private': node_settings.is_private } urls = { 'upload': node_settings.owner.api_url + 'github/file/' + (ref or ''), 'fetch': node_settings.owner.api_url + 'github/hgrid/' + (ref or ''), 'branch': node_settings.owner.api_url + 'github/hgrid/root/', 'zip': node_settings.owner.api_url + 'github/zipball/' + (ref or ''), 'repo': 'https://github.com/{0}/{1}/tree/{2}'.format(node_settings.user, node_settings.repo, branch) } branch_names = [each.name for each in branches] if not branch_names: branch_names = [branch] # if repo un-init-ed then still add default branch to list of branches return [rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=branch_names, defaultBranch=branch, private_key=kwargs.get('view_only', None), )]
def github_hgrid_data(node_settings, auth, **kwargs): # Quit if no repo linked if not node_settings.complete: return connection = GitHub.from_settings(node_settings.user_settings) # Initialize repo here in the event that it is set in the privacy check # below. This potentially saves an API call in _check_permissions, below. repo = None # Quit if privacy mismatch and not contributor node = node_settings.owner if node.is_public and not node.is_contributor(auth.user): try: repo = connection.repo(node_settings.user, node_settings.repo) except NotFoundError: # TODO: Test me @jmcarp # TODO: Add warning message logger.error("Could not access GitHub repo") return None if repo.private: return None try: branch, sha, branches = get_refs( node_settings, branch=kwargs.get("branch"), sha=kwargs.get("sha"), connection=connection ) except (NotFoundError, GitHubError): # TODO: Show an alert or change GitHub configuration? logger.error("GitHub repo not found") return if branch is not None: ref = ref_to_params(branch, sha) can_edit = check_permissions(node_settings, auth, connection, branch, sha, repo=repo) else: ref = None can_edit = False name_tpl = "{user}/{repo}".format(user=node_settings.user, repo=node_settings.repo) permissions = {"edit": can_edit, "view": True} urls = { "upload": node_settings.owner.api_url + "github/file/" + (ref or ""), "fetch": node_settings.owner.api_url + "github/hgrid/" + (ref or ""), "branch": node_settings.owner.api_url + "github/hgrid/root/", "zip": node_settings.owner.api_url + "github/zipball/" + (ref or ""), "repo": github_repo_url(owner=node_settings.user, repo=node_settings.repo, branch=branch), } branch_names = [each.name for each in branches] if not branch_names: branch_names = [branch] # if repo un-init-ed then still add default branch to list of branches return [ rubeus.build_addon_root( node_settings, name_tpl, urls=urls, permissions=permissions, branches=branch_names, defaultBranch=branch ) ]