Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions owncloud/owncloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ def __init__(self, res):
class PublicShare():
"""Public share information"""

def __init__(self, share_id, target_file, link, token):
def __init__(self, share_id, target_file, link, token, **kwargs):
self.share_id = share_id
self.target_file = target_file
self.link = link
self.token = token
self.permissions = kwargs.get('permissions', None)
self.expiration = kwargs.get('expiration', None)

def __str__(self):
return 'PublicShare(id=%i,path=%s,link=%s,token=%s)' % \
(self.share_id, self.target_file, self.link, self.token)
return 'PublicShare(id=%i,path=%s,link=%s,token=%s,permissions=%s,expiration=%s)' % \
(self.share_id, self.target_file, self.link, self.token, self.permissions, self.expiration)


class UserShare():
Expand Down Expand Up @@ -613,16 +615,18 @@ def update_share(self, share_id, **kwargs):
:param perms: (int) update permissions (see share_file_with_user() below)
:param password: (string) updated password for public link Share
:param public_upload: (boolean) enable/disable public upload for public shares
:param expiration: (optional) expiration date object, or string in format 'YYYY-MM-DD'
:returns: True if the operation succeeded, False otherwise
:raises: HTTPResponseError in case an HTTP error status was returned
"""

perms = kwargs.get('perms', None)
password = kwargs.get('password', None)
public_upload = kwargs.get('public_upload', None)
expiration = kwargs.get('expiration', None)
if (isinstance(perms, int)) and (perms > self.OCS_PERMISSION_ALL):
perms = None
if not (perms or password or (public_upload is not None)):
if not (perms or password or (public_upload is not None) or (expiration is not None)):
return False
if not isinstance(share_id, int):
return False
Expand All @@ -634,6 +638,8 @@ def update_share(self, share_id, **kwargs):
data['password'] = password
if (public_upload is not None) and (isinstance(public_upload, bool)):
data['publicUpload'] = str(public_upload).lower()
if expiration is not None:
data['expireDate'] = self.__parse_expiration_date(expiration)

res = self.__make_ocs_request(
'PUT',
Expand Down Expand Up @@ -677,15 +683,16 @@ def share_file_with_link(self, path, **kwargs):
defaults to read only (1)
:param public_upload (optional): allows users to upload files or folders
:param password (optional): sets a password
:param expiration: (optional) expiration date object, or string in format 'YYYY-MM-DD'
http://doc.owncloud.org/server/6.0/admin_manual/sharing_api/index.html
:returns: instance of :class:`PublicShare` with the share info
or False if the operation failed
:raises: HTTPResponseError in case an HTTP error status was returned
"""
perms = kwargs.get('perms', None)
public_upload = kwargs.get('public_upload', 'false')
password = kwargs.get('password', None)

password = kwargs.get('password', None)
expiration = kwargs.get('expiration', None)

path = self.__normalize_path(path)
post_data = {
Expand All @@ -698,6 +705,8 @@ def share_file_with_link(self, path, **kwargs):
post_data['password'] = password
if perms:
post_data['permissions'] = perms
if expiration is not None:
post_data['expireDate'] = self.__parse_expiration_date(expiration)

res = self.__make_ocs_request(
'POST',
Expand All @@ -709,11 +718,24 @@ def share_file_with_link(self, path, **kwargs):
tree = ET.fromstring(res.content)
self.__check_ocs_status(tree)
data_el = tree.find('data')

expiration = None
exp_el = data_el.find('expiration')
if exp_el is not None and exp_el.text is not None and len(exp_el.text) > 0:
expiration = exp_el.text

permissions = None
perms_el = data_el.find('permissions')
if perms_el is not None and perms_el.text is not None and len(perms_el.text) > 0:
permissions = int(perms_el.text)

return PublicShare(
int(data_el.find('id').text),
path,
data_el.find('url').text,
data_el.find('token').text
data_el.find('token').text,
permissions=permissions,
expiration=expiration
)
raise HTTPResponseError(res)

Expand Down Expand Up @@ -834,9 +856,9 @@ def user_exists(self, user_name):
"""Checks a user via provisioning API.
If you get back an error 999, then the provisioning API is not enabled.

:param user_name: name of user to be checked
:returns: True if user found
:param user_name: name of user to be checked
:returns: True if user found

"""
users=self.search_users(user_name)

Expand All @@ -861,7 +883,7 @@ def search_users(self, user_name):
tree = ET.fromstring(res.text)
users = [x.text for x in tree.findall('data/users/element')]

return users
return users

raise HTTPResponseError(res)

Expand Down Expand Up @@ -1407,6 +1429,22 @@ def __encode_string(s):
return s.encode('utf-8')
return s

@staticmethod
def __parse_expiration_date(date):
"""Converts the given datetime object into the format required
by the share API

:param date: datetime object
:returns: string encoded to use as expireDate parameter in the share API
"""
if date is None:
return None

if isinstance(date, datetime.datetime):
return date.strftime('YYYY-MM-DD')

return date

@staticmethod
def __check_ocs_status(tree, accepted_codes=[100]):
"""Checks the status code of an OCS request
Expand Down Expand Up @@ -1547,7 +1585,7 @@ def __strip_dav_path(self, path):
if path.startswith(self.__davpath):
return path[len(self.__davpath):]
return path

def __webdav_move_copy(self,remote_path_source,remote_path_target,operation):
"""Copies or moves a remote file or directory

Expand Down
17 changes: 16 additions & 1 deletion owncloud/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,14 +573,16 @@ def test_share_with_link(self, file_name):
path = self.test_root + file_name
self.assertTrue(self.client.put_file_contents(path, 'hello world!'))

share_info = self.client.share_file_with_link(path, public_upload=True, password='1234')
share_info = self.client.share_file_with_link(path, public_upload=True, password='1234', expiration='2150-02-04')

self.assertTrue(self.client.is_shared(path))
self.assertTrue(isinstance(share_info, owncloud.PublicShare))
self.assertTrue(type(share_info.share_id) is int)
self.assertEquals(share_info.target_file, path)
self.assertTrue(type(share_info.link) is str)
self.assertTrue(type(share_info.token) is str)
self.assertEquals(share_info.permissions, 7)
self.assertEquals(share_info.expiration, '2150-02-04 00:00:00')

def test_share_with_link_non_existing_file(self):
"""Test sharing a file with link"""
Expand Down Expand Up @@ -769,6 +771,19 @@ def test_update_share_password(self):
self.assertIsNotNone(share_info['share_with_displayname'])
self.assertTrue(self.client.delete_share(share_id))

def test_update_share_expiration(self):
"""Test updating a share parameters - expiration date"""
path = self.test_root + 'update_share_expiration'
self.client.mkdir(path)

share_info = self.client.share_file_with_link(path)
share_id = share_info.share_id
self.assertTrue(self.client.update_share(share_id, expiration='2150-02-04'))
share_info = self.client.get_shares(path)[0]
self.assertTrue('expiration' in share_info)
self.assertEquals(share_info['expiration'], '2150-02-04 00:00:00')
self.assertTrue(self.client.delete_share(share_id))


class TestPrivateDataAccess(unittest.TestCase):

Expand Down