Skip to content

Commit bbf79ef

Browse files
committed
Add permissions and expiration date for public share
1 parent 3dce21e commit bbf79ef

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

owncloud/owncloud.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ def __init__(self, res):
4343
class PublicShare():
4444
"""Public share information"""
4545

46-
def __init__(self, share_id, target_file, link, token):
46+
def __init__(self, share_id, target_file, link, token, **kwargs):
4747
self.share_id = share_id
4848
self.target_file = target_file
4949
self.link = link
5050
self.token = token
51+
self.permissions = kwargs.get('permissions', 1)
52+
self.expiration_date = kwargs.get('expiration_date', None)
5153

5254
def __str__(self):
53-
return 'PublicShare(id=%i,path=%s,link=%s,token=%s)' % \
54-
(self.share_id, self.target_file, self.link, self.token)
55+
return 'PublicShare(id=%i,path=%s,link=%s,token=%s,permissions=%s,expiration_date=%s)' % \
56+
(self.share_id, self.target_file, self.link, self.token, self.permissions, self.expiration_date)
5557

5658

5759
class UserShare():
@@ -596,6 +598,7 @@ def update_share(self, share_id, **kwargs):
596598
:param perms: (int) update permissions (see share_file_with_user() below)
597599
:param password: (string) updated password for public link Share
598600
:param public_upload: (boolean) enable/disable public upload for public shares
601+
:param expiration_date: (optional) expiration date object, or string in format 'YYYY-MM-DD'
599602
:returns: True if the operation succeeded, False otherwise
600603
:raises: HTTPResponseError in case an HTTP error status was returned
601604
"""
@@ -609,6 +612,9 @@ def update_share(self, share_id, **kwargs):
609612
return False
610613
if not isinstance(share_id, int):
611614
return False
615+
expiration = kwargs.get('expiration_date', None)
616+
if expiration is not None:
617+
post_data['expireDate'] = self.__parse_expiration_date(expiration)
612618

613619
data = {}
614620
if perms:
@@ -660,6 +666,7 @@ def share_file_with_link(self, path, **kwargs):
660666
defaults to read only (1)
661667
:param public_upload (optional): allows users to upload files or folders
662668
:param password (optional): sets a password
669+
:param expiration_date: (optional) expiration date object, or string in format 'YYYY-MM-DD'
663670
http://doc.owncloud.org/server/6.0/admin_manual/sharing_api/index.html
664671
:returns: instance of :class:`PublicShare` with the share info
665672
or False if the operation failed
@@ -668,7 +675,7 @@ def share_file_with_link(self, path, **kwargs):
668675
perms = kwargs.get('perms', None)
669676
public_upload = kwargs.get('public_upload', 'false')
670677
password = kwargs.get('password', None)
671-
678+
expiration = kwargs.get('expiration_date', None)
672679

673680
path = self.__normalize_path(path)
674681
post_data = {
@@ -681,6 +688,8 @@ def share_file_with_link(self, path, **kwargs):
681688
post_data['password'] = password
682689
if perms:
683690
post_data['permissions'] = perms
691+
if expiration is not None:
692+
post_data['expiration'] = self.__parse_expiration_date(expiration)
684693

685694
res = self.__make_ocs_request(
686695
'POST',
@@ -692,11 +701,19 @@ def share_file_with_link(self, path, **kwargs):
692701
tree = ET.fromstring(res.content)
693702
self.__check_ocs_status(tree)
694703
data_el = tree.find('data')
704+
705+
expiration = None
706+
exp_el = data_el.find('expiration')
707+
if exp_el is not None and exp_el.text is not None and len(exp_el.text) > 0:
708+
expiration = int(exp_el.text)
709+
695710
return PublicShare(
696711
int(data_el.find('id').text),
697712
path,
698713
data_el.find('url').text,
699-
data_el.find('token').text
714+
data_el.find('token').text,
715+
permissions=int(data_el.find('permissions').text),
716+
expiration=expiration
700717
)
701718
raise HTTPResponseError(res)
702719

@@ -1371,6 +1388,22 @@ def __encode_string(s):
13711388
return s.encode('utf-8')
13721389
return s
13731390

1391+
@staticmethod
1392+
def __parse_expiration_date(date):
1393+
"""Converts the given datetime object into the format required
1394+
by the share API
1395+
1396+
:param date: datetime object
1397+
:returns: string encoded to use as expireDate parameter in the share API
1398+
"""
1399+
if date is None:
1400+
return None
1401+
1402+
if isinstance(date, datetime.datetime):
1403+
return date.strftime('YYYY-MM-DD')
1404+
1405+
return date
1406+
13741407
@staticmethod
13751408
def __check_ocs_status(tree, accepted_codes=[100]):
13761409
"""Checks the status code of an OCS request
@@ -1491,7 +1524,7 @@ def __strip_dav_path(self, path):
14911524
if path.startswith(self.__davpath):
14921525
return path[len(self.__davpath):]
14931526
return path
1494-
1527+
14951528
def __webdav_move_copy(self,remote_path_source,remote_path_target,operation):
14961529
"""Copies or moves a remote file or directory
14971530

owncloud/test/test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,16 @@ def test_share_with_link(self, file_name):
562562
path = self.test_root + file_name
563563
self.assertTrue(self.client.put_file_contents(path, 'hello world!'))
564564

565-
share_info = self.client.share_file_with_link(path, public_upload=True, password='1234')
565+
share_info = self.client.share_file_with_link(path, public_upload=True, password='1234', expiration_date='2150-02-04')
566566

567567
self.assertTrue(self.client.is_shared(path))
568568
self.assertTrue(isinstance(share_info, owncloud.PublicShare))
569569
self.assertTrue(type(share_info.share_id) is int)
570570
self.assertEquals(share_info.target_file, path)
571571
self.assertTrue(type(share_info.link) is str)
572572
self.assertTrue(type(share_info.token) is str)
573-
574-
573+
self.assertEquals(share_info.permissions, 7)
574+
self.assertEquals(share_info.expiration_date, '2150-02-04')
575575

576576
def test_share_with_link_non_existing_file(self):
577577
"""Test sharing a file with link"""

0 commit comments

Comments
 (0)