|
2 | 2 |
|
3 | 3 | from dateutil.parser import parse |
4 | 4 |
|
5 | | -from .utils import ApiComponent, TrackerSet |
| 5 | +from .utils import ApiComponent, TrackerSet, NEXT_LINK_KEYWORD, Pagination |
6 | 6 | from .address_book import Contact |
7 | 7 | from .drive import Storage |
8 | 8 |
|
@@ -66,7 +66,8 @@ def __repr__(self): |
66 | 66 |
|
67 | 67 |
|
68 | 68 | class SharepointListItem(ApiComponent): |
69 | | - _endpoints = {'update_list_item': '/items/{item_id}/fields'} |
| 69 | + _endpoints = {'update_list_item': '/items/{item_id}/fields', |
| 70 | + 'delete_list_item': '/items/{item_id}'} |
70 | 71 |
|
71 | 72 | def __init__(self, *, parent=None, con=None, **kwargs): |
72 | 73 | """ A Sharepoint ListItem within a SharepointList |
@@ -109,9 +110,9 @@ def __init__(self, *, parent=None, con=None, **kwargs): |
109 | 110 | self.modified_by = Contact(con=self.con, protocol=self.protocol, |
110 | 111 | **{self._cloud_data_key: modified_by}) if modified_by else None |
111 | 112 |
|
112 | | - self.web_url = cloud_data.get(self._cc('webUrl')) |
| 113 | + self.web_url = cloud_data.get(self._cc('webUrl'), None) |
113 | 114 |
|
114 | | - self.content_type_id = cloud_data.get(self._cc('contentType')).get('id', None) |
| 115 | + self.content_type_id = cloud_data.get(self._cc('contentType'), {}).get('id', None) |
115 | 116 |
|
116 | 117 | self.fields = cloud_data.get(self._cc('fields'), None) |
117 | 118 |
|
@@ -168,6 +169,11 @@ def save_updates(self): |
168 | 169 | self._clear_tracker() |
169 | 170 | return True |
170 | 171 |
|
| 172 | + def delete(self): |
| 173 | + url = self.build_url(self._endpoints.get('delete_list_item').format(item_id=self.object_id)) |
| 174 | + response = self.con.delete(url) |
| 175 | + return bool(response) |
| 176 | + |
171 | 177 |
|
172 | 178 | class SharepointList(ApiComponent): |
173 | 179 | _endpoints = { |
@@ -244,23 +250,51 @@ def __init__(self, *, parent=None, con=None, **kwargs): |
244 | 250 | self.column_name_cw = {col.display_name: col.internal_name for |
245 | 251 | col in self.get_list_columns() if not col.read_only} |
246 | 252 |
|
247 | | - def get_items(self): |
| 253 | + def get_items(self, limit=None, *, query=None, order_by=None, batch=None): |
248 | 254 | """ Returns a collection of Sharepoint Items |
249 | | -
|
250 | | - :rtype: list[SharepointListItem] |
| 255 | + :param int limit: max no. of items to get. Over 999 uses batch. |
| 256 | + :param query: applies a filter to the request. |
| 257 | + :type query: Query or str |
| 258 | + :param order_by: orders the result set based on this condition |
| 259 | + :type order_by: Query or str |
| 260 | + :param int batch: batch size, retrieves items in |
| 261 | + batches allowing to retrieve more items than the limit. |
| 262 | + :return: list of Sharepoint Items |
| 263 | + :rtype: list[SharepointListItem] or Pagination |
251 | 264 | """ |
| 265 | + |
252 | 266 | url = self.build_url(self._endpoints.get('get_items')) |
253 | 267 |
|
254 | | - response = self.con.get(url) |
| 268 | + if limit is None or limit > self.protocol.max_top_value: |
| 269 | + batch = self.protocol.max_top_value |
| 270 | + |
| 271 | + params = {'$top': batch if batch else limit} |
| 272 | + |
| 273 | + if order_by: |
| 274 | + params['$orderby'] = order_by |
| 275 | + |
| 276 | + if query: |
| 277 | + if isinstance(query, str): |
| 278 | + params['$filter'] = query |
| 279 | + else: |
| 280 | + params.update(query.as_params()) |
| 281 | + |
| 282 | + response = self.con.get(url, params=params) |
255 | 283 |
|
256 | 284 | if not response: |
257 | 285 | return [] |
258 | 286 |
|
259 | 287 | data = response.json() |
| 288 | + next_link = data.get(NEXT_LINK_KEYWORD, None) |
260 | 289 |
|
261 | | - return [self.list_item_constructor(parent=self, |
262 | | - **{self._cloud_data_key: item}) |
263 | | - for item in data.get('value', [])] |
| 290 | + items = [self.list_item_constructor(parent=self, **{self._cloud_data_key: item}) |
| 291 | + for item in data.get('value', [])] |
| 292 | + |
| 293 | + if batch and next_link: |
| 294 | + return Pagination(parent=self, data=items, constructor=self.list_item_constructor, |
| 295 | + next_link=next_link, limit=limit) |
| 296 | + else: |
| 297 | + return items |
264 | 298 |
|
265 | 299 | def get_item_by_id(self, item_id): |
266 | 300 | """ Returns a sharepoint list item based on id""" |
@@ -477,6 +511,22 @@ def get_list_by_name(self, display_name): |
477 | 511 |
|
478 | 512 | return self.list_constructor(parent=self, **{self._cloud_data_key: data}) |
479 | 513 |
|
| 514 | + def create_list(self, list_data): |
| 515 | + """ |
| 516 | + Creates a SharePoint list. |
| 517 | + :param list_data: Dict representation of list. |
| 518 | + :type list_data: Dict |
| 519 | + :rtype: list[SharepointList] |
| 520 | + """ |
| 521 | + url = self.build_url(self._endpoints.get('get_lists')) |
| 522 | + response = self.con.post(url, data=list_data) |
| 523 | + |
| 524 | + if not response: |
| 525 | + return None |
| 526 | + |
| 527 | + data = response.json() |
| 528 | + return self.list_constructor(parent=self, **{self._cloud_data_key: data}) |
| 529 | + |
480 | 530 |
|
481 | 531 | class Sharepoint(ApiComponent): |
482 | 532 | """ A Sharepoint parent class to group functionality """ |
|
0 commit comments