Skip to content

Commit 0c9e1ce

Browse files
committed
add function to update the owning collection
1 parent 54bc5ea commit 0c9e1ce

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

dspace_rest_client/client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,56 @@ def get_item_owning_collection(self, item):
14991499
)
15001500
return None
15011501

1502+
def update_item_owning_collection(
1503+
self, item, collection_uuid, inherit_policies=False
1504+
):
1505+
"""
1506+
Update the owning collection of a given item.
1507+
@param item: Item object
1508+
@param collection_uuid: UUID of the target collection
1509+
@param inherit_policies: Boolean flag to inherit policies from the target collection
1510+
@return: True if successful, False otherwise
1511+
"""
1512+
if not isinstance(item, Item):
1513+
logging.error("Need a valid item")
1514+
return False
1515+
1516+
if collection_uuid is None:
1517+
logging.error("Collection UUID is missing.")
1518+
return False
1519+
1520+
url = f"{self.API_ENDPOINT}/core/items/{item.uuid}/owningCollection"
1521+
data = f"{self.API_ENDPOINT}/core/collections/{collection_uuid}"
1522+
params = {"inheritPolicies": str(inherit_policies).lower()}
1523+
1524+
headers = self.request_headers.copy()
1525+
headers["Content-Type"] = "text/uri-list"
1526+
1527+
try:
1528+
response = self.session.put(url, data=data, params=params, headers=headers)
1529+
self.update_token(response)
1530+
1531+
if response.status_code == 204:
1532+
logging.info(
1533+
f"Item {item.uuid} successfully moved to Collection {collection_uuid}."
1534+
)
1535+
return True
1536+
elif response.status_code == 403 and "CSRF token" in response.text:
1537+
logging.warning(
1538+
"CSRF token issue encountered, retrying with updated token."
1539+
)
1540+
return self.update_item_owning_collection(
1541+
item, collection_uuid, inherit_policies
1542+
)
1543+
else:
1544+
logging.error(
1545+
f"Failed to update owning collection: {response.status_code}: {response.text} ({url})"
1546+
)
1547+
return False
1548+
except Exception as e:
1549+
logging.error(f"Error updating owning collection for item {item.uuid}: {e}")
1550+
return False
1551+
15021552
def create_user(self, user, token=None, embeds=None):
15031553
"""
15041554
Create a user

0 commit comments

Comments
 (0)