Skip to content

Commit 72c29da

Browse files
committed
add example patch item script
1 parent a4e2c79 commit 72c29da

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

example_patch.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# This software is licenced under the BSD 3-Clause licence
2+
# available at https://opensource.org/licenses/BSD-3-Clause
3+
# and described in the LICENCE file in the root of this project
4+
5+
"""
6+
Example Python 3 application using the dspace.py API client library to patch
7+
some resources in a DSpace 7 repository.
8+
"""
9+
from pprint import pprint
10+
11+
import os
12+
import sys
13+
14+
from dspace_rest_client.client import DSpaceClient
15+
from dspace_rest_client.models import Community, Collection, Item, Bundle, Bitstream
16+
17+
DEFAULT_URL = "https://localhost:8080/server/api"
18+
DEFAULT_USERNAME = "[email protected]"
19+
DEFAULT_PASSWORD = "password"
20+
21+
# UUIDs for the object we want to patch
22+
RESOURCE_ID = "0128787c-6f79-4661-aea4-11635d6fb04f"
23+
24+
# Field and value to patch
25+
FIELD = "dc.title"
26+
VALUE = "New title"
27+
28+
# Configuration from environment variables
29+
URL = os.environ.get("DSPACE_API_ENDPOINT", DEFAULT_URL)
30+
USERNAME = os.environ.get("DSPACE_API_USERNAME", DEFAULT_USERNAME)
31+
PASSWORD = os.environ.get("DSPACE_API_PASSWORD", DEFAULT_PASSWORD)
32+
33+
# Instantiate DSpace client
34+
d = DSpaceClient(
35+
api_endpoint=URL, username=USERNAME, password=PASSWORD, fake_user_agent=True
36+
)
37+
38+
# Authenticate against the DSpace client
39+
authenticated = d.authenticate()
40+
if not authenticated:
41+
print("Error logging in! Giving up.")
42+
sys.exit(1)
43+
44+
# An example of searching for workflow items (any search configuration from discovery.xml can be used)
45+
# note that the results here depend on the workflow role / access of the logged in user
46+
search_results = d.search_objects(
47+
query=f"search.resourceid:{RESOURCE_ID}", dso_type="item"
48+
)
49+
for result in search_results:
50+
print(f"{result.name} ({result.uuid})")
51+
print(
52+
f"{FIELD}: {result.metadata.get(FIELD, [{'value': 'Not available'}])[0]['value']}"
53+
)
54+
55+
item = d.get_item(uuid=result.uuid)
56+
print(type(item))
57+
58+
if FIELD in result.metadata:
59+
if result.metadata[FIELD][0]["value"] == VALUE:
60+
print("Metadata is already correct, skipping")
61+
continue
62+
elif result.metadata[FIELD][0]["value"] != VALUE:
63+
patch_op = d.patch_item(
64+
item=item,
65+
operation="replace",
66+
field=FIELD,
67+
value=VALUE,
68+
)
69+
if patch_op:
70+
print(patch_op)
71+
print("Metadata updated")
72+
else:
73+
print("Error updating metadata")
74+
else:
75+
patch_op = d.patch_item(
76+
item=item,
77+
operation="add",
78+
field=FIELD,
79+
value=VALUE,
80+
)
81+
if patch_op:
82+
print(patch_op)
83+
print("Metadata added")
84+
else:
85+
print("Error adding metadata")

0 commit comments

Comments
 (0)