Skip to content

Commit b9f3775

Browse files
vvgrem@gmail.comvvgrem@gmail.com
authored andcommitted
Minor refactoring for ensure_folder_path method (#292) SharePoint API batch fixes
1 parent 40ec3a5 commit b9f3775

File tree

8 files changed

+65
-31
lines changed

8 files changed

+65
-31
lines changed

examples/sharepoint/webs/__init__.py

Whitespace-only changes.

generator/metadata/SharePoint.xml

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

office365/runtime/queries/batch_query.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import uuid
22

33
from office365.runtime.queries.client_query import ClientQuery
4-
from office365.runtime.queries.create_entity_query import CreateEntityQuery
54
from office365.runtime.queries.read_entity_query import ReadEntityQuery
6-
from office365.runtime.queries.service_operation_query import ServiceOperationQuery
75

86

97
def create_boundary(prefix, compact=False):
@@ -41,10 +39,13 @@ def add(self, query):
4139
self._queries.append(query)
4240

4341
def get(self, index):
44-
result = [qry for qry in self._queries
45-
if isinstance(qry, ReadEntityQuery)
46-
or isinstance(qry, CreateEntityQuery)
47-
or isinstance(qry, ServiceOperationQuery)]
42+
result = [qry for qry in self.change_sets if qry.return_type is not None] + \
43+
[qry for qry in self._queries if isinstance(qry, ReadEntityQuery)]
44+
45+
# result = [qry for qry in self._queries
46+
# if isinstance(qry, ReadEntityQuery)
47+
# or isinstance(qry, CreateEntityQuery)
48+
# or isinstance(qry, ServiceOperationQuery)]
4849
return result[index]
4950

5051
@property

office365/sharepoint/folders/folder_collection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
from office365.runtime.client_object_collection import ClientObjectCollection
24
from office365.runtime.queries.create_entity_query import CreateEntityQuery
35
from office365.runtime.resource_path_service_operation import ResourcePathServiceOperation
@@ -15,6 +17,22 @@ def get(self):
1517
"""
1618
return super(FolderCollection, self).get()
1719

20+
def ensure_folder_path(self, path):
21+
"""
22+
Function to create a folder
23+
:type path: string
24+
:param path: relative server URL (path) to a folder
25+
"""
26+
27+
url_component = os.path.normpath(path).split(os.path.sep)
28+
url_component = [part for part in url_component if part] # ensure no empty elements
29+
if not url_component:
30+
raise NotADirectoryError("Wrong relative URL provided")
31+
child_folder = self
32+
for url_part in url_component:
33+
child_folder = child_folder.add(url_part)
34+
return child_folder
35+
1836
def add(self, server_relative_url):
1937
"""Adds the folder that is located at the specified URL to the collection.
2038

office365/sharepoint/tenant/administration/tenant.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, context):
2020

2121
def get_site_secondary_administrators(self, site_id):
2222
"""
23-
Returns the current site collection administrators
23+
Gets site collection administrators
2424
2525
:type site_id: str
2626
"""
@@ -31,6 +31,20 @@ def get_site_secondary_administrators(self, site_id):
3131
self.context.add_query(qry)
3232
return return_type
3333

34+
def set_site_secondary_administrators(self, site_id, emails, names):
35+
"""
36+
Sets site collection administrators
37+
38+
:type names: list[str]
39+
:type emails: list[str]
40+
:type site_id: str
41+
"""
42+
payload = SecondaryAdministratorsFieldsData(emails, names, site_id)
43+
qry = ServiceOperationQuery(self, "SetSiteSecondaryAdministrators", None, payload,
44+
"secondaryAdministratorsFieldsData", None)
45+
self.context.add_query(qry)
46+
return self
47+
3448
def register_hub_site(self, site_url):
3549
"""
3650
Registers an existing site as a hub site.
@@ -84,13 +98,6 @@ def remove_deleted_site(self, site_url):
8498
def restore_deleted_site(self, site_url):
8599
pass
86100

87-
def set_site_secondary_administrators(self, data):
88-
"""
89-
90-
:type data: SecondaryAdministratorsFieldsData
91-
"""
92-
pass
93-
94101
def get_site_properties_by_url(self, url, include_detail):
95102
"""
96103

