예제 #1
0
파일: model.py 프로젝트: XTech2K/osf.io
 def _get_fileobj_child_metadata(self, filenode, user, cookie=None, version=None):
     kwargs = dict(
         provider=self.config.short_name,
         path=filenode.get('path', ''),
         node=self.owner,
         user=user,
         view_only=True,
     )
     if cookie:
         kwargs['cookie'] = cookie
     if version:
         kwargs['version'] = version
     metadata_url = waterbutler_url_for(
         'metadata',
         **kwargs
     )
     res = requests.get(metadata_url)
     if res.status_code != 200:
         # The Dataverse API returns a 404 if the dataset has no published files
         if res.status_code == http.NOT_FOUND and version == 'latest-published':
             return []
         raise HTTPError(res.status_code, data={
             'error': res.json(),
         })
     # TODO: better throttling?
     sleep(1.0 / 5.0)
     return res.json().get('data', [])
예제 #2
0
 def _get_fileobj_child_metadata(self,
                                 filenode,
                                 user,
                                 cookie=None,
                                 version=None):
     kwargs = dict(
         provider=self.config.short_name,
         path=filenode.get('path', ''),
         node=self.owner,
         user=user,
         view_only=True,
     )
     if cookie:
         kwargs['cookie'] = cookie
     if version:
         kwargs['version'] = version
     metadata_url = waterbutler_url_for('metadata', **kwargs)
     res = requests.get(metadata_url)
     if res.status_code != 200:
         # The Dataverse API returns a 404 if the dataset has no published files
         if res.status_code == http.NOT_FOUND and version == 'latest-published':
             return []
         raise HTTPError(res.status_code, data={
             'error': res.json(),
         })
     # TODO: better throttling?
     sleep(1.0 / 5.0)
     return res.json().get('data', [])
예제 #3
0
    def _test__get_file_tree(self, addon_short_name):
        requests_made = []
        def callback(request, uri, headers):
            path = request.querystring['path'][0]
            requests_made.append(path)
            return (200, headers, json.dumps(self.RESP_MAP[path]))

        for path in self.RESP_MAP.keys():
            url = waterbutler_url_for(
                'metadata',
                provider=addon_short_name,
                path=path,
                node=self.src,
                user=self.user,
                view_only=True,
                _internal=True,
            )
            httpretty.register_uri(httpretty.GET,
                                   url,
                                   body=callback,
                                   content_type='applcation/json')
        addon = self.src.get_or_add_addon(addon_short_name, auth=self.auth)
        root = {
            'path': '/',
            'name': '',
            'kind': 'folder',
        }
        file_tree = addon._get_file_tree(root, self.user)
        assert_equal(FILE_TREE, file_tree)
        assert_equal(requests_made, ['/', '/qwerty'])  # no requests made for files
예제 #4
0
 def _get_fileobj_child_metadata(self, filenode, user, cookie=None, version=None):
     kwargs = dict(
         provider=self.config.short_name,
         path=filenode.get('path', ''),
         node=self.owner,
         user=user,
         view_only=True,
     )
     if cookie:
         kwargs['cookie'] = cookie
     if version:
         kwargs['version'] = version
     metadata_url = waterbutler_url_for(
         'metadata',
         _internal=True,
         **kwargs
     )
     res = requests.get(metadata_url)
     if res.status_code != 200:
         raise HTTPError(res.status_code, data={
             'error': res.json(),
         })
     # TODO: better throttling?
     sleep(1.0 / 5.0)
     return res.json().get('data', [])
예제 #5
0
    def _test__get_file_tree(self, addon_short_name):
        requests_made = []
        def callback(request, uri, headers):
            path = request.querystring['path'][0]
            requests_made.append(path)
            return (200, headers, json.dumps(self.RESP_MAP[path]))

        for path in self.RESP_MAP.keys():
            url = waterbutler_url_for(
                'metadata',
                provider=addon_short_name,
                path=path,
                node=self.src,
                user=self.user,
                view_only=True,
            )
            httpretty.register_uri(httpretty.GET,
                                   url,
                                   body=callback,
                                   content_type='applcation/json')
        addon = self.src.get_or_add_addon(addon_short_name, auth=self.auth)
        root = {
            'path': '/',
            'name': '',
            'kind': 'folder',
        }
        file_tree = addon._get_file_tree(root, self.user)
        assert_equal(FILE_TREE, file_tree)
        assert_equal(requests_made, ['/', '/qwerty'])  # no requests made for files
예제 #6
0
    def _get_fileobj_child_metadata(self,
                                    filenode,
                                    user,
                                    cookie=None,
                                    version=None):
        kwargs = dict(
            provider=self.config.short_name,
            path=filenode.get('path', ''),
            node=self.owner,
            user=user,
            view_only=True,
        )
        if cookie:
            kwargs['cookie'] = cookie
        if version:
            kwargs['version'] = version
        metadata_url = waterbutler_url_for('metadata',
                                           _internal=True,
                                           **kwargs)

        res = requests.get(metadata_url)
        if res.status_code != 200:
            raise HTTPError(res.status_code, data={'error': res.json()})

        # TODO: better throttling?
        time.sleep(1.0 / 5.0)
        return res.json().get('data', [])
