Skip to content

Commit 2e56f9a

Browse files
committed
OneDrive API :NamedItem support
1 parent 789da26 commit 2e56f9a

File tree

12 files changed

+2781
-2252
lines changed

12 files changed

+2781
-2252
lines changed

generator/import_metadata.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def export_to_file(path, content):
2121
"--endpoint",
2222
dest="endpoint",
2323
help="Import metadata endpoint",
24-
default="sharepoint",
24+
default="microsoftgraph",
2525
)
2626
parser.add_argument(
2727
"-p",
2828
"--path",
2929
dest="path",
30-
default="./metadata/SharePoint.xml",
30+
default="./metadata/MicrosoftGraph.xml",
3131
help="Import metadata endpoint",
3232
)
3333

generator/metadata/MicrosoftGraph.xml

+2,140-2,062
Large diffs are not rendered by default.

generator/metadata/SharePoint.xml

+553-150
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from office365.directory.authentication.methods.method import AuthenticationMethod
24

35

@@ -9,8 +11,6 @@ class EmailAuthenticationMethod(AuthenticationMethod):
911

1012
@property
1113
def email_address(self):
12-
"""
13-
The email address registered to this user.
14-
:rtype: str
15-
"""
14+
# type: () -> Optional[str]
15+
"""The email address registered to this user."""
1616
return self.properties.get("emailAddress", None)

office365/onedrive/workbooks/functions/result.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
class WorkbookFunctionResult(Entity):
55
@property
66
def value(self):
7-
return self.properties.get("__value", None)
7+
return self.properties.get("value", None)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from office365.entity_collection import EntityCollection
2+
from office365.onedrive.workbooks.names.named_item import WorkbookNamedItem
3+
from office365.runtime.queries.service_operation import ServiceOperationQuery
4+
5+
6+
class WorkbookNamedItemCollection(EntityCollection[WorkbookNamedItem]):
7+
def __init__(self, context, resource_path=None):
8+
super(WorkbookNamedItemCollection, self).__init__(
9+
context, WorkbookNamedItem, resource_path
10+
)
11+
12+
def add(self, name, reference, comment=None):
13+
"""
14+
Adds a new name to the collection of the given scope using the user's locale for the formula.
15+
16+
:param str name: The name of the object.
17+
:param str reference: Represents the formula that the name is defined to refer to.
18+
For example, =Sheet14!$B$2:$H$12, =4.75,
19+
:param str comment: Represents the comment associated with this name.
20+
"""
21+
return_type = WorkbookNamedItem(self.context)
22+
self.add_child(return_type)
23+
payload = {"name": name, "reference": reference, "comment": comment}
24+
qry = ServiceOperationQuery(self, "add", None, payload, None, return_type)
25+
self.context.add_query(qry)
26+
return return_type

office365/onedrive/workbooks/names/named_item.py

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def worksheet(self):
3333
self.context, ResourcePath("worksheet", self.resource_path)
3434
),
3535
)
36+
37+
@property
38+
def property_ref_name(self):
39+
# type: () -> str
40+
return "name"

office365/onedrive/workbooks/workbook.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from office365.onedrive.workbooks.applications.application import WorkbookApplication
44
from office365.onedrive.workbooks.comments.comment import WorkbookComment
55
from office365.onedrive.workbooks.functions.functions import WorkbookFunctions
6-
from office365.onedrive.workbooks.names.named_item import WorkbookNamedItem
6+
from office365.onedrive.workbooks.names.collection import WorkbookNamedItemCollection
77
from office365.onedrive.workbooks.operations.workbook import WorkbookOperation
88
from office365.onedrive.workbooks.session_info import WorkbookSessionInfo
99
from office365.onedrive.workbooks.tables.collection import WorkbookTableCollection
1010
from office365.onedrive.workbooks.worksheets.collection import (
1111
WorkbookWorksheetCollection,
1212
)
1313
from office365.runtime.client_result import ClientResult
14+
from office365.runtime.http.request_options import RequestOptions
1415
from office365.runtime.paths.resource_path import ResourcePath
1516
from office365.runtime.queries.function import FunctionQuery
1617
from office365.runtime.queries.service_operation import ServiceOperationQuery
@@ -56,25 +57,20 @@ def refresh_session(self, session_id):
5657
self.context.add_query(qry)
5758

5859
def _construct_request(request):
59-
"""
60-
:type request: office365.runtime.http.request_options.RequestOptions
61-
"""
60+
# type: (RequestOptions) -> None
6261
request.set_header("workbook-session-id", session_id)
6362

6463
self.context.before_execute(_construct_request)
6564
return self
6665

6766
def close_session(self, session_id):
6867
"""Use this API to close an existing workbook session.
69-
7068
:param str session_id: Identifier of the workbook session
7169
"""
7270
qry = ServiceOperationQuery(self, "closeSession")
7371

7472
def _construct_request(request):
75-
"""
76-
:type request: office365.runtime.http.request_options.RequestOptions
77-
"""
73+
# type: (RequestOptions) -> None
7874
request.set_header("workbook-session-id", session_id)
7975

8076
self.context.before_execute(_construct_request)
@@ -126,13 +122,12 @@ def tables(self):
126122

