Skip to content

Commit

Permalink
Examples update & SharePoint model updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Apr 1, 2024
1 parent 615211c commit 1394511
Show file tree
Hide file tree
Showing 51 changed files with 402 additions and 103 deletions.
1 change: 0 additions & 1 deletion examples/onedrive/files/download.py
Expand Up @@ -8,7 +8,6 @@

from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username
from tests.graph_case import acquire_token_by_username_password

client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
Expand Down
1 change: 0 additions & 1 deletion examples/onedrive/files/send_sharing_invitation.py
Expand Up @@ -10,7 +10,6 @@

from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username
from tests.graph_case import acquire_token_by_username_password

file_name = "Financial Sample.xlsx"
client = GraphClient.with_username_and_password(
Expand Down
1 change: 0 additions & 1 deletion examples/onedrive/folders/list_files.py
Expand Up @@ -4,7 +4,6 @@
"""
from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username
from tests.graph_case import acquire_token_by_username_password

client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
Expand Down
6 changes: 4 additions & 2 deletions examples/onenote/create_page.py
Expand Up @@ -5,9 +5,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)

files = {}
with open("../data/Sample.html", "rb") as f, open(
Expand Down
10 changes: 6 additions & 4 deletions examples/outlook/calendars/share_with.py
Expand Up @@ -7,11 +7,13 @@
"""
from office365.graph_client import GraphClient
from office365.outlook.calendar.role_type import CalendarRoleType
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
my_cal = client.me.calendar
cal_perm = my_cal.calendar_permissions.add(
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)

cal_perm = client.me.calendar.calendar_permissions.add(
"samanthab@adatum.onmicrosoft.com", CalendarRoleType.read
).execute_query()
print(cal_perm)
6 changes: 4 additions & 2 deletions examples/outlook/events/delete.py
Expand Up @@ -3,9 +3,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
event_id = "--event id goes here--"
event_to_del = client.me.calendar.events[event_id]
event_to_del.delete_object().execute_query()
8 changes: 5 additions & 3 deletions examples/outlook/events/list.py
Expand Up @@ -4,11 +4,13 @@
https://learn.microsoft.com/en-us/graph/api/calendar-list-events?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
events = (
client.me.calendar.events.get().top(100).select(["subject", "body"]).execute_query()
client.me.calendar.events.get().top(10).select(["subject", "body"]).execute_query()
)
for event in events:
print(event.subject)
6 changes: 4 additions & 2 deletions examples/outlook/messages/create_draft_with_attachments.py
Expand Up @@ -4,11 +4,13 @@
import base64

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

with open(r"../../data/Sample.pdf", "rb") as f:
content = base64.b64encode(f.read()).decode()
client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
client.me.messages.add(
subject="Meet for lunch?",
body="The new cafeteria is open.",
Expand Down
6 changes: 4 additions & 2 deletions examples/outlook/messages/create_property.py
Expand Up @@ -8,9 +8,11 @@
import sys

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = client.me.messages.top(1).get().execute_query()
if len(messages) == 0:
sys.exit("No messages were found")
Expand Down
6 changes: 4 additions & 2 deletions examples/outlook/messages/create_subscription.py
Expand Up @@ -6,9 +6,11 @@
import datetime

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)

existing_subscriptions = client.subscriptions.get().execute_query()

Expand Down
6 changes: 4 additions & 2 deletions examples/outlook/messages/download.py
Expand Up @@ -9,9 +9,11 @@
import tempfile

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = client.me.messages.select(["id", "subject"]).top(1).get().execute_query()
with tempfile.TemporaryDirectory() as local_path:
for message in messages:
Expand Down
6 changes: 4 additions & 2 deletions examples/outlook/messages/get_with_body.py
Expand Up @@ -7,9 +7,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = client.me.messages.select(["subject", "body"]).top(10).get().execute_query()
for message in messages:
print(message.subject)
6 changes: 4 additions & 2 deletions examples/outlook/messages/list_new.py
Expand Up @@ -5,9 +5,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = (
client.me.mail_folders["Inbox"]
.messages.delta.change_type("created")
Expand Down
6 changes: 4 additions & 2 deletions examples/outlook/messages/mark_as_read.py
Expand Up @@ -7,9 +7,11 @@
import sys

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = client.me.messages.top(1).get().execute_query()
if len(messages) == 0:
sys.exit("No messages were found")
Expand Down
8 changes: 5 additions & 3 deletions examples/outlook/messages/move.py
Expand Up @@ -2,13 +2,15 @@
Move a message to another folder within the specified user's mailbox.
This creates a new copy of the message in the destination folder and removes the original message.
https://learn.microsoft.com/en-us/graph/api/message-move?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/message-move?view=graph-rest-1.0
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
folder_name = "Archive"
to_folder = client.me.mail_folders[folder_name]

Expand Down
6 changes: 4 additions & 2 deletions examples/outlook/messages/reply.py
Expand Up @@ -7,9 +7,11 @@
import sys

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = client.me.messages.top(1).get().execute_query()
if len(messages) == 0:
sys.exit("No messages were found")
Expand Down
8 changes: 5 additions & 3 deletions examples/outlook/messages/search.py
Expand Up @@ -5,10 +5,12 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
result = client.search.query_messages("Let's go for lunch").execute_query()
for item in result.value:
for hit in item.hitsContainers[0].hits:
print(hit.resource.properties.get("webLink"))
print(hit.resource.get("webLink"))
13 changes: 10 additions & 3 deletions examples/outlook/messages/send_with_attachment.py
Expand Up @@ -5,10 +5,17 @@
"""

from office365.graph_client import GraphClient
from tests import test_user_principal_name_alt
from tests.graph_case import acquire_token_by_username_password
from tests import (
test_client_id,
test_password,
test_tenant,
test_user_principal_name_alt,
test_username,
)

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
client.me.send_mail(
subject="Meet for lunch?",
body="The new cafeteria is open.",
Expand Down
14 changes: 11 additions & 3 deletions examples/outlook/messages/send_with_large_attachment.py
Expand Up @@ -5,15 +5,23 @@
"""

from office365.graph_client import GraphClient
from tests import test_user_principal_name_alt
from tests.graph_case import acquire_token_by_username_password
from tests import (
test_client_id,
test_password,
test_tenant,
test_user_principal_name_alt,
test_username,
)


def print_progress(range_pos):
# type: (int) -> None
print("{0} bytes uploaded".format(range_pos))


client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
local_path = "../../../tests/data/big_buck_bunny.mp4"
message = (
(
Expand Down
6 changes: 4 additions & 2 deletions examples/planner/create_task.py
Expand Up @@ -5,9 +5,11 @@
import sys

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
group = client.groups.get_by_name("My Sample Team").get().execute_query()
plans = group.planner.plans.get().execute_query()
if len(plans) == 0:
Expand Down
2 changes: 1 addition & 1 deletion examples/sharepoint/fields/create_lookup.py
Expand Up @@ -14,5 +14,5 @@
lookup_field_name="Title",
allow_multiple_values=True,
).execute_query()
print("Field {0} has been created", field.internal_name)
print("Field {0} has been created", field_name)
field.delete_object().execute_query() # clean up
13 changes: 11 additions & 2 deletions examples/sharepoint/files/delete.py
Expand Up @@ -5,6 +5,15 @@
from tests import test_team_site_url, test_user_credentials

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)
file_url = "Shared Documents/SharePoint User Guide.docx"
file_url = "Shared Documents/Financial Sample.xlsx"
file = ctx.web.get_file_by_server_relative_url(file_url)
file.delete_object().execute_query()
# file.recycle().execute_query()
# or delete permanently via delete_object:
# file.delete_object().execute_query()
print("Deleted file: {0}".format(file_url))


print("Print deleted files...")
result = ctx.web.get_recycle_bin_items().execute_query()
for recycle_bin_item in result:
print(recycle_bin_item)
9 changes: 9 additions & 0 deletions examples/sharepoint/webs/get_regional_settings.py
@@ -0,0 +1,9 @@
"""
Retrieves the locale settings of a site
"""
from office365.sharepoint.client_context import ClientContext
from tests import test_client_credentials, test_site_url

client = ClientContext(test_site_url).with_credentials(test_client_credentials)
result = client.web.regional_settings.get().execute_query()
print(result)
13 changes: 10 additions & 3 deletions examples/teams/create_team.py
Expand Up @@ -8,10 +8,17 @@
"""

from office365.graph_client import GraphClient
from tests import create_unique_name
from tests.graph_case import acquire_token_by_username_password
from tests import (
create_unique_name,
test_client_id,
test_password,
test_tenant,
test_username,
)

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
team_name = create_unique_name("Team")
print("Creating a team '{0}' ...".format(team_name))
team = client.teams.create(team_name).execute_query_and_wait()
Expand Down
6 changes: 4 additions & 2 deletions examples/teams/send_message.py
Expand Up @@ -6,9 +6,11 @@

from office365.graph_client import GraphClient
from office365.outlook.mail.item_body import ItemBody
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
my_teams = client.me.joined_teams.get().top(1).execute_query()
if len(my_teams) == 0:
sys.exit("No teams found")
Expand Down
3 changes: 2 additions & 1 deletion office365/sharepoint/alerts/collection.py
Expand Up @@ -5,7 +5,7 @@
from office365.sharepoint.entity_collection import EntityCollection


class AlertCollection(EntityCollection):
class AlertCollection(EntityCollection[Alert]):
"""Content Type resource collection"""

def __init__(self, context, resource_path=None):
Expand All @@ -26,6 +26,7 @@ def add(self, parameters):
return return_type

def contains(self, id_alert):
# type: (str) -> ClientResult[bool]
"""
Returns true if the given alert exists in the alert collection. False otherwise.
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/contenttypes/content_type.py
Expand Up @@ -20,7 +20,7 @@ class ContentType(Entity):
"""

def __str__(self):
return self.name
return self.name or self.entity_type_name

def __repr__(self):
return self.string_id or str(self.id) or self.entity_type_name
Expand Down

0 comments on commit 1394511

Please sign in to comment.