예제 #7
0
    def test_waterbutler_url_for(self):
        with self.app.test_request_context():
            url = waterbutler_url_for('upload', 'provider', 'path', mock.Mock(_id='_id'))

        assert_in('nid=_id', url)
        assert_in('/file?', url)
        assert_in('path=path', url)
        assert_in('provider=provider', url)
예제 #8
0
    def test_waterbutler_url_for(self):
        with self.app.test_request_context():
            url = waterbutler_url_for('upload', 'provider', 'path', mock.Mock(_id='_id'))

        assert_in('nid=_id', url)
        assert_in('/file?', url)
        assert_in('path=path', url)
        assert_in('provider=provider', url)
예제 #9
0
파일: utils.py 프로젝트: adlius/osf.io
def upload_attachment(user, node, attachment):
    attachment.seek(0)
    name = '/' + (attachment.filename or settings.MISSING_FILE_NAME)
    content = attachment.read()
    upload_url = util.waterbutler_url_for('upload', 'osfstorage', name, node, user=user, _internal=True)

    requests.put(
        upload_url,
        data=content,
    )
예제 #10
0
    def test_waterbutler_url_for_implicit_cookie(self):
        with self.app.test_request_context() as context:
            context.request.cookies = {settings.COOKIE_NAME: 'cookie'}
            url = waterbutler_url_for('upload', 'provider', 'path', mock.Mock(_id='_id'))

        assert_in('nid=_id', url)
        assert_in('/file?', url)
        assert_in('path=path', url)
        assert_in('cookie=cookie', url)
        assert_in('provider=provider', url)
예제 #11
0
def send_file(app, name, content_type, file_like, node, user):
    """Upload file to OSF."""
    file_like.seek(0)
    with app.test_request_context():
        upload_url = util.waterbutler_url_for('upload', 'osfstorage', name, node, user=user)
    requests.put(
        upload_url,
        data=file_like,
        headers={'Content-Type': content_type},
    )
예제 #12
0
def upload_attachment(user, node, attachment):
    attachment.seek(0)
    name = '/' + (attachment.filename or settings.MISSING_FILE_NAME)
    content = attachment.read()
    upload_url = util.waterbutler_url_for('upload', 'osfstorage', name, node, user=user)

    requests.put(
        upload_url,
        data=content,
    )
예제 #13
0
    def test_waterbutler_url_for_implicit_cookie(self):
        with self.app.test_request_context() as context:
            context.request.cookies = {settings.COOKIE_NAME: 'cookie'}
            url = waterbutler_url_for('upload', 'provider', 'path', mock.Mock(_id='_id'))

        assert_in('nid=_id', url)
        assert_in('/file?', url)
        assert_in('path=path', url)
        assert_in('cookie=cookie', url)
        assert_in('provider=provider', url)
예제 #14
0
파일: test_utils.py 프로젝트: adlius/osf.io
    def test_waterbutler_url_for_internal(self):
        settings.WATERBUTLER_INTERNAL_URL = 'http://1.2.3.4:7777'
        with self.app.test_request_context():
            url = waterbutler_url_for('upload', 'provider', 'path', mock.Mock(_id='_id'), _internal=True)

        assert_not_in(settings.WATERBUTLER_URL, url)
        assert_in(settings.WATERBUTLER_INTERNAL_URL, url)
        assert_in('nid=_id', url)
        assert_in('/file?', url)
        assert_in('path=path', url)
        assert_in('provider=provider', url)
예제 #15
0
파일: utils.py 프로젝트: dplorimer/osf
def send_file(app, name, content_type, file_like, node, user):
    """Upload file to OSF."""
    file_like.seek(0)
    with app.test_request_context():
        upload_url = util.waterbutler_url_for('upload',
                                              'osfstorage',
                                              name,
                                              node,
                                              user=user)
    requests.put(
        upload_url,
        data=file_like,
        headers={'Content-Type': content_type},
    )
예제 #16
0
    def test_waterbutler_url_for_internal(self):
        settings.WATERBUTLER_INTERNAL_URL = 'http://1.2.3.4:7777'
        with self.app.test_request_context():
            url = waterbutler_url_for('upload',
                                      'provider',
                                      'path',
                                      mock.Mock(_id='_id'),
                                      _internal=True)

        assert_not_in(settings.WATERBUTLER_URL, url)
        assert_in(settings.WATERBUTLER_INTERNAL_URL, url)
        assert_in('nid=_id', url)
        assert_in('/file?', url)
        assert_in('path=path', url)
        assert_in('provider=provider', url)
예제 #17
0
파일: __init__.py 프로젝트: cslzchen/osf.io
 def _get_fileobj_child_metadata(self, filenode, user, cookie=None, version=None):
     kwargs = dict(
         provider=self.config.short_name, path=filenode.get("path", ""), node=self.owner, user=user, view_only=True
     )
     if cookie:
         kwargs["cookie"] = cookie
     if version:
         kwargs["version"] = version
     metadata_url = waterbutler_url_for("metadata", **kwargs)
     res = requests.get(metadata_url)
     if res.status_code != 200:
         raise HTTPError(res.status_code, data={"error": res.json()})
     # TODO: better throttling?
     sleep(1.0 / 5.0)
     return res.json().get("data", [])