Skip to content

Commit

Permalink
Merge pull request googleapis#155 from AnoojNair/master
Browse files Browse the repository at this point in the history
Sharepoint features
  • Loading branch information
Alejandro Casanovas committed Dec 20, 2018
2 parents f4c2235 + b351023 commit bea5a84
Showing 1 changed file with 128 additions and 4 deletions.
132 changes: 128 additions & 4 deletions O365/sharepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,62 @@

log = logging.getLogger(__name__)

class SharepointListColumn(ApiComponent):
""" A Sharepoint List column within a SharepointList """

class SharepointListItem(ApiComponent):
_endpoints = {}

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

# 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

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

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

self.object_id = cloud_data.get('id')
self.column_group = cloud_data.get(self._cc('columnGroup'), None)
self.description = cloud_data.get(self._cc('description'), None)
self.display_name = cloud_data.get(self._cc('displayName'), None)
self.enforce_unique_values = cloud_data.get(self._cc('enforceUniqueValues'), None)
self.hidden = cloud_data.get(self._cc('hidden'), None)
self.indexed = cloud_data.get(self._cc('indexed'), None)
self.internal_name = cloud_data.get(self._cc('name'), None)
self.read_only = cloud_data.get(self._cc('readOnly'), None)
self.required = cloud_data.get(self._cc('required'), None)

# identify the sharepoint column type and set it - Graph api doesn't return the type for managed metadata and link column
if(cloud_data.get(self._cc('text'), None) != None):
self.field_type = 'text'
elif(cloud_data.get(self._cc('choice'), None) != None):
self.field_type = 'choice'
elif(cloud_data.get(self._cc('number'), None) != None):
self.field_type = 'number'
elif(cloud_data.get(self._cc('currency'), None) != None):
self.field_type = 'currency'
elif(cloud_data.get(self._cc('dateTime'), None) != None):
self.field_type = 'dateTime'
elif(cloud_data.get(self._cc('lookup'), None) != None):
self.field_type = 'lookup'
elif(cloud_data.get(self._cc('boolean'), None) != None):
self.field_type = 'boolean'
elif(cloud_data.get(self._cc('calculated'), None) != None):
self.field_type = 'calculated'
elif(cloud_data.get(self._cc('personOrGroup'), None) != None):
self.field_type = 'personOrGroup'
else:
self.field_type = None

def __repr__(self) :
return 'List Column: {0}-{1}'.format(self.display_name,self.field_type)


class SharepointListItem(ApiComponent):
_endpoints = {}

def __init__(self, *, parent=None, con=None, **kwargs):
""" A Sharepoint ListItem within a SharepointList
Expand All @@ -38,13 +90,37 @@ def __init__(self, *, parent=None, con=None, **kwargs):
cloud_data = kwargs.get(self._cloud_data_key, {})

self.object_id = cloud_data.get('id')
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

created_by = cloud_data.get(self._cc('createdBy'), {}).get('user', None)
self.created_by = Contact(con=self.con, protocol=self.protocol,
**{self._cloud_data_key: created_by}) if created_by else None
modified_by = cloud_data.get(self._cc('lastModifiedBy'), {}).get('user', None)
self.modified_by = Contact(con=self.con, protocol=self.protocol,
**{self._cloud_data_key: modified_by}) if modified_by else None

self.web_url = cloud_data.get(self._cc('webUrl'))

self.content_type_id = cloud_data.get(self._cc('contentType')).get('id',None)

self.fields = cloud_data.get(self._cc('fields'), None)

def __repr__(self) :
return 'List Item: {}'.format(self.web_url)


class SharepointList(ApiComponent):
_endpoints = {
'get_items': '/items'
'get_items': '/items',
'get_item_by_id':'/items/{item_id}',
'get_list_columns':'/columns'
}
list_item_constructor = SharepointListItem
list_column_constructor = SharepointListColumn

def __init__(self, *, parent=None, con=None, **kwargs):
""" A Sharepoint site List
Expand Down Expand Up @@ -127,13 +203,43 @@ def get_items(self):
**{self._cloud_data_key: item})
for item in data.get('value', [])]

def get_item_by_id(self,item_id):
""" Returns a sharepoint list item based on id"""

url = self.build_url(self._endpoints.get('get_item_by_id').format(item_id=item_id))

response = self.con.get(url)

if not response:
return []

data = response.json()

return self.list_item_constructor(parent=self, **{self._cloud_data_key: data})

def get_list_columns(self):
""" Returns the sharepoint list columns """

url = self.build_url(self._endpoints.get('get_list_columns'))

response = self.con.get(url)

if not response:
return []

data = response.json()

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


class Site(ApiComponent):
""" A Sharepoint Site """

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

Expand Down Expand Up @@ -262,10 +368,28 @@ def get_lists(self):

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', [])]


class Sharepoint(ApiComponent):
""" A Sharepoint parent class to group functionality """
Expand Down

0 comments on commit bea5a84

Please sign in to comment.