Skip to content

Commit

Permalink
typings enhancements & Folder.copy_to method fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jan 1, 2024
1 parent 0757e43 commit 2253066
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 66 deletions.
11 changes: 8 additions & 3 deletions examples/sharepoint/files/upload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Demonstrates how to upload a small files (up to 4MB in size)
"""
import os

from office365.sharepoint.client_context import ClientContext
from tests import test_team_site_url, test_user_credentials
Expand All @@ -10,7 +11,11 @@
list_title = "Documents"
folder = ctx.web.lists.get_by_title(list_title).root_folder
# path = "../../data/SharePoint User Guide.docx"
path = "../../data/Sample.pdf"
with open(path, "rb") as f:
file = folder.files.upload(f).execute_query()
# path = "../../data/Sample.pdf"
path = "../../data/countries.json"
# with open(path, "rb") as f:
# file = folder.files.upload(f).execute_query()
with open(path, "rb") as content_file:
file_content = content_file.read()
file = folder.upload_file(os.path.basename(path), file_content).execute_query()
print("File has been uploaded into: {0}".format(file.serverRelativeUrl))
17 changes: 10 additions & 7 deletions examples/sharepoint/folders/copy_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

# creates a temporary folder first in a Documents library
folder_from = ctx.web.default_document_library().root_folder.add(
create_unique_name("from")
)
# folder_from = ctx.web.default_document_library().root_folder.add(
# create_unique_name("from")
# )
folder_from = ctx.web.get_folder_by_server_relative_url("Shared Documents/Archive/2001")

# folder_to = ctx.web.default_document_library().root_folder.add(create_unique_name("to"))
folder_to_url = "/sites/team/Shared Documents/Archive/2001/01"
# folder_to_url = "/sites/team/Shared Documents/Archive/2001/01"
folder_to = ctx.web.get_folder_by_server_relative_url("Shared Documents/Archive/2002")

# copies the folder with a new name
folder = folder_from.copy_to(folder_to_url).execute_query()
folder = folder_from.copy_to(folder_to).execute_query()
print(
"Folder has been copied from '{0}' into '{1}'".format(
folder_from.serverRelativeUrl, folder.serverRelativeUrl
)
)

# clean up
folder_from.delete_object().execute_query()
folder.delete_object().execute_query()
# folder_from.delete_object().execute_query()
# folder.delete_object().execute_query()
8 changes: 4 additions & 4 deletions office365/directory/extensions/open_type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from office365.directory.extensions.extension import Extension


Expand All @@ -10,8 +12,6 @@ class OpenTypeExtension(Extension):

@property
def extension_name(self):
"""
A unique text identifier for an open type data extension. Optional.
:rtype: str
"""
# type: () -> Optional[str]
"""A unique text identifier for an open type data extension."""
return self.properties.get("extensionName", None)
16 changes: 7 additions & 9 deletions office365/onedrive/driveitems/driveItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ def upload(self, name, content):
:type content: str or bytes or None
"""
return_type = DriveItem(self.context, UrlPath(name, self.resource_path))
self.children.add_child(return_type)
qry = ServiceOperationQuery(
return_type, "content", None, content, None, return_type
)
self.children.add_child(qry.return_type)

def _modify_query(request):
# type: (RequestOptions) -> None
Expand Down Expand Up @@ -533,15 +533,13 @@ def get_activities_by_interval(self, start_dt=None, end_dt=None, interval=None):
self.context, ItemActivityStat, self.resource_path
)

def _create_query():
params = {
"startDateTime": start_dt.strftime("%m-%d-%Y") if start_dt else None,
"endDateTime": end_dt.strftime("%m-%d-%Y") if end_dt else None,
"interval": interval,
}
return FunctionQuery(self, "getActivitiesByInterval", params, return_type)
params = {
"startDateTime": start_dt.strftime("%m-%d-%Y") if start_dt else None,
"endDateTime": end_dt.strftime("%m-%d-%Y") if end_dt else None,
"interval": interval,
}

