Skip to content

Commit 384aaa1

Browse files
committed
OneDrive: sitepages namespace enhancements, support for Prefer header
1 parent d77967b commit 384aaa1

File tree

10 files changed

+146
-19
lines changed

10 files changed

+146
-19
lines changed

examples/directory/applications/has_delegated_perms.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
)
1818

1919
resource = client.service_principals.get_by_name("Microsoft Graph")
20-
scope = "FileStorageContainer.Selected"
20+
scope = "BackupRestore-Control.Read.All"
2121
user = client.users.get_by_principal_name(test_admin_principal_name)
2222
client_app = client.applications.get_by_app_id(test_client_id)
2323
result = resource.get_delegated_permissions(test_client_id).execute_query()
24-
if len([cur_scope for cur_scope in result.value if cur_scope == scope]) == 0:
24+
found_scope = next(
25+
(cur_scope for cur_scope in result.value if cur_scope == scope), None
26+
)
27+
if found_scope is None:
2528
print("Delegated permission '{0}' is not granted".format(scope))
2629
else:
2730
print(result.value)

examples/onedrive/files/get_by_abs_url.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@
1818
)
1919

2020
file_item = client.shares.by_url(file_abs_url).drive_item.get().execute_query()
21-
22-
result = file_item.get_content().execute_query()
23-
print(result.value)
21+
print(file_item.id)

office365/onedrive/contenttypes/collection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77

88
class ContentTypeCollection(EntityCollection[ContentType]):
9+
"""Content type collection"""
10+
911
def __init__(self, context, resource_path):
1012
super(ContentTypeCollection, self).__init__(context, ContentType, resource_path)
1113

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from office365.directory.permissions.identity_set import IdentitySet
12
from office365.runtime.client_value import ClientValue
23

34

45
class PublicationFacet(ClientValue):
5-
def __init__(self, level=None, version_id=None):
6+
"""The publicationFacet resource provides details on the published status of a driveItemVersion or driveItem
7+
resource."""
8+
9+
def __init__(self, checked_out_by=IdentitySet(), level=None, version_id=None):
610
"""
711
The publicationFacet resource provides details on the published status of a driveItemVersion
812
or driveItem resource.
@@ -11,5 +15,6 @@ def __init__(self, level=None, version_id=None):
1115
:param str version_id: The unique identifier for the version that is visible to the current caller. Read-only.
1216
"""
1317
super(PublicationFacet, self).__init__()
18+
self.checkedOutBy = checked_out_by
1419
self.level = level
1520
self.versionId = version_id
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from office365.entity_collection import EntityCollection
2+
from office365.onedrive.listitems.list_item import ListItem
3+
from office365.runtime.http.request_options import RequestOptions
4+
5+
6+
class ListItemCollection(EntityCollection[ListItem]):
7+
"""List Item's collection"""
8+
9+
def __init__(self, context, resource_path):
10+
super(ListItemCollection, self).__init__(context, ListItem, resource_path)
11+
self._honor_nonindexed = False
12+
13+
def honor_nonindexed(self, value):
14+
# type: (bool) -> "ListItemCollection"
15+
"""Sets if true HonorNonIndexedQueriesWarningMayFailRandomly header
16+
to allow non-indexed properties to be queried."""
17+
self._honor_nonindexed = value
18+
return self
19+
20+
def get(self):
21+
"""Retrieve a list item"""
22+
23+
def _construct_request(request):
24+
# type: (RequestOptions) -> None
25+
if self._honor_nonindexed:
26+
request.headers["Prefer"] = (
27+
"HonorNonIndexedQueriesWarningMayFailRandomly"
28+
)
29+
30+
return super(ListItemCollection, self).get().before_execute(_construct_request)
31+
32+
def get_by_name(self, name):
33+
"""Retrieve a list item by name"""
34+
return self.single("fields/FileLeafRef eq '{0}'".format(name))

