Esempio n. 1
0
def box_user_config_get(user_addon, auth, **kwargs):
    """View for getting a JSON representation of the logged-in user's
    Box user settings.
    """
    urls = {
        'create': api_url_for('box_oauth_start_user'),
        'delete': api_url_for('box_oauth_delete_user')
    }
    valid_credentials = True

    if user_addon.has_auth:
        try:
            client = get_client_from_user_settings(user_addon)
            client.get_user_info()
        except BoxClientException:
            valid_credentials = False

    return {
        'result': {
            'urls': urls,
            'boxName': user_addon.username,
            'userHasAuth': user_addon.has_auth,
            'validCredentials': valid_credentials,
            'nNodesAuthorized': len(user_addon.nodes_authorized),
        },
    }
Esempio n. 2
0
def serialize_settings(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
    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 get_client_from_user_settings(user_settings)
            client.get_user_info()
        except BoxClientException:
            valid_credentials = False

    result = {
        'userIsOwner':
        user_is_owner,
        'nodeHasAuth':
        node_settings.has_auth,
        'urls':
        serialize_urls(node_settings),
        '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
Esempio n. 3
0
    def _update_folder_data(self):
        if self.folder_id is None:
            return None

        if not self._folder_data:
            try:
                client = get_client_from_user_settings(self.user_settings)
                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.fetch_folder_name()]
            )
            self.save()
Esempio n. 4
0
    def _update_folder_data(self):
        if self.folder_id is None:
            return None

        if not self._folder_data:
            try:
                client = get_client_from_user_settings(self.user_settings)
                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.fetch_folder_name()]
            )
            self.save()
Esempio n. 5
0
def serialize_settings(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
    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 get_client_from_user_settings(user_settings)
            client.get_user_info()
        except BoxClientException:
            valid_credentials = False

    result = {
        'userIsOwner': user_is_owner,
        'nodeHasAuth': node_settings.has_auth,
        'urls': serialize_urls(node_settings),
        '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
Esempio n. 6
0
def serialize_settings(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
    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 get_client_from_user_settings(user_settings)
            client.get_user_info()
        except BoxClientException:
            valid_credentials = False

    result = {
        "userIsOwner": user_is_owner,
        "nodeHasAuth": node_settings.has_auth,
        "urls": serialize_urls(node_settings),
        "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
Esempio n. 7
0
 def test_get_client_from_user_settings(self):
     client = get_client_from_user_settings(self.user_settings)
     assert_true(isinstance(client, BoxClient))
Esempio n. 8
0
 def test_get_client_from_user_settings(self):
     client = get_client_from_user_settings(self.user_settings)
     assert_true(isinstance(client, BoxClient))