Exemple #1
0
    def __init__(self, *, parent=None, con=None, **kwargs):
        """ A Sharepoint site List

        :param parent: parent object
        :type parent: Sharepoint
        :param Connection con: connection to use if no parent specified
        :param Protocol protocol: protocol to use if no parent specified
         (kwargs)
        :param str main_resource: use this resource instead of parent resource
         (kwargs)
        """
        assert parent or con, 'Need a parent or a connection'
        self.con = parent.con if parent else con

        cloud_data = kwargs.get(self._cloud_data_key, {})

        self.object_id = cloud_data.get('id')

        # Choose the main_resource passed in kwargs over parent main_resource
        main_resource = (kwargs.pop('main_resource', None) or getattr(
            parent, 'main_resource', None) if parent else None)

        # prefix with the current known site
        resource_prefix = 'sites/{site_id}'.format(site_id=self.object_id)
        main_resource = '{}{}'.format(main_resource, resource_prefix)

        super().__init__(
            protocol=parent.protocol if parent else kwargs.get('protocol'),
            main_resource=main_resource)

        self.root = 'root' in cloud_data  # True or False
        # Fallback to manual site
        self.name = cloud_data.get(self._cc('name'), kwargs.get('name', ''))
        self.display_name = cloud_data.get(self._cc('displayName'), '')
        if not self.name:
            self.name = self.display_name
        self.description = cloud_data.get(self._cc('description'), '')
        self.web_url = cloud_data.get(self._cc('webUrl'))

        created = cloud_data.get(self._cc('createdDateTime'), None)
        modified = cloud_data.get(self._cc('lastModifiedDateTime'), None)
        local_tz = self.protocol.timezone
        self.created = parse(created).astimezone(local_tz) if created else None
        self.modified = parse(modified).astimezone(
            local_tz) if modified else None

        # site storage to access Drives and DriveItems
        self.site_storage = Storage(
            parent=self, main_resource='/sites/{id}'.format(id=self.object_id))
Exemple #2
0
    def storage(self, *, resource=None):
        """ Get an instance to handle file storage (OneDrive / Sharepoint)
        for the specified account resource

        :param str resource: Custom resource to be used in this drive object
         (Defaults to parent main_resource)
        :return: a representation of OneDrive File Storage
        :rtype: Storage
        :raises RuntimeError: if protocol doesn't support the feature
        """
        if not isinstance(self.protocol, MSGraphProtocol):
            # TODO: Custom protocol accessing OneDrive/Sharepoint Api fails here
            raise RuntimeError(
                'Drive options only works on Microsoft Graph API')

        return Storage(parent=self, main_resource=resource)
Exemple #3
0
class Site(ApiComponent):
    """ A Sharepoint Site """

    _endpoints = {
        'get_subsites': '/sites',
        'get_lists': '/lists',
        'get_list_by_name': '/lists/{display_name}'
    }
    list_constructor = SharepointList

    def __init__(self, *, parent=None, con=None, **kwargs):
        """ A Sharepoint site List

        :param parent: parent object
        :type parent: Sharepoint
        :param Connection con: connection to use if no parent specified
        :param Protocol protocol: protocol to use if no parent specified
         (kwargs)
        :param str main_resource: use this resource instead of parent resource
         (kwargs)
        """
        assert parent or con, 'Need a parent or a connection'
        self.con = parent.con if parent else con

        cloud_data = kwargs.get(self._cloud_data_key, {})

        self.object_id = cloud_data.get('id')

        # Choose the main_resource passed in kwargs over parent main_resource
        main_resource = (kwargs.pop('main_resource', None) or getattr(
            parent, 'main_resource', None) if parent else None)

        # prefix with the current known site
        resource_prefix = 'sites/{site_id}'.format(site_id=self.object_id)
        main_resource = '{}{}'.format(main_resource, resource_prefix)

        super().__init__(
            protocol=parent.protocol if parent else kwargs.get('protocol'),
            main_resource=main_resource)

        self.root = 'root' in cloud_data  # True or False
        # Fallback to manual site
        self.name = cloud_data.get(self._cc('name'), kwargs.get('name', ''))
        self.display_name = cloud_data.get(self._cc('displayName'), '')
        if not self.name:
            self.name = self.display_name
        self.description = cloud_data.get(self._cc('description'), '')
        self.web_url = cloud_data.get(self._cc('webUrl'))

        created = cloud_data.get(self._cc('createdDateTime'), None)
        modified = cloud_data.get(self._cc('lastModifiedDateTime'), None)
        local_tz = self.protocol.timezone
        self.created = parse(created).astimezone(local_tz) if created else None
        self.modified = parse(modified).astimezone(
            local_tz) if modified else None

        # site storage to access Drives and DriveItems
        self.site_storage = Storage(
            parent=self, main_resource='/sites/{id}'.format(id=self.object_id))

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return 'Site: {}'.format(self.name)

    def get_default_document_library(self, request_drive=False):
        """ Returns the default document library of this site (Drive instance)

        :param request_drive: True will make an api call to retrieve
         the drive data
        :rtype: Drive
        """
        return self.site_storage.get_default_drive(request_drive=request_drive)

    def get_document_library(self, drive_id):
        """ Returns a Document Library (a Drive instance)

        :param drive_id: the drive_id to be retrieved.
        :rtype: Drive
        """
        return self.site_storage.get_drive(drive_id=drive_id)

    def list_document_libraries(self,
                                limit=None,
                                *,
                                query=None,
                                order_by=None,
                                batch=None):
        """ Returns a collection of document libraries for this site
        (a collection of Drive instances)

        :param int limit: max no. of items to get. Over 999 uses batch.
        :param query: applies a OData filter to the request
        :type query: Query or str
        :param order_by: orders the result set based on this condition
        :type order_by: Query or str
        :param int batch: batch size, retrieves items in
         batches allowing to retrieve more items than the limit.
        :return: list of items in this folder
        :rtype: list[Drive] or Pagination
        """
        return self.site_storage.get_drives(limit=limit,
                                            query=query,
                                            order_by=order_by,
                                            batch=batch)

    def get_subsites(self):
        """ Returns a list of subsites defined for this site

        :rtype: list[Site]
        """
        url = self.build_url(
            self._endpoints.get('get_subsites').format(id=self.object_id))

        response = self.con.get(url)
        if not response:
            return []

        data = response.json()

        # Everything received from cloud must be passed as self._cloud_data_key
        return [
            self.__class__(parent=self, **{self._cloud_data_key: site})
            for site in data.get('value', [])
        ]

    def get_lists(self):
        """ Returns a collection of lists within this site

        :rtype: list[SharepointList]
        """
        url = self.build_url(self._endpoints.get('get_lists'))

        response = self.con.get(url)
        if not response:
            return []

        data = response.json()

        return [
            self.list_constructor(parent=self, **{self._cloud_data_key: lst})
            for lst in data.get('value', [])
        ]

    def get_list_by_name(self, display_name):
        """ Returns a sharepoint list based on the display name of the list 
        """

        if not display_name:
            raise ValueError('Must provide a valid list display name')

        url = self.build_url(
            self._endpoints.get('get_list_by_name').format(
                display_name=display_name))

        response = self.con.get(url)
        if not response:
            return []

        data = response.json()

        return [
            self.list_constructor(parent=self, **{self._cloud_data_key: lst})
            for lst in data.get('value', [])
        ]