office365/onedrive/lists/list.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from office365.onedrive.base_item import BaseItem
55
from office365.onedrive.columns.definition_collection import ColumnDefinitionCollection
66
from office365.onedrive.contenttypes.collection import ContentTypeCollection
7-
from office365.onedrive.listitems.list_item import ListItem
7+
from office365.onedrive.listitems.collection import ListItemCollection
88
from office365.onedrive.lists.info import ListInfo
99
from office365.onedrive.operations.rich_long_running import RichLongRunningOperation
1010
from office365.onedrive.sharepoint_ids import SharePointIds
@@ -68,13 +68,11 @@ def content_types(self):
6868

6969
@property
7070
def items(self):
71-
# type: () -> EntityCollection[ListItem]
71+
# type: () -> ListItemCollection
7272
"""All items contained in the list."""
7373
return self.properties.get(
7474
"items",
75-
EntityCollection(
76-
self.context, ListItem, ResourcePath("items", self.resource_path)
77-
),
75+
ListItemCollection(self.context, ResourcePath("items", self.resource_path)),
7876
)
7977

8078
@property

office365/onedrive/sitepages/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ def title(self):
3030
# type: () -> Optional[str]
3131
"""Title of the sitePage."""
3232
return self.properties.get("title", None)
33+
34+
def get_property(self, name, default_value=None):
35+
if default_value is None:
36+
property_mapping = {
37+
"publishingState": self.publishing_state,
38+
}
39+
default_value = property_mapping.get(name, None)
40+
return super(BaseSitePage, self).get_property(name, default_value)

office365/onedrive/sitepages/collection.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ def _construct_request(request):
1919
return super(SitePageCollection, self).get().before_execute(_construct_request)
2020

2121
def get_by_name(self, name):
22+
"""Get a sitePage by name."""
2223
return self.single("name eq '{0}'".format(name))
2324

24-
def add(self, title):
25+
def get_by_title(self, title):
26+
"""Get a sitePage by title."""
27+
return self.single("title eq '{0}'".format(title))
28+
29+
def add(self, title, page_layout="article"):
2530
"""
2631
Create a new sitePage in the site pages list in a site.
2732
2833
:param str title:
34+
:param str page_layout:
2935
"""
3036

3137
def _construct_request(request):
@@ -37,7 +43,7 @@ def _construct_request(request):
3743
.add(
3844
title=title,
3945
name="{0}.aspx".format(title),
40-
pageLayout="article",
46+
pageLayout=page_layout,
4147
titleArea=TitleArea(),
4248
)
4349
.before_execute(_construct_request)