office365/sharepoint/webs/web.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,7 @@ def ensure_folder_path(self, path):
208208
:type path: string
209209
:param path: relative server URL (path) to a folder
210210
"""
211-
212-
url_component = os.path.normpath(path).split(os.path.sep)
213-
url_component = [part for part in url_component if part] # ensure no empty elements
214-
if not url_component:
215-
raise NotADirectoryError("Wrong relative URL provided")
216-
217-
folder = self.get_folder_by_server_relative_url(url_component[0])
218-
for url_part in url_component[1:]:
219-
folder = folder.add(url_part)
220-
221-
self.context.execute_query() # need to execute now, otherwise folders are created in "after_execute_query"
222-
return folder
211+
return self.root_folder.folders.ensure_folder_path(path)
223212

224213
def ensure_user(self, login_name):
225214
"""Checks whether the specified logon name belongs to a valid user of the website, and if the logon name does
@@ -679,6 +668,11 @@ def recycleBin(self):
679668
RecycleBinItemCollection(self.context,
680669
ResourcePath("RecycleBin", self.resource_path)))
681670

671+
@property
672+
def root_folder(self):
673+
"""Get a root folder"""
674+
return self.properties.get("RootFolder", Folder(self.context, ResourcePath("RootFolder", self.resource_path)))
675+
682676
def set_property(self, name, value, persist_changes=True):
683677
super(Web, self).set_property(name, value, persist_changes)
684678
# fallback: create a new resource path

tests/sharepoint/test_tenant.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ def test4_get_site_secondary_administrators(self):
4848
self.client.execute_query()
4949
self.assertIsNotNone(result)
5050

51-
# def test5_create_site(self):
51+
# def test5_set_site_secondary_administrators(self):
52+
# target_site = self.client.site.get()
53+
# target_user = self.client.web.ensure_user("jdoe@mediadev8.onmicrosoft.com")
54+
# self.client.execute_batch()
55+
# self.tenant.set_site_secondary_administrators(target_site.id, [target_user.login_name],[target_user.login_name])
56+
# self.client.execute_query()
57+
58+
# def test6_create_site(self):
5259
# current_user = self.client.web.currentUser
5360
# self.client.load(current_user)
5461
# self.client.execute_query()
@@ -58,20 +65,20 @@ def test4_get_site_secondary_administrators(self):
5865
# self.client.execute_query()
5966
# self.assertIsNotNone(site_props)
6067

61-
# def test6_get_site_by_url(self):
68+
# def test7_get_site_by_url(self):
6269
# site_props = self.tenant.get_site_properties_by_url(self.__class__.target_site_url, False)
6370
# self.client.execute_query()
6471
# self.assertIsNotNone(site_props.properties['SiteUrl'], self.__class__.target_site_url)
6572
# self.__class__.target_site_props = site_props
6673

67-
# def test7_update_site(self):
74+
# def test8_update_site(self):
6875
# site_props_to_update = self.__class__.target_site_props
6976
# site_props_to_update.set_property('SharingCapability', SharingCapabilities.ExternalUserAndGuestSharing)
7077
# site_props_to_update.update()
7178
# self.client.execute_query()
7279
# self.assertTrue(site_props_to_update.properties['Status'], 'Active')
7380

74-
# def test8_delete_site(self):
81+
# def test9_delete_site(self):
7582
# site_url = self.__class__.target_site_props.properties['SiteUrl']
7683
# self.tenant.remove_site(site_url)
7784
# self.client.execute_query()

tests/sharepoint/test_web.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,10 @@ def test_18_get_list_templates(self):
128128
def test_19_get_custom_list_templates(self):
129129
templates = self.client.web.get_custom_list_templates().execute_query()
130130
self.assertGreaterEqual(len(templates), 0)
131+
132+
def test20_ensure_folder_path(self):
133+
folder_path = "/Shared Documents/Archive/2020/12"
134+
folder_new_nested = self.client.web.ensure_folder_path(folder_path).execute_query()
135+
folder_new_nested = self.client.web.get_folder_by_server_relative_url(folder_path).get().execute_query()
136+
self.assertTrue(folder_new_nested.properties["Exists"])
137+

0 commit comments

Comments
 (0)