From bbf79ef52b132f82d00d1a420a0fdfc1fc4469ea Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 28 Aug 2015 19:03:19 +0200 Subject: [PATCH] Add permissions and expiration date for public share --- owncloud/owncloud.py | 45 +++++++++++++++++++++++++++++++++++++------ owncloud/test/test.py | 6 +++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/owncloud/owncloud.py b/owncloud/owncloud.py index 3c92f43..492b386 100644 --- a/owncloud/owncloud.py +++ b/owncloud/owncloud.py @@ -43,15 +43,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', 1) + self.expiration_date = kwargs.get('expiration_date', 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_date=%s)' % \ + (self.share_id, self.target_file, self.link, self.token, self.permissions, self.expiration_date) class UserShare(): @@ -596,6 +598,7 @@ 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_date: (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 """ @@ -609,6 +612,9 @@ def update_share(self, share_id, **kwargs): return False if not isinstance(share_id, int): return False + expiration = kwargs.get('expiration_date', None) + if expiration is not None: + post_data['expireDate'] = self.__parse_expiration_date(expiration) data = {} if perms: @@ -660,6 +666,7 @@ 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_date: (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 @@ -668,7 +675,7 @@ def share_file_with_link(self, path, **kwargs): perms = kwargs.get('perms', None) public_upload = kwargs.get('public_upload', 'false') password = kwargs.get('password', None) - + expiration = kwargs.get('expiration_date', None) path = self.__normalize_path(path) post_data = { @@ -681,6 +688,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['expiration'] = self.__parse_expiration_date(expiration) res = self.__make_ocs_request( 'POST', @@ -692,11 +701,19 @@ 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 = int(exp_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=int(data_el.find('permissions').text), + expiration=expiration ) raise HTTPResponseError(res) @@ -1371,6 +1388,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 @@ -1491,7 +1524,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 diff --git a/owncloud/test/test.py b/owncloud/test/test.py index 1affd33..69c5ddd 100644 --- a/owncloud/test/test.py +++ b/owncloud/test/test.py @@ -562,7 +562,7 @@ 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_date='2150-02-04') self.assertTrue(self.client.is_shared(path)) self.assertTrue(isinstance(share_info, owncloud.PublicShare)) @@ -570,8 +570,8 @@ def test_share_with_link(self, file_name): 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_date, '2150-02-04') def test_share_with_link_non_existing_file(self): """Test sharing a file with link"""