Skip to content

Commit

Permalink
PR #240: bug fix to prevent File.from_url method from losing context
Browse files Browse the repository at this point in the history
  • Loading branch information
vvgrem@gmail.com authored and vvgrem@gmail.com committed Aug 4, 2020
1 parent 5525219 commit 5ae6400
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 22 deletions.
7 changes: 4 additions & 3 deletions examples/sharepoint/files/download_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext

ctx = ClientContext.connect_with_credentials(settings.get('url') + "/sites/team",
ClientCredential(settings.get('client_credentials').get('client_id'),
settings.get('client_credentials').get('client_secret')))
site_url = settings.get('url') + "/sites/team"
credentials = ClientCredential(settings.get('client_credentials').get('client_id'),
settings.get('client_credentials').get('client_secret'))

ctx = ClientContext(site_url).with_credentials(credentials)

file_url = '/sites/team/Shared Documents/big_buck_bunny.mp4'
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
Expand Down
15 changes: 7 additions & 8 deletions examples/sharepoint/files/download_file_from_url.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import os
import tempfile

from office365.runtime.auth.client_credential import ClientCredential
from settings import settings

from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.files.file import File

abs_file_url = "{site_url}sites/team/Shared Documents/sample.docx".format(site_url=settings.get('url'))
user_credentials = UserCredential(settings.get('user_credentials').get('username'),
settings.get('user_credentials').get('password'))
root_site_url = settings.get('url')
client_credentials = ClientCredential(settings.get('client_credentials').get('client_id'),
settings.get('client_credentials').get('client_secret'))

file_name = os.path.basename(abs_file_url)
abs_file_url = "{site_url}sites/team/Shared Documents/big_buck_bunny.mp4".format(site_url=root_site_url)
with tempfile.TemporaryDirectory() as local_path:
file_name = os.path.basename(abs_file_url)
with open(os.path.join(local_path, file_name), 'wb') as local_file:
file = File.from_url(abs_file_url).with_credentials(user_credentials).download(local_file).execute_query()
file = File.from_url(abs_file_url).with_credentials(client_credentials).download(local_file).execute_query()
print("'{0}' file has been downloaded into {1}".format(file.serverRelativeUrl, local_file.name))
7 changes: 7 additions & 0 deletions office365/runtime/client_object.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import copy

from office365.runtime.clientValue import ClientValue
from office365.runtime.odata.odata_query_options import QueryOptions

Expand Down Expand Up @@ -98,6 +100,11 @@ def ensure_property(self, name_or_names, action):
else:
action()

def clone_object(self):
result = copy.deepcopy(self)
result._context = self.context
return result

@property
def entity_type_name(self):
if self._entity_type_name is None:
Expand Down
5 changes: 2 additions & 3 deletions office365/runtime/client_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import copy

from office365.runtime.client_object import ClientObject


Expand Down Expand Up @@ -61,7 +59,8 @@ def __init__(self, entity_to_read, properties_to_include=None):
:type properties_to_include: list[str] or None
:type entity_to_read: office365.runtime.client_object.ClientObject
"""
binding_type = copy.deepcopy(entity_to_read)
binding_type = entity_to_read.clone_object()

super(ReadEntityQuery, self).__init__(binding_type, None, None, entity_to_read)
if properties_to_include:
self._include_properties(properties_to_include)
Expand Down
6 changes: 4 additions & 2 deletions office365/sharepoint/client_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,18 @@ def request_form_digest(self):
json_format.function_tag_name = "GetContextWebInformation"
self.get_pending_request().map_json(json, self._contextWebInformation, json_format)

def clone(self, url):
def clone(self, url, clear_queries=True):
"""
Creates a clone of ClientContext
:param bool clear_queries:
:param str url: Site Url
:return ClientContext
"""
ctx = copy.deepcopy(self)
ctx._base_url = url
ctx.clear_queries()
if clear_queries:
ctx.clear_queries()
return ctx

def _build_modification_query(self, request):
Expand Down
4 changes: 3 additions & 1 deletion tests/sharepoint/test_sharepoint_attachment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os.path
from io import BytesIO
from random import randint

from tests.sharepoint.sharepoint_case import SPTestCase

Expand All @@ -17,8 +18,9 @@ class TestListItemAttachment(SPTestCase):
@classmethod
def setUpClass(cls):
super(TestListItemAttachment, cls).setUpClass()
list_name = "Tasks" + str(randint(0, 10000))
cls.target_list = cls.ensure_list(cls.client.web,
ListCreationInformation("Tasks",
ListCreationInformation(list_name,
None,
ListTemplateType.Tasks))
item_properties = {'Title': 'Approval Task'}
Expand Down
2 changes: 1 addition & 1 deletion tests/sharepoint/test_sharepoint_tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from random import randint
from unittest import TestCase

from office365.sharepoint.tenant.tenant_settings import TenantSettings
from settings import settings

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.tenant.administration.siteProperties import SiteProperties
from office365.sharepoint.tenant.administration.sitePropertiesCollection import SitePropertiesCollection
from office365.sharepoint.tenant.administration.tenant import Tenant
from office365.sharepoint.tenant.tenant_settings import TenantSettings


class TestTenant(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/sharepoint/test_sharepoint_web.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from random import randint

from office365.sharepoint.lists.list_template_type import ListTemplateType
from settings import settings
from tests.sharepoint.sharepoint_case import SPTestCase

from office365.sharepoint.lists.list_template_type import ListTemplateType
from office365.sharepoint.permissions.basePermissions import BasePermissions
from office365.sharepoint.permissions.permissionKind import PermissionKind
from office365.sharepoint.principal.user import User
Expand Down
6 changes: 3 additions & 3 deletions tests/teams/test_graph_team.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import uuid
from time import sleep

from office365.runtime.client_request_exception import ClientRequestException
from tests.graph_case import GraphTestCase
from office365.graph.graph_client import GraphClient

from office365.graph.directory.group import Group
from office365.graph.directory.groupProfile import GroupProfile
from office365.runtime.client_request_exception import ClientRequestException


class TestGraphTeam(GraphTestCase):
Expand Down Expand Up @@ -45,7 +45,7 @@ def setUpClass(cls):
properties.securityEnabled = False
properties.mailEnabled = True
properties.groupTypes = ["Unified"]
#cls.target_group = cls.client.groups.add(properties)
# cls.target_group = cls.client.groups.add(properties)
cls.target_group = cls.ensure_group(cls.client, properties)
cls.client.execute_query()

Expand Down

0 comments on commit 5ae6400

Please sign in to comment.