Exemple #4
0
class Site(ApiComponent):
    """ A Sharepoint Site """

    _endpoints = {'get_subsites': '/sites', 'get_lists': '/lists'}
    list_constructor = SharepointList

    def __init__(self, *, parent=None, con=None, **kwargs):
        assert parent or con, 'Need a parent or a connection'
        self.con = parent.con if parent else con

        cloud_data = kwargs.get(self._cloud_data_key, {})

        self.object_id = cloud_data.get('id')

        # Choose the main_resource passed in kwargs over the parent main_resource
        main_resource = kwargs.pop('main_resource', None) or getattr(
            parent, 'main_resource', None) if parent else None

        # prefix with the current known site
        resource_prefix = 'sites/{site_id}'.format(site_id=self.object_id)
        main_resource = '{}{}'.format(main_resource, resource_prefix)

        super().__init__(
            protocol=parent.protocol if parent else kwargs.get('protocol'),
            main_resource=main_resource)

        self.root = 'root' in cloud_data  # True or False
        self.name = cloud_data.get(self._cc('name'),
                                   kwargs.get('name',
                                              ''))  # Fallback to manual site
        self.display_name = cloud_data.get(self._cc('displayName'), '')
        if not self.name:
            self.name = self.display_name
        self.description = cloud_data.get(self._cc('description'), '')
        self.web_url = cloud_data.get(self._cc('webUrl'))

        created = cloud_data.get(self._cc('createdDateTime'), None)
        modified = cloud_data.get(self._cc('lastModifiedDateTime'), None)
        local_tz = self.protocol.timezone
        self.created = parse(created).astimezone(local_tz) if created else None
        self.modified = parse(modified).astimezone(
            local_tz) if modified else None

        # site storage to access Drives and DriveItems
        self.site_storage = Storage(
            parent=self, main_resource='/sites/{id}'.format(id=self.object_id))

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return 'Site: {}'.format(self.name)

    def get_default_document_library(self, request_drive=False):
        """
        Returns the default document library of this site (a Drive instance)
        :param request_drive: True will make an api call to retrieve the drive data
        """
        return self.site_storage.get_default_drive(request_drive=request_drive)

    def get_document_library(self, drive_id):
        """
        Returns a Document Library (a Drive instance)
        :param drive_id: the drive_id to be retrieved.
        """
        return self.site_storage.get_drive(drive_id=drive_id)

    def list_document_libraries(self,
                                limit=None,
                                *,
                                query=None,
                                order_by=None,
                                batch=None):
        """ Returns a collection of document libraries for this site (a collection of Drive instances) """
        return self.site_storage.get_drives(limit=limit,
                                            query=query,
                                            order_by=order_by,
                                            batch=batch)

    def get_subsites(self):
        """ Returns a list of subsites defined for this site """
        url = self.build_url(
            self._endpoints.get('get_subsites').format(id=self.object_id))

        response = self.con.get(url)
        if not response:
            return []

        data = response.json()

        # Everything received from the cloud must be passed with self._cloud_data_key
        return [
            self.__class__(parent=self, **{self._cloud_data_key: site})
            for site in data.get('value', [])
        ]

    def get_lists(self):
        """ Returns a collection of lists within this site """
        url = self.build_url(self._endpoints.get('get_lists'))

        response = self.con.get(url)
        if not response:
            return []

        data = response.json()

        return [
            self.list_constructor(parent=self, **{self._cloud_data_key: lst})
            for lst in data.get('value', [])
        ]