Skip to content

Commit d1083b9

Browse files
vvgrem@gmail.comvvgrem@gmail.com
authored andcommitted
SharePoint API: Folder methods and types, bug fixes: #256
1 parent 310ccf2 commit d1083b9

File tree

15 files changed

+128
-76
lines changed

15 files changed

+128
-76
lines changed

examples/sharepoint/files/upload_large_file.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1+
import functools
12
import os
2-
33
from settings import settings
44

55
from office365.runtime.auth.user_credential import UserCredential
66
from office365.sharepoint.client_context import ClientContext
77

8-
9-
def print_upload_progress(offset, total_size):
10-
print("Uploaded '{}' bytes from '{}'...[{}%]".format(offset, total_size, round(offset/total_size*100, 2)))
11-
12-
138
credentials = UserCredential(settings['user_credentials']['username'],
149
settings['user_credentials']['password'])
1510
ctx = ClientContext(settings['url']).with_credentials(credentials)
@@ -19,8 +14,14 @@ def print_upload_progress(offset, total_size):
1914
size_chunk = 1000000
2015
local_path = "../../../tests/data/big_buck_bunny.mp4"
2116

17+
2218
file_size = os.path.getsize(local_path)
2319

20+
21+
def print_upload_progress(offset):
22+
print("Uploaded '{}' bytes from '{}'...[{}%]".format(offset, file_size, round(offset / file_size * 100, 2)))
23+
24+
2425
if file_size > size_chunk:
2526
result_file = target_folder.files.create_upload_session(local_path, size_chunk, print_upload_progress)
2627
else:

examples/sharepoint/tenant/get_site_admin.py renamed to examples/sharepoint/tenant/get_set_site_admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
admins = tenant.get_site_secondary_administrators(site_id=target_site.id)
1818
admin_client.execute_query()
1919

20-
#emails = settings.get("test_accounts")
21-
#tenant.set_site_secondary_administrators(site_id=target_site.id, emails=emails).execute_query()
20+
emails = settings.get("test_accounts")
21+
tenant.set_site_secondary_administrators(site_id=target_site.id, emails=emails).execute_query()
2222

2323
for admin in admins: # type: SecondaryAdministratorsInfo
2424
print(admin.get_property('loginName'))

office365/runtime/queries/service_operation_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def build_url(self):
2828
self.binding_type.query_options.reset()
2929
if self.static:
3030
url = self.context.service_root_url() + \
31-
'.'.join([self.binding_type.entity_type_name, method_path.to_url()])
31+
'.'.join([self.binding_type.entity_type_name, method_path.to_url()])
3232
else:
3333
url = '/'.join([self.binding_type.resource_url, method_path.to_url()])
3434
return url

office365/sharepoint/actions/create_file.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ class CreateFileQuery(ServiceOperationQuery):
77
def __init__(self, files, file_creation_information):
88
"""
99
10-
:type file_creation_information: office365.sharepoint.file_creation_information.FileCreationInformation
10+
:type file_creation_information: office365.sharepoint.files.file_creation_information.FileCreationInformation
1111
:type files: FileCollection
1212
"""
13-
self._return_type = File(files.context)
14-
super().__init__(files, "add", {
15-
"overwrite": file_creation_information.overwrite,
16-
"url": file_creation_information.url
17-
}, file_creation_information.content, None, self._return_type)
13+
super().__init__(files, "add", file_creation_information.to_json(), file_creation_information.content, None,
14+
File(files.context))
1815
files.add_child(self._return_type)

office365/sharepoint/actions/upload_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, files, source_path, chunk_size, chunk_uploaded, chunk_func_ar
3131
:type files: office365.sharepoint.files.file_collection.FileCollection
3232
:type source_path: str
3333
:type chunk_size: int
34-
:type chunk_uploaded: (int)->None
34+
:type chunk_uploaded: (int, *)->None
3535
"""
3636

3737
super().__init__(files, _create_empty_file(source_path))

office365/sharepoint/files/file_creation_information.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ def __init__(self, url=None, overwrite=False, content=None):
1414
self._overwrite = overwrite
1515
self._content = content
1616

17+
def to_json(self):
18+
return {
19+
"overwrite": self.overwrite,
20+
"url": self.url
21+
}
22+
1723
@property
1824
def content(self):
1925
"""Gets the binary content of the file."""

office365/sharepoint/folders/folder.py

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
from office365.runtime.queries.update_entity_query import UpdateEntityQuery
66
from office365.runtime.resource_path import ResourcePath
77
from office365.runtime.resource_path_service_operation import ResourcePathServiceOperation
8+
from office365.sharepoint.actions.create_file import CreateFileQuery
89
from office365.sharepoint.base_entity import BaseEntity
910
from office365.sharepoint.changes.change_collection import ChangeCollection
1011
from office365.sharepoint.changes.change_query import ChangeQuery
12+
from office365.sharepoint.contenttypes.content_type_id import ContentTypeId
1113
from office365.sharepoint.files.file_creation_information import FileCreationInformation
1214
from office365.sharepoint.listitems.listitem import ListItem
1315

@@ -68,6 +70,7 @@ def _add_sub_folder():
6870
new_folder.set_property("ServerRelativeUrl", new_folder_url)
6971
qry = CreateEntityQuery(self.folders, new_folder, new_folder)
7072
self.context.add_query(qry)
73+
7174
self.ensure_property("ServerRelativeUrl", _add_sub_folder)
7275
return new_folder
7376

@@ -90,18 +93,17 @@ def delete_object(self):
9093
self.remove_from_parent_collection()
9194
return self
9295

93-
def upload_file(self, name, content):
96+
def upload_file(self, file_name, content):
9497
"""Uploads a file into folder
9598
96-
:type name: str
99+
:type file_name: str
97100
:type content: str
101+
:rtype: office365.sharepoint.files.file.File
98102
"""
99-
info = FileCreationInformation()
100-
info.content = content
101-
info.url = name
102-
info.overwrite = True
103-
target_file = self.files.add(info)
104-
return target_file
103+
info = FileCreationInformation(url=file_name, overwrite=True, content=content)
104+
qry = CreateFileQuery(self.files, info)
105+
self.context.add_query(qry)
106+
return qry.return_type
105107

106108
def copy_to(self, new_relative_url, overwrite):
107109
"""Copies the folder with files to the destination URL.
@@ -132,16 +134,15 @@ def _move_folder_with_files():
132134
for file in self.files:
133135
new_file_url = "/".join([new_relative_url, file.properties['Name']])
134136
file.moveto(new_file_url, flags)
137+
135138
self.ensure_property("Files", _move_folder_with_files)
136139
return self
137140

