Skip to content

Commit a19071d

Browse files
authored
feat: Support base item operations for WebLink (#639)
Closes: SDK-1862
1 parent 07058be commit a19071d

File tree

12 files changed

+786
-333
lines changed

12 files changed

+786
-333
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Next release
1111
- Add `admin_logs_streaming` support for events stream (`#623 <https://github.com/box/box-python-sdk/pull/623>`_)
1212
- Add `vanity_name` param for creating shared link to a file or folder (`#637 <https://github.com/box/box-python-sdk/pull/637>`_)
1313
- Add getting files and file versions under retention for a retention policy assignment (`#633 <https://github.com/box/box-python-sdk/pull/633>`_)
14+
- Support base item operations for WebLink class (`#639 <https://github.com/box/box-python-sdk/pull/639>`_)
1415

1516
**Bug Fixes:**
1617
- Limit cryptography to version <3.5.0 (`#636 <https://github.com/box/box-python-sdk/pull/636>`_)

boxsdk/object/base_item.py

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import json
2+
3+
from .base_object import BaseObject
4+
from ..exception import BoxValueError
5+
from ..util.api_call_decorator import api_call
6+
from ..util.default_arg_value import SDK_VALUE_NOT_SET
7+
8+
9+
class BaseItem(BaseObject):
10+
11+
@api_call
12+
def copy(self, parent_folder, name=None):
13+
"""Copy the item to the given folder.
14+
15+
:param parent_folder:
16+
The folder to which the item should be copied.
17+
:type parent_folder:
18+
:class:`Folder`
19+
:param name:
20+
A new name for the item, in case there is already another item in the new parent folder with the same name.
21+
:type name:
22+
`unicode` or None
23+
"""
24+
self.validate_item_id(self._object_id)
25+
url = self.get_url('copy')
26+
data = {
27+
'parent': {'id': parent_folder.object_id}
28+
}
29+
if name is not None:
30+
data['name'] = name
31+
box_response = self._session.post(url, data=json.dumps(data))
32+
response = box_response.json()
33+
return self.translator.translate(
34+
session=self._session,
35+
response_object=response,
36+
)
37+
38+
@api_call
39+
def move(self, parent_folder, name=None):
40+
"""
41+
Move the item to the given folder.
42+
43+
:param parent_folder:
44+
The parent `Folder` object, where the item will be moved to.
45+
:type parent_folder:
46+
:class:`Folder`
47+
:param name:
48+
A new name for the item, in case there is already another item in the new parent folder with the same name.
49+
:type name:
50+
`unicode` or None
51+
"""
52+
data = {
53+
'parent': {'id': parent_folder.object_id}
54+
}
55+
if name is not None:
56+
data['name'] = name
57+
return self.update_info(data)
58+
59+
@api_call
60+
def rename(self, name):
61+
"""
62+
Rename the item to a new name.
63+
64+
:param name:
65+
The new name, you want the item to be renamed to.
66+
:type name:
67+
`unicode`
68+
"""
69+
data = {
70+
'name': name,
71+
}
72+
return self.update_info(data)
73+
74+
@api_call
75+
def create_shared_link(self, **kwargs):
76+
"""
77+
Create a shared link for the item with the given access permissions.
78+
79+
:param kwargs:
80+
Keyword arguments passed from overriding method used to build request properties.
81+
:return:
82+
The updated object with shared link.
83+
Returns a new object of the same type, without modifying the original object passed as self.
84+
:rtype:
85+
:class:`BaseItem`
86+
"""
87+
shared_link = {}
88+
89+
if kwargs.get('access') is not None:
90+
shared_link['access'] = kwargs.get('access')
91+
92+
if kwargs.get('unshared_at') is not SDK_VALUE_NOT_SET:
93+
shared_link['unshared_at'] = kwargs.get('unshared_at')
94+
95+
if kwargs.get('allow_download') is not None or kwargs.get('allow_preview') is not None:
96+
shared_link['permissions'] = {}
97+
if kwargs.get('allow_download') is not None:
98+
shared_link['permissions']['can_download'] = kwargs.get('allow_download')
99+
if kwargs.get('allow_preview') is not None:
100+
shared_link['permissions']['can_preview'] = kwargs.get('allow_preview')
101+
102+
if kwargs.get('password') is not None:
103+
shared_link['password'] = kwargs.get('password')
104+
105+
if kwargs.get('vanity_name') is not None:
106+
shared_link['vanity_name'] = kwargs.get('vanity_name')
107+
108+
data = {'shared_link': shared_link}
109+
update_info_kwargs = {'etag': kwargs.get('etag')} if kwargs.get('etag') is not None else {}
110+
111+
return self.update_info(data, **update_info_kwargs)
112+
113+
@api_call
114+
def get_shared_link(self, **kwargs):
115+
"""
116+
Get a shared link for the item with the given access permissions.
117+
This url leads to a Box.com shared link page, where the item can be previewed, downloaded, etc.
118+
119+
:param kwargs:
120+
Keyword arguments passed from overriding method used to create a new shared link.
121+
:returns:
122+
The URL of the shared link.
123+
:rtype:
124+
`unicode`
125+
"""
126+
item = self.create_shared_link(**kwargs)
127+
return item.shared_link['url'] # pylint:disable=no-member
128+
129+
@api_call
130+
def remove_shared_link(self, **kwargs):
131+
"""
132+
Delete the shared link for the item.
133+
134+
:param kwargs:
135+
Keyword arguments passed from overriding method used to build request properties.
136+
:returns:
137+
Whether or not the update was successful.
138+
:rtype:
139+
`bool`
140+
"""
141+
data = {'shared_link': None}
142+
update_info_kwargs = {'etag': kwargs.get('etag')} if kwargs.get('etag') is not None else {}
143+
144+
item = self.update_info(data, **update_info_kwargs)
145+
return item.shared_link is None # pylint:disable=no-member
146+
147+
@api_call
148+
def add_to_collection(self, collection):
149+
"""
150+
Add the item to a collection. This method is not currently safe from race conditions.
151+
152+
:param collection:
153+
The collection to add the item to.
154+
:type collection:
155+
:class:`Collection`
156+
:return:
157+
This item.
158+
:rtype:
159+
:class:`Item`
160+
"""
161+
collections = self.get(fields=['collections']).collections # pylint:disable=no-member
162+
collections.append({'id': collection.object_id})
163+
data = {
164+
'collections': collections
165+
}
166+
return self.update_info(data)
167+
168+
@api_call
169+
def remove_from_collection(self, collection):
170+
"""
171+
Remove the item from a collection. This method is not currently safe from race conditions.
172+
173+
:param collection:
174+
The collection to remove the item from.
175+
:type collection:
176+
:class:`Collection`
177+
:return:
178+
This item.
179+
:rtype:
180+
:class:`Item`
181+
"""
182+
collections = self.get(fields=['collections']).collections # pylint:disable=no-member
183+
updated_collections = [c for c in collections if c['id'] != collection.object_id]
184+
data = {
185+
'collections': updated_collections
186+
}
187+
return self.update_info(data)
188+
189+
@staticmethod
190+
def validate_item_id(item_id):
191+
"""
192+
Validates an item ID is numeric
193+
194+
:param item_id:
195+
:type item_id:
196+
`str` or `int`
197+
:raises:
198+
BoxException: if item_id is not numeric
199+
:returns:
200+
:rtype:
201+
None
202+
"""
203+
if not isinstance(item_id, int) and not item_id.isdigit():
204+
raise BoxValueError("Invalid item ID")

boxsdk/object/folder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def get_items(self, limit=None, offset=0, marker=None, use_marker=False, sort=No
201201
`unicode` or None
202202
:param sort:
203203
Item field to sort results on: 'id', 'name', or 'date'.
204-
:type sort':
204+
:type sort:
205205
`unicode` or None
206206
:param direction:
207207
Sort direction for the items returned.

0 commit comments

Comments
 (0)