127123
@property
128124
def names(self):
129-
# type: () -> EntityCollection[WorkbookNamedItem]
125+
# type: () -> WorkbookNamedItemCollection
130126
"""Represents a collection of workbook scoped named items (named ranges and constants). Read-only."""
131127
return self.properties.get(
132128
"names",
133-
EntityCollection(
129+
WorkbookNamedItemCollection(
134130
self.context,
135-
WorkbookNamedItem,
136131
ResourcePath("names", self.resource_path),
137132
),
138133
)

office365/outlook/item.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
from typing import Optional
23

34
from office365.entity import Entity
45
from office365.runtime.types.collections import StringCollection
@@ -7,18 +8,16 @@
78
class OutlookItem(Entity):
89
@property
910
def change_key(self):
11+
# type: () -> Optional[str]
1012
"""
1113
Identifies the version of the item. Every time the item is changed, changeKey changes as well.
1214
This allows Exchange to apply changes to the correct version of the object.
13-
:rtype: str
1415
"""
1516
return self.properties.get("ChangeKey", None)
1617

1718
@property
1819
def categories(self):
19-
"""
20-
The categories associated with the item
21-
"""
20+
"""The categories associated with the item"""
2221
return self.properties.get("categories", StringCollection())
2322

2423
@property

office365/runtime/odata/json_format.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ class ODataJsonFormat(object):
55
"""OData JSON format"""
66

77
def __init__(self, metadata_level=None):
8-
"""
9-
:type metadata_level: str
10-
"""
8+
# type: (str) -> None
119
self.metadata_level = metadata_level
1210

1311
__metaclass__ = ABCMeta
@@ -28,15 +26,13 @@ def collection_next(self):
2826
def media_type(self):
2927
"""
3028
Gets media type
31-
3229
:rtype: str
3330
"""
3431
raise NotImplementedError
3532

3633
@property
3734
def include_control_information(self):
3835
"""Determines whether control information that is represented as annotations should be included in payload
39-
4036
:rtype: bool
4137
"""
4238
raise NotImplementedError

office365/runtime/odata/request.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
from typing import Any, Optional
23

34
import requests
45

@@ -69,11 +70,7 @@ def process_response(self, response, query):
6970
self.map_json(response.json(), return_type, json_format)
7071

7172
def map_json(self, json, return_type, json_format=None):
72-
"""
73-
:type json: any
74-
:type return_type: ClientValue or ClientResult or ClientObject
75-
:type json_format: office365.runtime.odata.json_format.ODataJsonFormat
76-
"""
73+
# type: (Any, ClientValue | ClientResult | ClientObject, Optional[ODataJsonFormat]) -> None
7774
if json_format is None:
7875
json_format = self.json_format
7976

@@ -82,19 +79,17 @@ def map_json(self, json, return_type, json_format=None):
8279
return_type.set_property(k, v, False)
8380

8481
def _next_property(self, json, json_format):
85-
"""
86-
:type json: Any
87-
:type json_format: office365.runtime.odata.json_format.ODataJsonFormat
88-
"""
82+
# type: (Any, ODataJsonFormat) -> None
8983
if isinstance(json_format, JsonLightFormat):
9084
json = json.get(json_format.security, json)
9185
json = json.get(json_format.function, json)
9286

9387
if isinstance(json, dict):
94-
next_link_url = json.get(json_format.collection_next, None)
95-
json = json.get(json_format.collection, json)
96-
if next_link_url:
97-
yield "__nextLinkUrl", next_link_url
88+
if isinstance(json.get(json_format.collection, None), list):
89+
next_link_url = json.get(json_format.collection_next, None)
90+
json = json.get(json_format.collection, json)
91+
if next_link_url:
92+
yield "__nextLinkUrl", next_link_url
9893

9994
if isinstance(json, list):
10095
for index, item in enumerate(json):

tests/onedrive/test_excel_ranges.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from office365.onedrive.driveitems.driveItem import DriveItem
2+
from office365.onedrive.workbooks.names.named_item import WorkbookNamedItem
3+
from tests import create_unique_name
4+
from tests.graph_case import GraphTestCase
5+
from tests.onedrive.test_excel import upload_excel
6+
7+
8+
class TestExcelRanges(GraphTestCase):
9+
excel_file = None # type: DriveItem
10+
named_item = None # type: WorkbookNamedItem
11+
sheet_name = create_unique_name("Sheet")
12+
13+
@classmethod
14+
def setUpClass(cls):
15+
super(TestExcelRanges, cls).setUpClass()
16+
cls.excel_file = upload_excel(cls.client.me.drive)
17+
assert cls.excel_file.resource_path is not None
18+
19+
@classmethod
20+
def tearDownClass(cls):
21+
cls.excel_file.delete_object().execute_query_retry()
22+
23+
def test1_name_create(self):
24+
result = self.__class__.excel_file.workbook.names.add(
25+
"test5", "=Sheet1!$F$15:$N$27", "Comment for the named item"
26+
).execute_query()
27+
self.assertIsNotNone(result.resource_path)
28+
self.__class__.named_item = result
29+
30+
def test2_names_get(self):
31+
result = self.__class__.named_item.get().execute_query()
32+
self.assertIsNotNone(result.resource_path)

0 commit comments

Comments
 (0)