Skip to content

Commit 88d6b61

Browse files
committed
Added support for remaining methods for File resource
1 parent 9c49fab commit 88d6b61

File tree

6 files changed

+162
-2
lines changed

6 files changed

+162
-2
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ matrix:
1111
allow_failures:
1212
- python: 2.6
1313
- python: 3.2
14+
- python: 3.3
1415

1516
# command to run tests
1617
script: python setup.py test

office365/sharepoint/attachmentfile_collection.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ def add(self, attachment_file_information):
3333
def get_by_filename(self, filename):
3434
"""Retrieve Attachmentfile object by filename"""
3535
return Attachmentfile(self.context,
36-
ResourcePathServiceOperation(self.context, self.resource_path, "GetByFileName", [filename]))
36+
ResourcePathServiceOperation(self.context, self.resource_path, "GetByFileName",
37+
[filename]))

office365/sharepoint/file.py

+138
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from office365.runtime.action_type import ActionType
22
from office365.runtime.client_object import ClientObject
33
from office365.runtime.client_query import ClientQuery
4+
from office365.runtime.client_result import ClientResult
45
from office365.runtime.odata.odata_path_parser import ODataPathParser
56
from office365.runtime.resource_path_entry import ResourcePathEntry
7+
from office365.runtime.resource_path_service_operation import ResourcePathServiceOperation
68
from office365.runtime.utilities.http_method import HttpMethod
79
from office365.runtime.utilities.request_options import RequestOptions
810
from office365.sharepoint.listitem import ListItem
11+
from office365.sharepoint.webparts.limited_webpart_manager import LimitedWebPartManager
912

1013

1114
class AbstractFile(ClientObject):
@@ -35,7 +38,28 @@ class File(AbstractFile):
3538
"""Represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library,
3639
or a file in a folder."""
3740

