示例#1
0
def box_get_config(node_addon, auth, **kwargs):
    """API that returns the serialized node settings."""
    if node_addon.external_account:
        refresh_oauth_key(node_addon.external_account)
    return {
        'result': BoxSerializer().serialize_settings(node_addon, auth.user),
    }
示例#2
0
文件: views.py 项目: Alpani/osf.io
def box_get_config(node_addon, auth, **kwargs):
    """API that returns the serialized node settings."""
    if node_addon.external_account:
        refresh_oauth_key(node_addon.external_account)
    return {
        'result': BoxSerializer().serialize_settings(node_addon, auth.user),
    }
示例#3
0
文件: model.py 项目: Alpani/osf.io
 def serialize_waterbutler_credentials(self):
     if not self.has_auth:
         raise exceptions.AddonError('Addon is not authorized')
     try:
         refresh_oauth_key(self.external_account)
         return {'token': self.external_account.oauth_key}
     except BoxClientException as error:
         raise HTTPError(error.status_code, data={'message_long': error.message})
示例#4
0
 def serialize_waterbutler_credentials(self):
     if not self.has_auth:
         raise exceptions.AddonError('Addon is not authorized')
     try:
         refresh_oauth_key(self.external_account)
         return {'token': self.external_account.oauth_key}
     except BoxClientException as error:
         raise HTTPError(error.status_code, data={'message_long': error.message})
示例#5
0
文件: views.py 项目: Alpani/osf.io
def box_folder_list(node_addon, **kwargs):
    """Returns a list of folders in Box"""
    if not node_addon.has_auth:
        raise HTTPError(http.FORBIDDEN)

    node = node_addon.owner
    folder_id = request.args.get('folderId')

    if folder_id is None:
        return [{
            'id': '0',
            'path': 'All Files',
            'addon': 'box',
            'kind': 'folder',
            'name': '/ (Full Box)',
            'urls': {
                'folders': node.api_url_for('box_folder_list', folderId=0),
            }
        }]

    try:
        refresh_oauth_key(node_addon.external_account)
        client = BoxClient(node_addon.external_account.oauth_key)
    except BoxClientException:
        raise HTTPError(http.FORBIDDEN)

    try:
        metadata = client.get_folder(folder_id)
    except BoxClientException:
        raise HTTPError(http.NOT_FOUND)
    except MaxRetryError:
        raise HTTPError(http.BAD_REQUEST)

    # Raise error if folder was deleted
    if metadata.get('is_deleted'):
        raise HTTPError(http.NOT_FOUND)

    folder_path = '/'.join(
        [
            x['name']
            for x in metadata['path_collection']['entries']
        ] + [metadata['name']]
    )

    return [
        {
            'addon': 'box',
            'kind': 'folder',
            'id': item['id'],
            'name': item['name'],
            'path': os.path.join(folder_path, item['name']),
            'urls': {
                'folders': node.api_url_for('box_folder_list', folderId=item['id']),
            }
        }
        for item in metadata['item_collection']['entries']
        if item['type'] == 'folder'
    ]
示例#6
0
def box_folder_list(node_addon, **kwargs):
    """Returns a list of folders in Box"""
    if not node_addon.has_auth:
        raise HTTPError(http.FORBIDDEN)

    node = node_addon.owner
    folder_id = request.args.get('folderId')

    if folder_id is None:
        return [{
            'id': '0',
            'path': 'All Files',
            'addon': 'box',
            'kind': 'folder',
            'name': '/ (Full Box)',
            'urls': {
                'folders': node.api_url_for('box_folder_list', folderId=0),
            }
        }]

    try:
        refresh_oauth_key(node_addon.external_account)
        client = BoxClient(node_addon.external_account.oauth_key)
    except BoxClientException:
        raise HTTPError(http.FORBIDDEN)

    try:
        metadata = client.get_folder(folder_id)
    except BoxClientException:
        raise HTTPError(http.NOT_FOUND)
    except MaxRetryError:
        raise HTTPError(http.BAD_REQUEST)

    # Raise error if folder was deleted
    if metadata.get('is_deleted'):
        raise HTTPError(http.NOT_FOUND)

    folder_path = '/'.join(
        [x['name']
         for x in metadata['path_collection']['entries']] + [metadata['name']])

    return [{
        'addon': 'box',
        'kind': 'folder',
        'id': item['id'],
        'name': item['name'],
        'path': os.path.join(folder_path, item['name']),
        'urls': {
            'folders': node.api_url_for('box_folder_list',
                                        folderId=item['id']),
        }
    } for item in metadata['item_collection']['entries']
            if item['type'] == 'folder']
def main(delta, dry_run):
    for record in get_targets(delta):
        logger.info(
            'Refreshing tokens on record {0}; expires at {1}'.format(
                record._id,
                record.expires_at.strftime('%c')
            )
        )
        if not dry_run:
            try:
                refresh_oauth_key(record, force=True)
            except AddonError as ex:
                logger.error(ex.message)
示例#8
0
文件: model.py 项目: Alpani/osf.io
    def _update_folder_data(self):
        if self.folder_id is None:
            return None

        if not self._folder_data:
            try:
                refresh_oauth_key(self.external_account)
                client = BoxClient(self.external_account.oauth_key)
                self._folder_data = client.get_folder(self.folder_id)
            except BoxClientException:
                return

            self.folder_name = self._folder_data['name']
            self.folder_path = '/'.join(
                [x['name'] for x in self._folder_data['path_collection']['entries']]
                + [self._folder_data['name']]
            )
            self.save()
示例#9
0
    def _update_folder_data(self):
        if self.folder_id is None:
            return None

        if not self._folder_data:
            try:
                refresh_oauth_key(self.external_account)
                client = BoxClient(self.external_account.oauth_key)
                self._folder_data = client.get_folder(self.folder_id)
            except BoxClientException:
                return

            self.folder_name = self._folder_data['name']
            self.folder_path = '/'.join(
                [x['name'] for x in self._folder_data['path_collection']['entries']]
                + [self._folder_data['name']]
            )
            self.save()