Skip to content

Commit

Permalink
Merge pull request googleapis#210 from jtweeder/sharepoint_list_edit
Browse files Browse the repository at this point in the history
Ability to create, and edit listitems in sharepoint
  • Loading branch information
Alejandro Casanovas authored Mar 31, 2019
2 parents cc7c9bd + 1d3f28c commit d45e78b
Showing 1 changed file with 88 additions and 2 deletions.
90 changes: 88 additions & 2 deletions O365/sharepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __repr__(self):


class SharepointListItem(ApiComponent):
_endpoints = {}
_endpoints = {'update_list_item': '/items/{item_id}/fields'}

def __init__(self, *, parent=None, con=None, **kwargs):
""" A Sharepoint ListItem within a SharepointList
Expand All @@ -81,6 +81,7 @@ def __init__(self, *, parent=None, con=None, **kwargs):
if parent and con:
raise ValueError('Need a parent or a connection but not both')
self.con = parent.con if parent else con
self._parent = parent

# Choose the main_resource passed in kwargs over parent main_resource
main_resource = (kwargs.pop('main_resource', None) or
Expand All @@ -93,6 +94,7 @@ def __init__(self, *, parent=None, con=None, **kwargs):

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

self._track_changes = TrackerSet(casing=self._cc)
self.object_id = cloud_data.get('id')
created = cloud_data.get(self._cc('createdDateTime'), None)
modified = cloud_data.get(self._cc('lastModifiedDateTime'), None)
Expand All @@ -116,6 +118,56 @@ def __init__(self, *, parent=None, con=None, **kwargs):
def __repr__(self):
return 'List Item: {}'.format(self.web_url)

def _clear_tracker(self):
self._track_changes = TrackerSet(casing=self._cc)

def _valid_field(self, field):
#Verify the used field names are valid internal field names
valid_field_names = self.fields if self.fields \
else self._parent.column_name_cw.values() \
if self._parent \
else None
if valid_field_names:
return field in valid_field_names

#If no parent is given, and no internal fields are defined assume correct, API will check
return True

def update_fields(self, updates):
"""
Update the value for a field(s) in the listitem
:param update: A dict of {'field name': newvalue}
"""

for field in updates:
if self._valid_field(field):
self._track_changes.add(field)
else:
raise ValueError('"{}" is not a valid internal field name'.format(field))

#Update existing instance of fields, or create a fields instance if needed
if self.fields:
self.fields.update(updates)
else:
self.fields = updates

def save_updates(self):
"""Save the updated fields to the cloud"""

if not self._track_changes:
return True # there's nothing to update

url = self.build_url(self._endpoints.get('update_list_item').format(item_id=self.object_id))
update = {field: value for field, value in self.fields.items()
if self._cc(field) in self._track_changes}

response = self.con.patch(url, update)
if not response:
return False
self._clear_tracker()
return True


class SharepointList(ApiComponent):
_endpoints = {
Expand Down Expand Up @@ -189,7 +241,11 @@ def __init__(self, *, parent=None, con=None, **kwargs):
self._cc('contentTypesEnabled'), False)
self.hidden = lst_info.get(self._cc('hidden'), False)
self.template = lst_info.get(self._cc('template'), False)


#Crosswalk between display name of user defined columns to internal name
self.column_name_cw = {col.display_name: col.internal_name for
col in self.get_list_columns() if not col.read_only}

def get_items(self):
""" Returns a collection of Sharepoint Items
Expand Down Expand Up @@ -237,6 +293,36 @@ def get_list_columns(self):
return [self.list_column_constructor(parent=self, **{self._cloud_data_key: column})
for column in data.get('value', [])]

def create_list_item(self, new_data):
"""Create new list item
:param new_data: dictionary of {'col_name': col_value}
:rtype: SharepointListItem
"""

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

response = self.con.post(url, {'fields': new_data})
if not response:
return False

data = response.json()

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

def delete_list_item(self, item_id):
""" Delete an existing list item
:param item_id: Id of the item to be delted
"""

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

response = self.con.delete(url)

return bool(response)


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

0 comments on commit d45e78b

Please sign in to comment.