Skip to content

Commit 7cb35e5

Browse files
Adapted PR 120 to new structure
1 parent 4bb5b53 commit 7cb35e5

File tree

2 files changed

+70
-14
lines changed

2 files changed

+70
-14
lines changed

owncloud/owncloud.py

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,27 @@ def get_share_time(self):
129129
return datetime.datetime.fromtimestamp(
130130
self.__get_int('stime')
131131
)
132-
133-
def get_expiration(self):
132+
133+
def get_expiration_datetime(self):
134134
"""Returns the expiration date.
135135
136136
:returns: expiration date
137137
:rtype: datetime object
138138
"""
139-
exp = self.__get_int('expiration')
139+
exp = self.share_info['expiration']
140140
if exp is not None:
141-
return datetime.datetime.fromtimestamp(
142-
exp
143-
)
141+
return datetime.datetime.strptime(exp, '%Y-%m-%d %H:%M:%S')
142+
return None
143+
144+
def get_expiration(self):
145+
"""Returns the expiration date.
146+
147+
:returns: expiration date in YYYY-MM-DD hh:mm:ss format
148+
:rtype: string
149+
"""
150+
exp = self.share_info['expiration']
151+
if exp is not None:
152+
return exp
144153
return None
145154

146155
def get_token(self):
@@ -727,9 +736,10 @@ def update_share(self, share_id, **kwargs):
727736
perms = kwargs.get('perms', None)
728737
password = kwargs.get('password', None)
729738
public_upload = kwargs.get('public_upload', None)
739+
expiration = kwargs.get('expiration', None)
730740
if (isinstance(perms, int)) and (perms > self.OCS_PERMISSION_ALL):
731741
perms = None
732-
if not (perms or password or (public_upload is not None)):
742+
if not (perms or password or (public_upload is not None) or (expiration is not None)):
733743
return False
734744
if not isinstance(share_id, int):
735745
return False
@@ -741,6 +751,8 @@ def update_share(self, share_id, **kwargs):
741751
data['password'] = password
742752
if (public_upload is not None) and (isinstance(public_upload, bool)):
743753
data['publicUpload'] = str(public_upload).lower()
754+
if expiration is not None:
755+
data['expireDate'] = self.__parse_expiration_date(expiration)
744756

