Skip to content

Commit

Permalink
#787: expose file_name parameter for copyto and copyto_using_path met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
vgrem committed Dec 6, 2023
1 parent b2139c8 commit 671e177
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
15 changes: 15 additions & 0 deletions examples/sharepoint/files/copy_file_with_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Demonstrates how to copy a file within a site
"""
from office365.sharepoint.client_context import ClientContext
from tests import test_team_site_url, test_user_credentials

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

file_from = ctx.web.get_file_by_server_relative_url(
"Shared Documents/Financial Sample.xlsx"
)
folder_to_url = "Shared Documents/archive"
new_filename = "Financial 2023.xlsx"
file_to = file_from.copyto(folder_to_url, True, new_filename).execute_query()
print("File copied into '{0}'".format(file_to.serverRelativeUrl))
2 changes: 2 additions & 0 deletions examples/sharepoint/folders/copy_folder_using_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
folder_from = ctx.web.default_document_library().root_folder.add(
create_unique_name("from")
)


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

Expand Down
2 changes: 1 addition & 1 deletion examples/sharepoint/sharing/share_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
shared_folder = ctx.web.get_folder_by_guest_url(
result.value.sharingLinkInfo.Url
).execute_query()
print(shared_folder.unique_id)
print(shared_folder)
16 changes: 10 additions & 6 deletions office365/sharepoint/files/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __repr__(self):
return self.serverRelativeUrl or self.unique_id or self.entity_type_name

def __str__(self):
return self.name
return self.name or self.entity_type_name

@staticmethod
def from_url(abs_url):
Expand Down Expand Up @@ -254,20 +254,23 @@ def deny(self, comment):
self.context.add_query(qry)
return self

def copyto(self, destination, overwrite=False):
# type: (Folder|str, bool) -> File
def copyto(self, destination, overwrite=False, file_name=None):
# type: (Folder|str, bool, str) -> "File"
"""Copies the file to the destination URL.
:param office365.sharepoint.folders.folder.Folder or str destination: Specifies the destination folder or
folder server relative url where to copy a file.
:param bool overwrite: Specifies whether a file with the same name is overwritten.
:param str file_name: A new file name
"""
return_type = File(self.context)
self.parent_collection.add_child(return_type)

def _copyto(destination_folder):
# type: (Folder) -> None
file_path = "/".join([str(destination_folder.serverRelativeUrl), self.name])
file_path = "/".join(
[str(destination_folder.serverRelativeUrl), file_name or self.name]
)
return_type.set_property("ServerRelativeUrl", file_path)

params = {"strNewUrl": file_path, "boverwrite": overwrite}
Expand All @@ -283,14 +286,15 @@ def _source_file_resolved():
self.ensure_properties(["ServerRelativeUrl", "Name"], _source_file_resolved)
return return_type

def copyto_using_path(self, destination, overwrite=False):
def copyto_using_path(self, destination, overwrite=False, file_name=None):
"""
Copies the file to the destination path. Server MUST overwrite an existing file of the same name
if overwrite is true.
:param bool overwrite: Specifies whether a file with the same name is overwritten.
:param office365.sharepoint.folders.folder.Folder or str destination: Specifies the destination folder or
folder server relative url where to copy a file.
:param str file_name: New file name
"""

return_type = File(self.context)
Expand All @@ -299,7 +303,7 @@ def copyto_using_path(self, destination, overwrite=False):
def _copyto_using_path(destination_folder):
# type: (Folder) -> None
file_path = "/".join(
[str(destination_folder.server_relative_path), self.name]
[str(destination_folder.server_relative_path), file_name or self.name]
)
return_type.set_property("ServerRelativePath", file_path)

Expand Down
4 changes: 2 additions & 2 deletions office365/sharepoint/webs/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ def get_folder_by_guest_url(self, guest_url):
:param str guest_url: The tokenized sharing link URL for the folder.
"""
return_type = Folder(self.context)
return_type = Folder(self.context, parent_collection=self.folders)
payload = {"guestUrl": guest_url}
qry = ServiceOperationQuery(
self, "GetFolderByGuestUrl", None, payload, None, return_type
Expand All @@ -1228,7 +1228,7 @@ def get_folder_by_guest_url_extended(
:param bool ensure_access: Indicates if the request to the tokenized sharing link grants perpetual access to
the calling user.
"""
return_type = Folder(self.context)
return_type = Folder(self.context, parent_collection=self.folders)
payload = {
"guestUrl": guest_url,
"requestSettings": SharingLinkAccessRequest(ensure_access, password),
Expand Down
2 changes: 1 addition & 1 deletion tests/sharepoint/test_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TestRoles(SPTestCase):
target_object = None # type: RoleDefinition
role_name = "Create and Manage Alerts 12"
role_name = "Create and Manage Alerts 123"

def test1_create_role(self):
permissions = BasePermissions()
Expand Down

0 comments on commit 671e177

Please sign in to comment.