qry = _create_query()
qry = FunctionQuery(self, "getActivitiesByInterval", params, return_type)
self.context.add_query(qry)
return return_type

Expand Down
24 changes: 11 additions & 13 deletions office365/runtime/client_runtime_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def has_pending_request(self):

def build_request(self, query):
# type: (ClientQuery) -> RequestOptions
"""
Builds a request
"""
"""Builds a request"""
self._current_query = query
request = self.pending_request().build_request(query)
self.pending_request().beforeExecute.notify(request)
Expand Down Expand Up @@ -98,8 +96,8 @@ def load(
self.after_query_execute(after_loaded, client_object)
return self

def before_query_execute(self, action, once=True, *args, **kwargs):
# type: (Callable[[RequestOptions, Any, Any], None], bool, Any, Any) -> Self
def before_query_execute(self, action, once=True):
# type: (Callable[[RequestOptions], None], bool) -> Self
"""
Attach an event handler which is triggered before query is submitted to server
Expand All @@ -115,13 +113,13 @@ def _prepare_request(request):
if self.current_query.id == query.id:
if once:
self.pending_request().beforeExecute -= _prepare_request
action(request, *args, **kwargs)
action(request)

self.pending_request().beforeExecute += _prepare_request
return self

def before_execute(self, action, once=True, *args, **kwargs):
# type: (Callable[[RequestOptions, ...], None], bool, Optional[Any], Optional[Any]) -> Self
def before_execute(self, action, once=True):
# type: (Callable[[RequestOptions], None], bool) -> Self
"""
Attach an event handler which is triggered before request is submitted to server
:param (office365.runtime.http.request_options.RequestOptions, any) -> None action:
Expand All @@ -132,13 +130,13 @@ def _process_request(request):
# type: (RequestOptions) -> None
if once:
self.pending_request().beforeExecute -= _process_request
action(request, *args, **kwargs)
action(request)

self.pending_request().beforeExecute += _process_request
return self

def after_query_execute(self, action, *args, **kwargs):
# type: (Callable[[Any, Any], None], Any, Any) -> Self
# type: (Callable[..., None], Any, Any) -> Self
"""Attach an event handler which is triggered after query is submitted to server"""
if len(self._queries) == 0:
return
Expand All @@ -159,15 +157,15 @@ def _process_response(resp):

return self

def after_execute(self, action, once=True, *args, **kwargs):
# type: (Callable[[requests.Response, Any, Any], None], bool, Any, Any) -> Self
def after_execute(self, action, once=True):
# type: (Callable[[requests.Response], None], bool) -> Self
"""Attach an event handler which is triggered after request is submitted to server"""

def _process_response(response):
# type: (requests.Response) -> None
if once:
self.pending_request().afterExecute -= _process_response
action(response, *args, **kwargs)
action(response)

self.pending_request().afterExecute += _process_response
return self
Expand Down
12 changes: 4 additions & 8 deletions office365/sharepoint/folders/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def copy_to(self, destination, keep_both=False, reset_author_and_created=False):
return_type = Folder(self.context)
self.parent_collection.add_child(return_type)

def _copy_folder(destination_folder):
def _copy_to(destination_folder):
# type: ("Folder") -> None
destination_url = "/".join(
[destination_folder.serverRelativeUrl, self.name]
Expand All @@ -396,15 +396,11 @@ def _copy_folder(destination_folder):

def _source_folder_resolved():
if isinstance(destination, Folder):
destination.ensure_property(
"ServerRelativeUrl", _copy_folder, destination
)
destination.ensure_property("ServerRelativeUrl", _copy_to, destination)
else:
self.context.web.ensure_folder_path(destination).after_execute(
_copy_folder
)
self.context.web.ensure_folder_path(destination).after_execute(_copy_to)

self.ensure_property("ServerRelativeUrl", _source_folder_resolved)
self.ensure_properties(["ServerRelativeUrl", "Name"], _source_folder_resolved)
return return_type

def copy_to_using_path(
Expand Down
16 changes: 7 additions & 9 deletions office365/sharepoint/listitems/listitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ def share(
ExternalSharingSiteOption.Edit: "role:1073741827",
}

def _picker_value_resolved(resp, picker_result):
def _picker_value_resolved(picker_result):
# type: (ClientResult) -> None
file_abs_url = self.get_property("EncodedAbsUrl")
picker_value = "[{0}]".format(picker_result.value)
from office365.sharepoint.webs.web import Web
Expand All @@ -229,15 +230,12 @@ def _picker_value_resolved(resp, picker_result):
return_type=return_type,
)

def _property_resolved():
picker_result = (
ClientPeoplePickerWebServiceInterface.client_people_picker_resolve_user(
self.context, user_principal_name
)
)
self.context.after_execute(_picker_value_resolved, True, picker_result)
def _url_resolved():
ClientPeoplePickerWebServiceInterface.client_people_picker_resolve_user(
self.context, user_principal_name
).after_execute(_picker_value_resolved)

self.ensure_property("EncodedAbsUrl", _property_resolved)
self.ensure_property("EncodedAbsUrl", _url_resolved)
return return_type

def unshare(self):
Expand Down
7 changes: 4 additions & 3 deletions office365/sharepoint/portal/sites/manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from office365.runtime.client_result import ClientResult
from office365.runtime.http.http_method import HttpMethod
from office365.runtime.http.request_options import RequestOptions
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.queries.service_operation import ServiceOperationQuery
from office365.sharepoint.entity import Entity
Expand Down Expand Up @@ -73,13 +74,13 @@ def get_status(self, site_url):
qry = ServiceOperationQuery(
self, "Status", None, {"url": site_url}, None, response
)
self.context.add_query(qry)

def _construct_status_request(request):
def _construct_request(request):
# type: (RequestOptions) -> None
request.method = HttpMethod.Get
request.url += "?url='{0}'".format(site_url)

self.context.before_execute(_construct_status_request)
self.context.add_query(qry).before_execute(_construct_request)
return response

def get_site_url(self, site_id):
Expand Down
20 changes: 10 additions & 10 deletions office365/sharepoint/webs/template.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from office365.sharepoint.entity import Entity


Expand All @@ -9,42 +11,41 @@ def __repr__(self):

@property
def description(self):
"""Gets a value that specifies the description of the list template.
:rtype: str or None
"""
# type: () -> Optional[str]
"""Gets a value that specifies the description of the list template."""
return self.properties.get("Description", None)

@property
def display_category(self):
# type: () -> Optional[str]
"""
Specifies the display name for the category that this site definition configuration or site template is
a part of.
:rtype: str or None
"""
return self.properties.get("DisplayCategory", None)

@property
def image_url(self):
# type: () -> Optional[str]
"""
Specifies the URL for the image that is associated with the site definition configuration or site template.
:rtype: str or None
"""
return self.properties.get("ImageUrl", None)

@property
def is_hidden(self):
# type: () -> Optional[bool]
"""
Specifies whether the site definition configuration is displayed in the user interface for creating new sites
:rtype: bool or None
"""
return self.properties.get("IsHidden", None)

@property
def is_root_web_only(self):
# type: () -> Optional[bool]
"""
Specifies whether the site definition configuration or site template can only be applied to the top-level site
in the site collection.
:rtype: bool or None
"""
return self.properties.get("IsRootWebOnly", None)

Expand Down Expand Up @@ -74,7 +75,6 @@ def name(self):

@property
def title(self):
"""Specifies the display name for the site definition configuration or site template.
:rtype: str or None
"""
# type: () -> Optional[str]
"""Specifies the display name for the site definition configuration or site template."""
return self.properties.get("Title", None)

0 comments on commit 2253066

Please sign in to comment.