Skip to content

Commit 9eefb90

Browse files
committed
DriveItem.move method & example updates
1 parent 7c744b9 commit 9eefb90

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

examples/onedrive/files/move_file.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
https://learn.microsoft.com/en-us/graph/api/driveitem-move?view=graph-rest-1.0
66
"""
77
from office365.graph_client import GraphClient
8+
from office365.onedrive.driveitems.conflict_behavior import ConflictBehavior
89
from tests import test_client_id, test_password, test_tenant, test_username
910

1011
client = GraphClient.with_username_and_password(
1112
test_tenant, test_client_id, test_username, test_password
1213
)
13-
source_path = "archive/Sample.rtf"
14+
15+
local_path = "../../data/Financial Sample.xlsx"
16+
source_file = client.me.drive.root.upload_file(local_path).execute_query()
1417
target_path = "archive/2018"
15-
source_file_item = client.me.drive.root.get_by_path(source_path)
16-
target_folder_item = client.me.drive.root.get_by_path(target_path)
17-
result = source_file_item.move(parent=target_folder_item).execute_query()
18-
print(result.value)
18+
target_folder = client.me.drive.root.get_by_path(target_path)
19+
target_file = source_file.move(parent=target_folder, conflict_behavior=ConflictBehavior.Replace).execute_query()
20+
print(target_file.web_url)

office365/onedrive/driveitems/driveItem.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -520,31 +520,43 @@ def _drive_item_loaded():
520520
_create_and_add_query(parent)
521521
return return_type
522522

523-
def move(self, name=None, parent=None):
523+
def move(self, name=None, parent=None, conflict_behavior=ConflictBehavior.Fail):
524+
# type: (str, ItemReference|"DriveItem", str) -> "DriveItem"
524525
"""To move a DriveItem to a new parent item, your app requests to update the parentReference of the DriveItem
525526
to move.
526527
527-
return_type = ClientResult(self.context, str())
528+
:param str name: The new name for the move. If this isn't provided, the same name will be used as the
529+
original.
530+
:param ItemReference or DriveItem or None parent: Reference to the
531+
parent item the move will be created in.
532+
:param str conflict_behavior: query parameter to customize the behavior when a conflict occurs.
533+
"""
528534

529-
def _create_and_add_query(parent_reference):
535+
return_type = DriveItem(self.context)
536+
self.children.add_child(return_type)
537+
538+
def _move(parent_reference):
539+
# type: (ItemReference) -> None
530540
payload = {"name": name, "parentReference": parent_reference}
531541

532542
def _construct_request(request):
533543
# type: (RequestOptions) -> None
534544
request.method = HttpMethod.Patch
545+
request.url += "?@microsoft.graph.conflictBehavior={0}".format(
546+
conflict_behavior
547+
)
535548

536-
self.context.before_execute(_construct_request)
537549
qry = ServiceOperationQuery(self, "", None, payload, None, return_type)
538-
self.context.add_query(qry)
550+
self.context.add_query(qry).before_execute(_construct_request)
539551

540552
if isinstance(parent, DriveItem):
541553

542554
def _drive_item_loaded():
543-
_create_and_add_query(ItemReference(_id=parent.id))
555+
_move(ItemReference(_id=parent.id))
544556

545557
parent.ensure_property("parentReference", _drive_item_loaded)
546558
else:
547-
_create_and_add_query(parent)
559+
_move(parent)
548560
return return_type
549561

550562
def rename(self, new_name):

0 commit comments

Comments
 (0)