From 2c0c2ecf1993bc375e0ddd9d4b58a6a43dc3cdf0 Mon Sep 17 00:00:00 2001 From: vgrem Date: Fri, 23 Feb 2024 23:55:37 +0200 Subject: [PATCH] refactorings & examples updates --- examples/onedrive/shares/read_workbook.py | 13 +- examples/onedrive/sites/list_permission.py | 15 -- examples/onedrive/sites/list_permissions.py | 7 +- examples/onedrive/sites/list_sites.py | 4 +- examples/onedrive/sites/revoke_permission.py | 14 +- examples/sharepoint/folders/share.py | 18 ++ .../sharepoint/taxonomy/set_field_value.py | 26 +-- generator/metadata/SharePoint.xml | 171 +++++++++++++----- office365/graph_client.py | 2 +- office365/onedrive/permissions/collection.py | 15 +- office365/onedrive/sites/sites_with_root.py | 4 +- office365/runtime/client_runtime_context.py | 14 +- office365/sharepoint/features/collection.py | 3 +- office365/sharepoint/fields/collection.py | 7 +- office365/sharepoint/listitems/listitem.py | 4 +- office365/sharepoint/sharing/result.py | 1 + office365/sharepoint/taxonomy/groups/group.py | 2 +- 17 files changed, 202 insertions(+), 118 deletions(-) delete mode 100644 examples/onedrive/sites/list_permission.py create mode 100644 examples/sharepoint/folders/share.py diff --git a/examples/onedrive/shares/read_workbook.py b/examples/onedrive/shares/read_workbook.py index 1f6b5e53..e2c0da05 100644 --- a/examples/onedrive/shares/read_workbook.py +++ b/examples/onedrive/shares/read_workbook.py @@ -2,12 +2,19 @@ Get workbook """ from office365.graph_client import GraphClient -from tests import test_team_site_url -from tests.graph_case import acquire_token_by_username_password +from tests import ( + test_client_id, + test_password, + test_team_site_url, + test_tenant, + test_username, +) file_abs_url = "{0}/Shared Documents/Financial Sample.xlsx".format(test_team_site_url) -client = GraphClient(acquire_token_by_username_password) +client = GraphClient.with_username_and_password( + test_tenant, test_client_id, test_username, test_password +) drive_item = client.shares.by_url(file_abs_url).drive_item.get().execute_query() worksheets = drive_item.workbook.worksheets.get().execute_query() for ws in worksheets: diff --git a/examples/onedrive/sites/list_permission.py b/examples/onedrive/sites/list_permission.py deleted file mode 100644 index 8b1871fc..00000000 --- a/examples/onedrive/sites/list_permission.py +++ /dev/null @@ -1,15 +0,0 @@ -""" -Lists site permissions. - -""" - -from office365.graph_client import GraphClient -from tests import test_team_site_url -from tests.graph_case import acquire_token_by_client_credentials - -client = GraphClient(acquire_token_by_client_credentials) - -site = client.sites.get_by_url(test_team_site_url) -permissions = site.permissions.get().execute_query() -for perm in permissions: - print(perm.granted_to) diff --git a/examples/onedrive/sites/list_permissions.py b/examples/onedrive/sites/list_permissions.py index 622895e4..94101aa9 100644 --- a/examples/onedrive/sites/list_permissions.py +++ b/examples/onedrive/sites/list_permissions.py @@ -4,12 +4,11 @@ """ from office365.graph_client import GraphClient -from tests import test_team_site_url -from tests.graph_case import acquire_token_by_client_credentials +from tests import test_client_id, test_client_secret, test_team_site_url, test_tenant -client = GraphClient(acquire_token_by_client_credentials) +client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret) permissions = ( client.sites.get_by_url(test_team_site_url).permissions.get().execute_query() ) for perm in permissions: - print(perm.granted_to) + print("Roles: {0}, Granted to: {1}".format(perm.roles, perm.granted_to)) diff --git a/examples/onedrive/sites/list_sites.py b/examples/onedrive/sites/list_sites.py index 2d2389f8..6736a3e0 100644 --- a/examples/onedrive/sites/list_sites.py +++ b/examples/onedrive/sites/list_sites.py @@ -5,9 +5,9 @@ """ from office365.graph_client import GraphClient -from tests.graph_case import acquire_token_by_client_credentials +from tests import test_client_id, test_client_secret, test_tenant -client = GraphClient(acquire_token_by_client_credentials) +client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret) sites = client.sites.paged(100).get().execute_query() for site in sites: print("Site url: {0}".format(site.web_url)) diff --git a/examples/onedrive/sites/revoke_permission.py b/examples/onedrive/sites/revoke_permission.py index c4b3b324..a7348230 100644 --- a/examples/onedrive/sites/revoke_permission.py +++ b/examples/onedrive/sites/revoke_permission.py @@ -5,12 +5,16 @@ """ from office365.graph_client import GraphClient -from tests import test_client_credentials, test_team_site_url -from tests.graph_case import acquire_token_by_client_credentials +from tests import ( + test_client_credentials, + test_client_id, + test_client_secret, + test_team_site_url, + test_tenant, +) -client = GraphClient(acquire_token_by_client_credentials) +client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret) app = client.applications.get_by_app_id(test_client_credentials.clientId) site = client.sites.get_by_url(test_team_site_url) -site.permissions.delete(["write"], app).execute_query() -# site.permissions.delete_all().execute_query() +site.permissions.delete_all().execute_query() diff --git a/examples/sharepoint/folders/share.py b/examples/sharepoint/folders/share.py new file mode 100644 index 00000000..4dc9fc95 --- /dev/null +++ b/examples/sharepoint/folders/share.py @@ -0,0 +1,18 @@ +""" +Shares a folder +""" +from office365.sharepoint.client_context import ClientContext +from office365.sharepoint.sharing.external_site_option import ExternalSharingSiteOption +from tests import ( + test_team_site_url, + test_user_credentials, + test_user_principal_name, +) + +ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials) +folder_url = "Shared Documents/Archive" +folder = ctx.web.get_folder_by_server_relative_path(folder_url) +result = folder.list_item_all_fields.share( + test_user_principal_name, ExternalSharingSiteOption.View +).execute_query() +print(result.url) diff --git a/examples/sharepoint/taxonomy/set_field_value.py b/examples/sharepoint/taxonomy/set_field_value.py index 241e38f0..1fcf371f 100644 --- a/examples/sharepoint/taxonomy/set_field_value.py +++ b/examples/sharepoint/taxonomy/set_field_value.py @@ -10,29 +10,33 @@ from tests import create_unique_name, test_client_credentials, test_team_site_url ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials) +custom_list = ctx.web.lists.get_by_title("Requests").get().execute_query() -print("Creating a custom list...") -custom_list = ctx.web.add_list(create_unique_name("Custom List")).execute_query() - -print("Adding a taxonomy field into list '{0}'...".format(custom_list.title)) term_set_id = "3b712032-95c4-4bb5-952d-f85ae9288f99" +tax_field_name = create_unique_name("Country") +mult_tax_field_name = create_unique_name("Countries") + +print("1. Adding a taxonomy field into list '{0}'...".format(custom_list.title)) tax_field = custom_list.fields.create_taxonomy_field( - "Country", term_set_id + tax_field_name, term_set_id ).execute_query() multi_tax_field = custom_list.fields.create_taxonomy_field( - "Countries", term_set_id, allow_multiple_values=True + mult_tax_field_name, term_set_id, allow_multiple_values=True ).execute_query() -print("Creating a list item and setting a taxonomy field value ...") +print("2. Creating a list item and setting a taxonomy field value ...") item = custom_list.add_item( { "Title": "New item", - "Country": TaxonomyFieldValue("Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73"), - "Countries": TaxonomyFieldValueCollection( + tax_field_name: TaxonomyFieldValue( + "Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73" + ), + mult_tax_field_name: TaxonomyFieldValueCollection( [TaxonomyFieldValue("Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73")] ), } ).execute_query() -print("Cleaning up temporary resources...") -custom_list.delete_object().execute_query() +print("3. Deleting tax fields ...") +tax_field.delete_object().execute_query() +multi_tax_field.delete_object().execute_query() diff --git a/generator/metadata/SharePoint.xml b/generator/metadata/SharePoint.xml index d12f091e..e3964c9e 100644 --- a/generator/metadata/SharePoint.xml +++ b/generator/metadata/SharePoint.xml @@ -168,24 +168,41 @@ + + + + + + + + + + + + + + + + + @@ -1335,6 +1352,11 @@ + + + + + @@ -1383,14 +1405,23 @@ + + + + + + + + + - - - - + + + + @@ -1403,11 +1434,6 @@ - - - - - @@ -2656,6 +2682,7 @@ + @@ -3530,6 +3557,15 @@ + + + + + + + + + @@ -4520,6 +4556,7 @@ + @@ -4746,6 +4783,7 @@ + @@ -5772,9 +5810,6 @@ - - - @@ -9856,6 +9891,7 @@ + @@ -11346,6 +11382,13 @@ + + + + + + + @@ -13196,6 +13239,9 @@ + + + @@ -16124,7 +16170,7 @@ - + @@ -16532,7 +16578,7 @@ - + @@ -17359,6 +17405,10 @@ + + + + @@ -19626,15 +19676,6 @@ - - - - - - - - - @@ -20661,6 +20702,15 @@ + + + + + + + + + @@ -20739,9 +20789,6 @@ - - - @@ -21360,6 +21407,9 @@ + + + @@ -22041,6 +22091,9 @@ + + + @@ -23494,6 +23547,9 @@ + + + @@ -23554,6 +23610,9 @@ + + + @@ -24385,9 +24444,6 @@ - - - @@ -25999,6 +26055,12 @@ + + + + + + @@ -26398,6 +26460,9 @@ + + + @@ -27406,10 +27471,6 @@ - - - - @@ -27640,6 +27701,7 @@ + @@ -27905,6 +27967,7 @@ + @@ -34060,6 +34123,12 @@ + + + + + + @@ -34196,6 +34265,19 @@ + + + + + + + + + + + + + @@ -34209,6 +34291,12 @@ + + + + + + @@ -34228,6 +34316,10 @@ + + + + @@ -34696,10 +34788,6 @@ - - - - @@ -35234,6 +35322,7 @@ + @@ -36270,10 +36359,10 @@ - - - - + + + + diff --git a/office365/graph_client.py b/office365/graph_client.py index cb85dbab..50f00ff1 100644 --- a/office365/graph_client.py +++ b/office365/graph_client.py @@ -149,7 +149,7 @@ def with_client_secret( :param str client_secret: Client secret :param list[str] or None scopes: Scopes requested to access an API :param Any token_cache: Default cache is in memory only, - Refer https://msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache + Refer https://msal-python.readthedocs.io/en/latest/#msal.SerializableTokenCache """ if scopes is None: scopes = ["https://graph.microsoft.com/.default"] diff --git a/office365/onedrive/permissions/collection.py b/office365/onedrive/permissions/collection.py index f4524664..0d8f1c3a 100644 --- a/office365/onedrive/permissions/collection.py +++ b/office365/onedrive/permissions/collection.py @@ -69,19 +69,6 @@ def _add(): identity.ensure_properties(["displayName"], _add) return return_type - def delete(self, roles, identity): - # type: (list[str], Application|User|Group|Device) -> Permission - """Deletes the permission.""" - - def _delete(col): - pass - - def _identity_loaded(): - self.get_all(page_loaded=_delete) - - identity.ensure_property("id", _identity_loaded) - return self - def delete_all(self): """Remove all access to resource""" @@ -90,5 +77,5 @@ def _after_loaded(return_type): for permission in return_type: permission.delete_object() - self.context.load(self, after_loaded=_after_loaded) + self.get().after_execute(_after_loaded) return self diff --git a/office365/onedrive/sites/sites_with_root.py b/office365/onedrive/sites/sites_with_root.py index 10bf1a81..991de784 100644 --- a/office365/onedrive/sites/sites_with_root.py +++ b/office365/onedrive/sites/sites_with_root.py @@ -15,9 +15,7 @@ def __init__(self, context, resource_path=None): super(SitesWithRoot, self).__init__(context, Site, resource_path) def get_all_sites(self): - """ - List root sites across geographies in an organization. - """ + """List root sites across geographies in an organization.""" return_type = SitesWithRoot(self.context) qry = FunctionQuery(self, "getAllSites", return_type=return_type) self.context.add_query(qry) diff --git a/office365/runtime/client_runtime_context.py b/office365/runtime/client_runtime_context.py index 77a1a832..4dfb43ab 100644 --- a/office365/runtime/client_runtime_context.py +++ b/office365/runtime/client_runtime_context.py @@ -80,21 +80,11 @@ def service_root_url(self): # type: () -> str pass - def load( - self, - client_object, - properties_to_retrieve=None, - before_loaded=None, - after_loaded=None, - ): - # type: (T, List[str], Callable[[RequestOptions], None], Callable[[T], None]) -> Self + def load(self, client_object, properties_to_retrieve=None): + # type: (T, List[str]) -> Self """Prepare retrieval query""" qry = ReadEntityQuery(client_object, properties_to_retrieve) self.add_query(qry) - if callable(before_loaded): - self.before_query_execute(before_loaded) - if callable(after_loaded): - self.after_query_execute(after_loaded, client_object) return self def before_query_execute(self, action, once=True): diff --git a/office365/sharepoint/features/collection.py b/office365/sharepoint/features/collection.py index 4aec99fb..dbb779d3 100644 --- a/office365/sharepoint/features/collection.py +++ b/office365/sharepoint/features/collection.py @@ -36,8 +36,7 @@ def _create_if_not_activated(f): self.context.add_query(_create_query()) if verify_if_activated: - feature = self.get_by_id(feature_id) - self.context.load(feature, after_loaded=_create_if_not_activated) + self.get_by_id(feature_id).get().after_execute(_create_if_not_activated) else: self.context.add_query(_create_query()) diff --git a/office365/sharepoint/fields/collection.py b/office365/sharepoint/fields/collection.py index 8eea5c50..759caec0 100644 --- a/office365/sharepoint/fields/collection.py +++ b/office365/sharepoint/fields/collection.py @@ -18,6 +18,7 @@ ) from office365.sharepoint.taxonomy.field import TaxonomyField from office365.sharepoint.taxonomy.sets.set import TermSet +from office365.sharepoint.taxonomy.stores.store import TermStore class FieldCollection(EntityCollection[Field]): @@ -267,6 +268,7 @@ def _term_set_loaded(): else: def _term_store_loaded(term_store): + # type: (TermStore) -> None TaxonomyField.create( self, name, @@ -276,9 +278,10 @@ def _term_store_loaded(term_store): return_type=return_type, ) - self.context.load( - self.context.taxonomy.term_store, after_loaded=_term_store_loaded + self.context.load(self.context.taxonomy.term_store).after_query_execute( + _term_store_loaded ) + return return_type def create_field_as_xml(self, schema_xml, return_type=None): diff --git a/office365/sharepoint/listitems/listitem.py b/office365/sharepoint/listitems/listitem.py index 930f929f..bbb572b3 100644 --- a/office365/sharepoint/listitems/listitem.py +++ b/office365/sharepoint/listitems/listitem.py @@ -212,13 +212,13 @@ def share( def _picker_value_resolved(picker_result): # type: (ClientResult) -> None - file_abs_url = self.get_property("EncodedAbsUrl") + abs_url = self.get_property("EncodedAbsUrl") picker_value = "[{0}]".format(picker_result.value) from office365.sharepoint.webs.web import Web Web.share_object( self.context, - file_abs_url, + abs_url, picker_value, role_values[share_option], 0, diff --git a/office365/sharepoint/sharing/result.py b/office365/sharepoint/sharing/result.py index 205fb2ac..5a1839be 100644 --- a/office365/sharepoint/sharing/result.py +++ b/office365/sharepoint/sharing/result.py @@ -91,6 +91,7 @@ def get_property(self, name, default_value=None): property_mapping = { "GroupsSharedWith": self.groups_shared_with, "UsersAddedToGroup": self.users_added_to_group, + "UniquelyPermissionedUsers": self.uniquely_permissioned_users, } default_value = property_mapping.get(name, None) return super(SharingResult, self).get_property(name, default_value) diff --git a/office365/sharepoint/taxonomy/groups/group.py b/office365/sharepoint/taxonomy/groups/group.py index fcf1bf8a..16f8f8c9 100644 --- a/office365/sharepoint/taxonomy/groups/group.py +++ b/office365/sharepoint/taxonomy/groups/group.py @@ -31,7 +31,7 @@ def _sets_loaded(col): ] def _group_resolved(): - self.context.load(self.term_sets, after_loaded=_sets_loaded) + self.term_sets.get().after_execute(_sets_loaded) self.ensure_property("id", _group_resolved) return return_type