Skip to content

Commit

Permalink
Add permissions and expiration date for public share
Browse files Browse the repository at this point in the history
  • Loading branch information
PVince81 committed Aug 28, 2015
1 parent 3dce21e commit bbf79ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
45 changes: 39 additions & 6 deletions owncloud/owncloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
"""
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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',
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions owncloud/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,16 +562,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_date='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_date, '2150-02-04')

def test_share_with_link_non_existing_file(self):
"""Test sharing a file with link"""
Expand Down

0 comments on commit bbf79ef

Please sign in to comment.