745757
res = self.__make_ocs_request(
746758
'PUT',
@@ -791,7 +803,8 @@ def share_file_with_link(self, path, **kwargs):
791803
"""
792804
perms = kwargs.get('perms', None)
793805
public_upload = kwargs.get('public_upload', 'false')
794-
password = kwargs.get('password', None)
806+
password = kwargs.get('password', None)
807+
expiration = kwargs.get('expiration', None)
795808

796809

797810
path = self.__normalize_path(path)
@@ -805,6 +818,8 @@ def share_file_with_link(self, path, **kwargs):
805818
post_data['password'] = password
806819
if perms:
807820
post_data['permissions'] = perms
821+
if expiration is not None:
822+
post_data['expireDate'] = self.__parse_expiration_date(expiration)
808823

809824
res = self.__make_ocs_request(
810825
'POST',
@@ -816,12 +831,25 @@ def share_file_with_link(self, path, **kwargs):
816831
tree = ET.fromstring(res.content)
817832
self.__check_ocs_status(tree)
818833
data_el = tree.find('data')
834+
835+
expiration = None
836+
exp_el = data_el.find('expiration')
837+
if exp_el is not None and exp_el.text is not None and len(exp_el.text) > 0:
838+
expiration = exp_el.text
839+
840+
permissions = None
841+
perms_el = data_el.find('permissions')
842+
if perms_el is not None and perms_el.text is not None and len(perms_el.text) > 0:
843+
permissions = int(perms_el.text)
844+
819845
return ShareInfo(
820846
{
821847
'id': data_el.find('id').text,
822848
'path':path,
823849
'link': data_el.find('url').text,
824-
'token': data_el.find('token').text
850+
'token': data_el.find('token').text,
851+
'expiration': expiration,
852+
'permissions':permissions
825853
}
826854
)
827855
raise HTTPResponseError(res)
@@ -965,8 +993,8 @@ def user_exists(self, user_name):
965993
"""Checks a user via provisioning API.
966994
If you get back an error 999, then the provisioning API is not enabled.
967995
968-
:param user_name: name of user to be checked
969-
:returns: True if user found
996+
:param user_name: name of user to be checked
997+
:returns: True if user found
970998
971999
"""
9721000
users=self.search_users(user_name)
@@ -992,7 +1020,7 @@ def search_users(self, user_name):
9921020
tree = ET.fromstring(res.text)
9931021
users = [x.text for x in tree.findall('data/users/element')]
9941022

995-
return users
1023+
return users
9961024

9971025
raise HTTPResponseError(res)
9981026

@@ -1701,7 +1729,7 @@ def __strip_dav_path(self, path):
17011729
if path.startswith(self.__davpath):
17021730
return path[len(self.__davpath):]
17031731
return path
1704-
1732+
17051733
def __webdav_move_copy(self,remote_path_source,remote_path_target,operation):
17061734
"""Copies or moves a remote file or directory
17071735
@@ -1749,6 +1777,21 @@ def __xml_to_dict(self, element):
17491777
else:
17501778
return_dict[el.tag] = el.text
17511779
return return_dict
1780+
1781+
@staticmethod
1782+
def __parse_expiration_date(date):
1783+
"""Converts the given datetime object into the format required
1784+
by the share API
1785+
:param date: datetime object
1786+
:returns: string encoded to use as expireDate parameter in the share API
1787+
"""
1788+
if date is None:
1789+
return None
1790+
1791+
if isinstance(date, datetime.datetime):
1792+
return date.strftime('YYYY-MM-DD')
1793+
1794+
return date
17521795

17531796
def __get_shareinfo(self, data_el):
17541797
"""Simple helper which returns instance of ShareInfo class

owncloud/test/test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,16 @@ def test_share_with_link(self, file_name):
573573
path = self.test_root + file_name
574574
self.assertTrue(self.client.put_file_contents(path, 'hello world!'))
575575

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

578578
self.assertTrue(self.client.is_shared(path))
579579
self.assertTrue(isinstance(share_info, owncloud.ShareInfo))
580580
self.assertTrue(type(share_info.get_id()) is int)
581581
self.assertEquals(share_info.get_path(), path)
582582
self.assertTrue(type(share_info.get_link()) is str)
583583
self.assertTrue(type(share_info.get_token()) is str)
584+
self.assertEquals(share_info.get_permissions(), 7)
585+
self.assertEquals(share_info.get_expiration(), '2150-02-04 00:00:00')
584586

585587
def test_share_with_link_non_existing_file(self):
586588
"""Test sharing a file with link"""
@@ -781,6 +783,17 @@ def test_update_share_user(self):
781783
self.assertEqual(int(perms), self.client.OCS_PERMISSION_ALL)
782784
self.assertTrue(self.client.delete_share(share_id))
783785

786+
def test_update_share_expiration(self):
787+
"""Test updating a share parameters - expiration date"""
788+
path = self.test_root + 'update_share_expiration'
789+
self.client.mkdir(path)
790+
share_info = self.client.share_file_with_link(path)
791+
share_id = share_info.get_id()
792+
self.assertTrue(self.client.update_share(share_id, expiration='2150-02-04'))
793+
share_info = self.client.get_shares(path)[0]
794+
self.assertEquals(share_info.get_expiration(), '2150-02-04 00:00:00')
795+
self.assertTrue(self.client.delete_share(share_id))
796+
784797
def test_update_share_public(self):
785798
"""Test updating a share parameters - public share"""
786799
path = self.test_root + 'update_share_public.txt'

0 commit comments

Comments
 (0)