Skip to content

Commit 859d0b9

Browse files
Improvements for fluent interface, support for saving taxonomy field values (#353)
1 parent 98448bb commit 859d0b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+505
-349
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ where
303303
def download_files(remote_folder, local_path):
304304
drive_items = remote_folder.children.get().execute_query()
305305
for drive_item in drive_items:
306-
if not drive_item.file.is_server_object_null: # is file?
306+
if drive_item.file is not None: # is file?
307307
# download file content
308308
with open(os.path.join(local_path, drive_item.name), 'wb') as local_file:
309309
drive_item.download(local_file).execute_query()

examples/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
from tests import settings
44

55

6-
def acquire_token_client_credentials():
7-
6+
def acquire_token_by_client_credentials():
87
authority_url = 'https://login.microsoftonline.com/{0}'.format(settings.get('default', 'tenant'))
98
app = msal.ConfidentialClientApplication(
109
authority=authority_url,
1110
client_id=settings.get('client_credentials', 'client_id'),
1211
client_credential=settings.get('client_credentials', 'client_secret')
1312
)
14-
result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
15-
return result
13+
return app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
14+
15+
16+
def acquire_token_by_username_password():
17+
authority_url = 'https://login.microsoftonline.com/{0}'.format(settings.get('default', 'tenant'))
18+
app = msal.PublicClientApplication(
19+
authority=authority_url,
20+
client_id=settings.get('client_credentials', 'client_id')
21+
)
22+
return app.acquire_token_by_username_password(username=settings.get('user_credentials', "username"),
23+
password=settings.get('user_credentials', "password"),
24+
scopes=["https://graph.microsoft.com/.default"])

examples/b2c/registerIdP.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from examples import acquire_token_client_credentials
1+
from examples import acquire_token_by_client_credentials
22
from office365.graph_client import GraphClient
33

44

5-
client = GraphClient(acquire_token_client_credentials)
5+
client = GraphClient(acquire_token_by_client_credentials)
66
idp_col = client.identity_providers.get().execute_query()
77
for idp in idp_col:
88
print(idp.id)

examples/directory/delete_groups.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
from examples import acquire_token_client_credentials
1+
from examples import acquire_token_by_username_password
22
from office365.graph_client import GraphClient
33

4-
client = GraphClient(acquire_token_client_credentials)
4+
client = GraphClient(acquire_token_by_username_password)
55

6-
groups = client.groups.get().top(10).execute_query()
7-
index = 0
6+
groups = client.groups.get().top(100).execute_query()
7+
deletedCount = 0
88
groups_count = len(groups)
99
while len(groups) > 0:
1010
cur_grp = groups[0]
11-
print("({0} of {1}) Deleting {2} group ...".format(index + 1, groups_count, cur_grp.properties['displayName']))
12-
cur_grp.delete_object().execute_query()
13-
print("Group deleted.")
14-
index += 1
15-
16-
deleted_groups = client.directory.deleted_groups.get().execute_query()
17-
groups_count = len(deleted_groups)
18-
index = 0
19-
while len(deleted_groups) > 0:
20-
cur_grp = deleted_groups[0]
21-
print("({0} of {1}) Deleting {2} group permanently ...".format(index + 1, groups_count,
22-
cur_grp.properties['displayName']))
23-
cur_grp.delete_object().execute_query()
24-
print("Group deleted.")
25-
index += 1
11+
print(
12+
"({0} of {1}) Deleting {2} group ...".format(deletedCount + 1, groups_count, cur_grp.properties['displayName']))
13+
cur_grp.delete_object(permanent_delete=True).execute_query()
14+
print("Group deleted permanently.")
15+
deletedCount += 1

examples/onedrive/export_files.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
import os
66
import tempfile
77

8-
from examples import acquire_token_client_credentials
8+
from examples import acquire_token_by_client_credentials
99
from office365.graph_client import GraphClient
1010
from tests import test_user_principal_name
1111

1212

13-
client = GraphClient(acquire_token_client_credentials)
13+
client = GraphClient(acquire_token_by_client_credentials)
1414
drive = client.users[test_user_principal_name].drive # get user's drive
1515
with tempfile.TemporaryDirectory() as local_path:
1616
drive_items = drive.root.children.get().execute_query()
17-
file_items = [item for item in drive_items if not item.file.is_server_object_null] # files only
17+
file_items = [item for item in drive_items if item.file is not None] # files only
1818
for drive_item in file_items:
1919
with open(os.path.join(local_path, drive_item.name), 'wb') as local_file:
2020
drive_item.download(local_file).execute_query() # download file content

examples/onedrive/import_files.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from os.path import isfile, join
33

4-
from examples import acquire_token_client_credentials
4+
from examples import acquire_token_by_client_credentials
55
from office365.graph_client import GraphClient
66
from tests import load_settings
77

@@ -23,7 +23,7 @@ def upload_files(remote_drive, local_root_path):
2323

2424

2525
settings = load_settings()
26-
client = GraphClient(acquire_token_client_credentials)
26+
client = GraphClient(acquire_token_by_client_credentials)
2727
user_name = settings.get('test_alt_account_name')
2828
target_drive = client.users[user_name].drive # get target drive
2929
# import local files into OneDrive

examples/onedrive/print_folders_and_files.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from office365.graph_client import GraphClient
2-
from tests import acquire_token_by_username_password
2+
from tests.graph_case import acquire_token_by_username_password
33

44

55
def enum_folders_and_files(root_folder):
66
drive_items = root_folder.children.get().execute_query()
77
for drive_item in drive_items:
8-
item_type = drive_item.folder.is_server_object_null and "file" or "folder"
9-
print("Type: {0} Name: {1}".format(item_type, drive_item.name))
10-
if not drive_item.folder.is_server_object_null and drive_item.folder.childCount > 0:
8+
print("Name: {0}".format(drive_item.name))
9+
if not drive_item.is_property_available("folder"): # is folder facet?
1110
enum_folders_and_files(drive_item)
1211

1312

examples/onedrive/upload_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import os
22

3-
from examples import acquire_token_client_credentials
3+
from examples import acquire_token_by_client_credentials
44
from office365.graph_client import GraphClient
55
from tests import settings
66

7-
client = GraphClient(acquire_token_client_credentials)
7+
client = GraphClient(acquire_token_by_client_credentials)
88
user_name = settings.get('first_account_name')
99
target_drive = client.users[user_name].drive
1010

examples/outlook/send_message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from examples import acquire_token_client_credentials
1+
from examples import acquire_token_by_client_credentials
22
from office365.graph_client import GraphClient
33
from tests import test_user_principal_name, test_user_principal_name_alt
44

5-
client = GraphClient(acquire_token_client_credentials)
5+
client = GraphClient(acquire_token_by_client_credentials)
66
message_json = {
77
"Message": {
88
"Subject": "Meet for lunch?",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from office365.sharepoint.client_context import ClientContext
2+
from office365.sharepoint.fields.field_creation_information import FieldCreationInformation
3+
from office365.sharepoint.fields.field_multi_user_value import FieldMultiUserValue
4+
from office365.sharepoint.fields.field_type import FieldType
5+
from office365.sharepoint.fields.field_user_value import FieldUserValue
6+
from office365.sharepoint.lists.list_creation_information import ListCreationInformation
7+
from office365.sharepoint.lists.list_template_type import ListTemplateType
8+
from tests import create_unique_name, test_site_url, test_client_credentials, test_user_principal_name
9+
10+
11+
def create_tasks_list(client):
12+
"""
13+
14+
:type client: ClientContext
15+
"""
16+
list_title = create_unique_name("Tasks N")
17+
list_create_info = ListCreationInformation(list_title,
18+
None,
19+
ListTemplateType.TasksWithTimelineAndHierarchy)
20+
21+
target_list = client.web.lists.add(list_create_info).execute_query()
22+
field_info = FieldCreationInformation("Manager", FieldType.User)
23+
user_field = target_list.fields.add(field_info).execute_query()
24+
return target_list
25+
26+
27+
ctx = ClientContext(test_site_url).with_credentials(test_client_credentials)
28+
tasks_list = create_tasks_list(ctx)
29+
30+
#user = ctx.web.current_user.get().execute_query()
31+
user = ctx.web.ensure_user(test_user_principal_name)
32+
multi_user_value = FieldMultiUserValue()
33+
multi_user_value.add(FieldUserValue.from_user(user))
34+
35+
item_to_create = tasks_list.add_item({
36+
"Title": "New Task",
37+
"AssignedTo": multi_user_value,
38+
"Manager": FieldUserValue.from_user(user)
39+
}).execute_query()
40+
41+
multi_user_value_alt = FieldMultiUserValue()
42+
multi_user_value_alt.add(FieldUserValue(user.id))
43+
44+
item_to_create_alt = tasks_list.add_item({
45+
"Title": "New Task 2",
46+
"AssignedTo": multi_user_value_alt
47+
}).execute_query()
48+

0 commit comments

Comments
 (0)