41+
def approve(self, comment):
42+
"""Approves the file submitted for content approval with the specified comment."""
43+
qry = ClientQuery.service_operation_query(self,
44+
ActionType.PostMethod,
45+
"approve",
46+
{
47+
"comment": comment
48+
})
49+
self.context.add_query(qry)
50+
51+
def deny(self, comment):
52+
"""Denies approval for a file that was submitted for content approval."""
53+
qry = ClientQuery.service_operation_query(self,
54+
ActionType.PostMethod,
55+
"deny",
56+
{
57+
"comment": comment
58+
})
59+
self.context.add_query(qry)
60+
3861
def copyto(self, new_relative_url, overwrite):
62+
"""Copies the file to the destination URL."""
3963
qry = ClientQuery.service_operation_query(self,
4064
ActionType.PostMethod,
4165
"moveto",
@@ -47,6 +71,7 @@ def copyto(self, new_relative_url, overwrite):
4771
self.context.add_query(qry)
4872

4973
def moveto(self, new_relative_url, flag):
74+
"""Moves the file to the specified destination URL."""
5075
qry = ClientQuery.service_operation_query(self,
5176
ActionType.PostMethod,
5277
"moveto",
@@ -57,6 +82,119 @@ def moveto(self, new_relative_url, flag):
5782
None)
5883
self.context.add_query(qry)
5984

85+
def publish(self, comment):
86+
"""Submits the file for content approval with the specified comment."""
87+
qry = ClientQuery.service_operation_query(self,
88+
ActionType.PostMethod,
89+
"publish",
90+
{
91+
"comment": comment,
92+
}
93+
)
94+
self.context.add_query(qry)
95+
96+
def unpublish(self, comment):
97+
"""Removes the file from content approval or unpublish a major version."""
98+
qry = ClientQuery.service_operation_query(self,
99+
ActionType.PostMethod,
100+
"unpublish",
101+
{
102+
"comment": comment,
103+
}
104+
)
105+
self.context.add_query(qry)
106+
107+
def checkout(self):
108+
"""Checks out the file from a document library based on the check-out type."""
109+
qry = ClientQuery.service_operation_query(self,
110+
ActionType.PostMethod,
111+
"checkout",
112+
)
113+
self.context.add_query(qry)
114+
115+
def checkin(self, comment, checkin_type):
116+
"""Checks the file in to a document library based on the check-in type."""
117+
qry = ClientQuery.service_operation_query(self,
118+
ActionType.PostMethod,
119+
"checkin",
120+
{
121+
"comment": comment,
122+
"checkInType": checkin_type
123+
}
124+
)
125+
self.context.add_query(qry)
126+
127+
def undocheckout(self):
128+
"""Reverts an existing checkout for the file."""
129+
qry = ClientQuery.service_operation_query(self,
130+
ActionType.PostMethod,
131+
"undocheckout",
132+
)
133+
self.context.add_query(qry)
134+
135+
def recycle(self):
136+
"""Moves the file to the Recycle Bin and returns the identifier of the new Recycle Bin item."""
137+
qry = ClientQuery.service_operation_query(self,
138+
ActionType.PostMethod,
139+
"recycle",
140+
)
141+
self.context.add_query(qry)
142+
143+
def get_limited_webpart_manager(self, scope):
144+
"""Specifies the control set used to access, modify, or add Web Parts associated with this Web Part Page and
145+
view. """
146+
return LimitedWebPartManager(self.context,
147+
ResourcePathServiceOperation(self.context, self.resource_path,
148+
"getlimitedwebpartmanager",
149+
[scope]
150+
))
151+
152+
def start_upload(self, upload_id, content):
153+
"""Starts a new chunk upload session and uploads the first fragment."""
154+
qry = ClientQuery.service_operation_query(self,
155+
ActionType.PostMethod,
156+
"startupload",
157+
{
158+
"uploadID": upload_id
159+
},
160+
content
161+
)
162+
result = ClientResult
163+
self.context.add_query(qry, result)
164+
return result
165+
166+
def continue_upload(self, upload_id, file_offset, content):
167+
"""Continues the chunk upload session with an additional fragment. The current file content is not changed."""
168+
qry = ClientQuery.service_operation_query(self,
169+
ActionType.PostMethod,
170+
"continueupload",
171+
{
172+
"uploadID": upload_id,
173+
"fileOffset": file_offset,
174+
},
175+
content
176+
)
177+
result = ClientResult
178+
self.context.add_query(qry, result)
179+
return result
180+
181+
def finish_upload(self, upload_id, file_offset, content):
182+
"""Uploads the last file fragment and commits the file. The current file content is changed when this method
183+
completes. """
184+
qry = ClientQuery.service_operation_query(self,
185+
ActionType.PostMethod,
186+
"finishupload",
187+
{
188+
"uploadID": upload_id,
189+
"fileOffset": file_offset,
190+
},
191+
content
192+
)
193+
self.context.add_query(qry, self)
194+
return self
195+
196+
197+
60198
@staticmethod
61199
def save_binary(ctx, server_relative_url, content):
62200
try:

office365/sharepoint/webparts/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.runtime.client_object import ClientObject
2+
3+
4+
class LimitedWebPartManager(ClientObject):
5+
""""""

tests/test_file.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,22 @@ def test_5_move_file(self):
8282
self.context.execute_query()
8383
self.assertEqual(new_file_url, file.properties["ServerRelativeUrl"])
8484

85-
def test_6_delete_file(self):
85+
def test_6_recycle_first_file(self):
86+
"""Test file upload operation"""
87+
files = self.target_list.root_folder.files
88+
self.context.load(files)
89+
self.context.execute_query()
90+
files_count = len(files)
91+
if files_count > 0:
92+
first_file = files[0]
93+
first_file.recycle()
94+
self.context.execute_query()
95+
files_after = self.target_list.root_folder.files
96+
self.context.load(files_after)
97+
self.context.execute_query()
98+
self.assertEqual(len(files)-1, len(files_after))
99+
100+
def test_7_delete_file(self):
86101
files_to_delete = self.target_list.root_folder.files
87102
self.context.load(files_to_delete)
88103
self.context.execute_query()

0 commit comments

Comments
 (0)