Skip to content

Commit

Permalink
Initial support for sitepages namespace in OneDrive, upload_folder me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
vgrem committed Mar 9, 2024
1 parent 34ec506 commit c2e2f47
Show file tree
Hide file tree
Showing 32 changed files with 435 additions and 109 deletions.
1 change: 0 additions & 1 deletion examples/data/report #123.csv

This file was deleted.

1 change: 0 additions & 1 deletion examples/data/report '123.csv

This file was deleted.

File renamed without changes.
6 changes: 1 addition & 5 deletions examples/directory/applications/get_by_app_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
as Object ID and appId is referred to as Application (client) ID on the Azure portal.
"""
from office365.graph_client import GraphClient
from tests import (
test_client_id,
test_client_secret,
test_tenant,
)
from tests import test_client_id, test_client_secret, test_tenant

client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
app = client.applications.get_by_app_id(test_client_id).get().execute_query()
Expand Down
6 changes: 1 addition & 5 deletions examples/directory/applications/grant_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
"""

from office365.graph_client import GraphClient
from tests import (
test_admin_principal_name,
test_client_id,
test_tenant,
)
from tests import test_admin_principal_name, test_client_id, test_tenant

client = GraphClient.with_token_interactive(
test_tenant, test_client_id, test_admin_principal_name
Expand Down
6 changes: 1 addition & 5 deletions examples/directory/applications/revoke_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
"""

from office365.graph_client import GraphClient
from tests import (
test_admin_principal_name,
test_client_id,
test_tenant,
)
from tests import test_admin_principal_name, test_client_id, test_tenant

client = GraphClient.with_token_interactive(
test_tenant, test_client_id, test_admin_principal_name
Expand Down
7 changes: 1 addition & 6 deletions examples/directory/roles/for_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
https://learn.microsoft.com/en-us/graph/api/directoryrole-list?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests import (
test_client_id,
test_password,
test_tenant,
test_username,
)
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
Expand Down
7 changes: 1 addition & 6 deletions examples/directory/users/get_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
https://learn.microsoft.com/en-us/graph/api/user-list-licensedetails?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests import (
test_client_id,
test_password,
test_tenant,
test_username,
)
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
Expand Down
4 changes: 3 additions & 1 deletion examples/onedrive/files/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@

# local_path = "../../data/Financial Sample.xlsx"
local_path = "../../data/countries.json"
file = folder.upload_file(local_path).execute_query()
# file = folder.upload_file(local_path).execute_query()
with open(local_path, "rb") as f:
file = folder.upload_file(f).execute_query()
print("File {0} has been uploaded".format(file.web_url))
37 changes: 13 additions & 24 deletions examples/onedrive/folders/upload.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
"""
Demonstrates how to upload files from a local folder into OneDrive drive
"""

import os
from os.path import isfile, join

from office365.graph_client import GraphClient
from office365.runtime.client_request_exception import ClientRequestException
from tests.graph_case import acquire_token_by_username_password
from office365.onedrive.driveitems.driveItem import DriveItem
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
remote_drive = client.me.drive
local_path = "../../data"

for name in os.listdir(local_path):
path = join(local_path, name)
if isfile(path):
try:
with open(path, "rb") as local_file:
uploaded_file = remote_drive.root.upload_file(
local_file
).execute_query()
print("File '{0}' uploaded into '{1}'".format(path, uploaded_file.web_url))
except ClientRequestException as e:
print(
"An error occured while uploading a file {0}: {1}".format(
path, e.message
)
)
def print_progress(uploaded_file):
# type: (DriveItem)-> None
print("File has been uploaded into '{0}'".format(uploaded_file.web_url))


client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
local_path = "../../data"
target_folder = client.me.drive.root.get_by_path("Import")
target_folder.upload_folder(local_path, print_progress).execute_query()
32 changes: 32 additions & 0 deletions examples/onedrive/folders/upload_custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Demonstrates how to upload files from a local folder into OneDrive drive
"""

import os
from os.path import isfile, join

from office365.graph_client import GraphClient
from office365.runtime.client_request_exception import ClientRequestException
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
remote_drive = client.me.drive
local_path = "../../data"

for name in os.listdir(local_path):
path = join(local_path, name)
if isfile(path):
try:
with open(path, "rb") as local_file:
uploaded_file = remote_drive.root.upload_file(
local_file
).execute_query()
print("File '{0}' uploaded into '{1}'".format(path, uploaded_file.web_url))
except ClientRequestException as e:
print(
"An error occured while uploading a file {0}: {1}".format(
path, e.message
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
import os

from office365.sharepoint.client_context import ClientContext
from tests import (
test_cert_thumbprint,
test_client_id,
test_site_url,
test_tenant,
)
from tests import test_cert_thumbprint, test_client_id, test_site_url, test_tenant

cert_credentials = {
"tenant": test_tenant,
Expand Down
1 change: 0 additions & 1 deletion examples/sharepoint/files/download_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
file_url = "Shared Documents/Sample.pdf"
# file_url = "Shared Documents/report '123.csv"
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
with open(download_path, "wb") as local_file:
file = ctx.web.get_file_by_server_relative_url(file_url).get().execute_query()
Expand Down
2 changes: 1 addition & 1 deletion examples/sharepoint/files/permissions/assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)

client = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
file_url = "Shared Documents/report #123.csv"
file_url = "Shared Documents/Financial Sample.xlsx"


role_def = client.web.role_definitions.get_by_type(RoleType.Contributor)
Expand Down
2 changes: 1 addition & 1 deletion examples/sharepoint/files/permissions/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
)

client = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
file_url = "Shared Documents/report #123.csv"
file_url = "Shared Documents/Financial Sample.xlsx"

target_user = client.web.site_users.get_by_email(test_user_principal_name_alt)
target_file = client.web.get_file_by_server_relative_path(file_url)
Expand Down
2 changes: 1 addition & 1 deletion examples/sharepoint/files/permissions/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)

client = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
file_url = "Shared Documents/report #123.csv"
file_url = "Shared Documents/Financial Sample.xlsx"

target_user = client.web.site_users.get_by_email(test_user_principal_name_alt)
target_file = client.web.get_file_by_server_relative_path(file_url)
Expand Down
2 changes: 1 addition & 1 deletion examples/sharepoint/files/replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

path = "../../data/report #123.csv"
path = "../../data/report.csv"

print("Uploading a new file...")
with open(path, "rb") as f:
Expand Down
6 changes: 1 addition & 5 deletions examples/sharepoint/folders/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
"""
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,
)
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"
Expand Down
2 changes: 1 addition & 1 deletion examples/sharepoint/listitems/attachments/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
task_item = tasks_list.add_item({"Title": "New Task"}).execute_query()

# 2. read & upload attachment for a list item
path = "../../../data/report #123.csv"
path = "../../../data/Financial Sample.xlsx"
with open(path, "rb") as fh:
file_content = fh.read()
attachment_file_info = AttachmentCreationInformation(
Expand Down
6 changes: 1 addition & 5 deletions examples/sharepoint/listitems/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.fields.user_value import FieldUserValue
from tests import (
test_client_credentials,
test_team_site_url,
test_user_principal_name,
)
from tests import test_client_credentials, test_team_site_url, test_user_principal_name

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
tasks_list = ctx.web.lists.get_by_title("Company Tasks")
Expand Down
6 changes: 1 addition & 5 deletions examples/sharepoint/listitems/system_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.fields.user_value import FieldUserValue
from tests import (
test_client_credentials,
test_site_url,
test_user_principal_name,
)
from tests import test_client_credentials, test_site_url, test_user_principal_name

ctx = ClientContext(test_site_url).with_credentials(test_client_credentials)

Expand Down
6 changes: 1 addition & 5 deletions examples/sharepoint/pages/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
"""

from office365.sharepoint.client_context import ClientContext
from tests import (
create_unique_name,
test_team_site_url,
test_user_credentials,
)
from tests import create_unique_name, test_team_site_url, test_user_credentials

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
page_title = create_unique_name("Site Page ")
Expand Down
6 changes: 1 addition & 5 deletions examples/sharepoint/pages/create_and_publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
"""

from office365.sharepoint.client_context import ClientContext
from tests import (
create_unique_name,
test_team_site_url,
test_user_credentials,
)
from tests import create_unique_name, test_team_site_url, test_user_credentials

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
page_title = create_unique_name("Site Page ")
Expand Down
6 changes: 1 addition & 5 deletions examples/sharepoint/search/search_site_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
"""

from office365.sharepoint.client_context import ClientContext
from tests import (
test_site_url,
test_team_site_url,
test_user_credentials,
)
from tests import test_site_url, test_team_site_url, test_user_credentials

ctx = ClientContext(test_site_url).with_credentials(test_user_credentials)

Expand Down
Loading

0 comments on commit c2e2f47

Please sign in to comment.