Exemple #1
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'
    ]
Exemple #2
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']
Exemple #3
0
 def credentials_are_valid(self, user_settings, client):
     if user_settings:
         client = client or BoxClient(
             user_settings.external_accounts[0].oauth_key)
         try:
             client.get_user_info()
         except (BoxClientException, IndexError):
             return False
     return True
Exemple #4
0
    def serialize_settings(self, node_settings, current_user, client=None):
        """View helper that returns a dictionary representation of a
        BoxNodeSettings record. Provides the return value for the
        box config endpoints.
        """
        valid_credentials = True
        user_settings = node_settings.user_settings
        self.node_settings = node_settings
        current_user_settings = current_user.get_addon('box')
        user_is_owner = user_settings is not None and user_settings.owner == current_user

        if user_settings:
            try:
                client = client or BoxClient(
                    user_settings.external_accounts[0].oauth_key)
                client.get_user_info()
            except (BoxClientException, IndexError):
                valid_credentials = False

        result = {
            'userIsOwner':
            user_is_owner,
            'nodeHasAuth':
            node_settings.has_auth,
            'urls':
            self.addon_serialized_urls,
            'validCredentials':
            valid_credentials,
            'userHasAuth':
            current_user_settings is not None
            and current_user_settings.has_auth,
        }

        if node_settings.has_auth:
            # Add owner's profile URL
            result['urls']['owner'] = web_url_for('profile_view_id',
                                                  uid=user_settings.owner._id)
            result['ownerName'] = user_settings.owner.fullname
            # Show available folders
            # path = node_settings.folder

            if node_settings.folder_id is None:
                result['folder'] = {'name': None, 'path': None}
            elif valid_credentials:
                path = node_settings.fetch_full_folder_path()

                result['folder'] = {
                    'path':
                    path,
                    'name':
                    path.replace('All Files', '', 1)
                    if path != 'All Files' else '/ (Full Box)'
                }
        return result
Exemple #5
0
    def credentials_are_valid(self, user_settings, client):
        from website.addons.box.model import Box  # Avoid circular import
        if self.node_settings.has_auth:
            if Box(self.node_settings.external_account).refresh_oauth_key():
                return True

        if user_settings:
            client = client or BoxClient(user_settings.external_accounts[0].oauth_key)
            try:
                client.get_user_info()
            except (BoxClientException, IndexError):
                return False
        return True