138141
@property
139142
def list_item_all_fields(self):
140143
"""Specifies the list item fields (2) values for the list item corresponding to the folder."""
141-
if self.is_property_available('ListItemAllFields'):
142-
return self.properties["ListItemAllFields"]
143-
else:
144-
return ListItem(self.context, ResourcePath("ListItemAllFields", self.resource_path))
144+
return self.properties.get("ListItemAllFields",
145+
ListItem(self.context, ResourcePath("ListItemAllFields", self.resource_path)))
145146

146147
@property
147148
def files(self):
@@ -152,6 +153,28 @@ def files(self):
152153
from office365.sharepoint.files.file_collection import FileCollection
153154
return FileCollection(self.context, ResourcePath("Files", self.resource_path))
154155

156+
@property
157+
def folders(self):
158+
"""Specifies the collection of list folders contained within the list folder.
159+
"""
160+
from office365.sharepoint.folders.folder_collection import FolderCollection
161+
return self.properties.get("Folders",
162+
FolderCollection(self.context, ResourcePath("Folders", self.resource_path)))
163+
164+
@property
165+
def parent_folder(self):
166+
"""Specifies the list folder.
167+
"""
168+
return self.properties.get("ParentFolder",
169+
Folder(self.context, ResourcePath("ParentFolder", self.resource_path)))
170+
171+
@property
172+
def name(self):
173+
"""Specifies the list folder name.
174+
:rtype: str or None
175+
"""
176+
return self.properties.get("Name", None)
177+
155178
@property
156179
def unique_id(self):
157180
"""Gets the unique ID of the folder.
@@ -181,6 +204,14 @@ def unique_content_type_order(self):
181204
"""
182205
return self.properties.get("UniqueContentTypeOrder", None)
183206

207+
@property
208+
def content_type_order(self):
209+
"""Specifies the content type order for the list folder.
210+
211+
:rtype: office365.sharepoint.contenttypes.content_type_id.ContentTypeId or None
212+
"""
213+
return self.properties.get("ContentTypeOrder", ContentTypeId())
214+
184215
@property
185216
def time_last_modified(self):
186217
"""Gets the last time this folder or a direct child was modified in UTC.
@@ -196,22 +227,23 @@ def serverRelativeUrl(self):
196227
"""
197228
return self.properties.get("ServerRelativeUrl", None)
198229

199-
@property
200-
def folders(self):
201-
"""Get a folder collection"""
202-
if self.is_property_available('Folders'):
203-
return self.properties["Folders"]
230+
def get_property(self, name):
231+
property_mapping = {
232+
"ListItemAllFields": self.list_item_all_fields,
233+
"ParentFolder": self.parent_folder
234+
}
235+
if name in property_mapping:
236+
return property_mapping[name]
204237
else:
205-
from office365.sharepoint.folders.folder_collection import FolderCollection
206-
return FolderCollection(self.context, ResourcePath("Folders", self.resource_path))
238+
return super(Folder, self).get_property(name)
207239

208240
def set_property(self, name, value, persist_changes=True):
209241
super(Folder, self).set_property(name, value, persist_changes)
210242
# fallback: create a new resource path
211-
if self._resource_path is None:
212-
if name == "ServerRelativeUrl":
213-
self._resource_path = ResourcePathServiceOperation("getFolderByServerRelativeUrl", [value],
214-
ResourcePath("Web"))
215-
elif name == "UniqueId":
216-
self._resource_path = ResourcePathServiceOperation("getFolderById", [value], ResourcePath("Web"))
243+
# if self._resource_path is None:
244+
if name == "ServerRelativeUrl":
245+
self._resource_path = ResourcePathServiceOperation("getFolderByServerRelativeUrl", [value],
246+
ResourcePath("Web"))
247+
elif name == "UniqueId":
248+
self._resource_path = ResourcePathServiceOperation("getFolderById", [value], ResourcePath("Web"))
217249
return self

office365/sharepoint/listitems/listitem.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def set_property(self, name, value, persist_changes=True):
244244
if name == "Id" and self._parent_collection is not None:
245245
self._resource_path = ResourcePathServiceOperation(
246246
"getItemById", [value], self._parent_collection.resource_path.parent)
247+
return self
247248

248249
def ensure_type_name(self, target_list):
249250
"""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.sharepoint.listitems.listitem import ListItem
2+
3+
4+
class UserInfoItem(ListItem):
5+
pass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class SiteCreationDefaultStorageQuota(ClientValue):
5+
pass

0 commit comments

Comments
 (0)