office365/onedrive/sitepages/site_page.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class SitePage(BaseSitePage):
1515
webParts."""
1616

1717
def get_web_parts_by_position(
18-
self, web_part_index, horizontal_section_id, is_in_vertical_section, column_id
18+
self,
19+
web_part_index=None,
20+
horizontal_section_id=None,
21+
is_in_vertical_section=None,
22+
column_id=None,
1923
):
2024
"""
2125
Get a collection of webPart by providing webPartPosition information.
@@ -40,6 +44,13 @@ def get_web_parts_by_position(
4044
self.context.add_query(qry)
4145
return return_type
4246

47+
def checkin(self):
48+
"""
49+
Check in the latest version of a sitePage resource, which makes the version of the page available to all users.
50+
If the page is checked out, check in the page and publish it. If the page is checked out to the caller
51+
of this API, the page is automatically checked in and then published."""
52+
return self
53+
4354
def publish(self):
4455
"""
4556
Publish the latest version of a sitePage resource, which makes the version of the page available to all users.
@@ -49,7 +60,7 @@ def publish(self):
4960
If a page approval flow has been activated in the page library, the page is not published until the approval
5061
flow is completed.
5162
"""
52-
qry = ServiceOperationQuery(self, "publish")
63+
qry = ServiceOperationQuery(self, "microsoft.graph.sitePage/publish")
5364
self.context.add_query(qry)
5465
return self
5566

tests/onedrive/test_sitepage.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,85 @@
1-
from tests import test_team_site_url
1+
from office365.onedrive.sitepages.site_page import SitePage
2+
from tests import create_unique_name, test_team_site_url
23
from tests.graph_case import GraphTestCase
34

45

56
class TestSitePage(GraphTestCase):
67
"""OneDrive specific test case base class"""
78

9+
target_page = None # type: SitePage
10+
811
@classmethod
912
def setUpClass(cls):
1013
super(TestSitePage, cls).setUpClass()
1114
cls.test_site = cls.client.sites.get_by_url(test_team_site_url)
15+
cls.page_name = create_unique_name("Test Page")
1216

1317
@classmethod
1418
def tearDownClass(cls):
1519
pass
1620

17-
def test1_list_site_pages(self):
21+
def test1_create_site_page(self):
22+
result = self.test_site.pages.add(self.page_name).execute_query()
23+
self.assertIsNotNone(result.resource_path)
24+
self.__class__.target_page = result
25+
26+
def test2_get_site_page(self):
27+
page = self.__class__.target_page
28+
result = page.get().execute_query()
29+
self.assertIsNotNone(result.resource_path)
30+
31+
def test3_checkin_site_page(self):
32+
page_name = self.__class__.target_page.name
33+
pages_list = self.test_site.lists.get_by_name("Site Pages")
34+
list_item = (
35+
pages_list.items.honor_nonindexed(True)
36+
.get_by_name(page_name)
37+
.execute_query()
38+
)
39+
list_item.drive_item.checkin("Initial version").execute_query()
40+
self.assertIsNotNone(list_item.resource_path)
41+
42+
def test4_get_site_page_pub_state(self):
43+
page = self.__class__.target_page
44+
result = page.get().select(["publishingState"]).execute_query()
45+
self.assertIsNotNone(result.publishing_state.level)
46+
47+
# def test5_publish_site_page(self):
48+
# page = self.__class__.target_page
49+
# result = page.publish().execute_query()
50+
# self.assertIsNotNone(result.resource_path)
51+
52+
def test6_list_site_pages(self):
1853
result = self.test_site.pages.top(10).get().execute_query()
1954
self.assertIsNotNone(result.resource_path)
2055

21-
# def test2_get_site_page_by_name(self):
22-
# result = self.test_site.pages.get_by_name("Home.aspx").execute_query()
56+
# def test7_get_site_page_by_name(self):
57+
# result = self.test_site.pages.get_by_name(self.page_name).execute_query()
2358
# self.assertIsNotNone(result.resource_path)
59+
60+
# def test8_get_site_page_by_title(self):
61+
# page = self.__class__.target_page
62+
# result = self.test_site.pages.get_by_title(page.title).execute_query()
63+
# self.assertIsNotNone(result.resource_path)
64+
65+
# def test9_get_web_parts_by_position(self):
66+
# page = self.__class__.target_page
67+
# result = page.get_web_parts_by_position().execute_query()
68+
# self.assertIsNotNone(result.resource_path)
69+
70+
def test_10_delete_site_page(self):
71+
page = self.__class__.target_page
72+
page.delete_object().execute_query()
73+
74+
def test_11_get_site_page_list(self):
75+
result = self.test_site.lists.get_by_name("Site Pages").get().execute_query()
76+
self.assertIsNotNone(result.resource_path)
77+
78+
def test_12_get_site_page_item_by_name(self):
79+
pages_list = self.test_site.lists.get_by_name("Site Pages")
80+
result = (
81+
pages_list.items.honor_nonindexed(True)
82+
.get_by_name("Home.aspx")
83+
.execute_query()
84+
)
85+
self.assertIsNotNone(result.resource_path)

0 commit comments

Comments
 (0)