Skip to content

Commit 90291fd

Browse files
committed
add eperson and group example function script
1 parent c11e2e6 commit 90291fd

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

example_eperson_group.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
2+
# This software is licenced under the BSD 3-Clause licence
3+
# available at https://opensource.org/licenses/BSD-3-Clause
4+
# and described in the LICENCE file in the root of this project
5+
6+
"""
7+
Example Python 3 application using the dspace.py API client library to retrieve basic person
8+
and group information in a DSpace repository
9+
"""
10+
11+
import logging
12+
import os
13+
import sys
14+
15+
from dspace_rest_client.client import DSpaceClient
16+
17+
# Import models as below if needed
18+
# from dspace_rest_d.models import Community, Collection, Item, Bundle, Bitstream
19+
20+
# Example variables needed for authentication and basic API requests
21+
# SET THESE TO MATCH YOUR TEST SYSTEM BEFORE RUNNING THE EXAMPLE SCRIPT
22+
# You can also leave them out of the constructor and set environment variables instead:
23+
# DSPACE_API_ENDPOINT=
24+
# DSPACE_API_USERNAME=
25+
# DSPACE_API_PASSWORD=
26+
# USER_AGENT=
27+
DEFAULT_URL = "https://localhost:8080/server/api"
28+
DEFAULT_USERNAME = "[email protected]"
29+
DEFAULT_PASSWORD = "password"
30+
31+
GROUP_UUID = "UUID_OF_GROUP_TO_FETCH"
32+
NEW_GROUP_NAME = "New Test Group"
33+
UPDATED_GROUP_NAME = "Updated Test Group"
34+
PARENT_GROUP_UUID = "UUID_OF_PARENT_GROUP"
35+
CHILD_GROUP_UUID = "UUID_OF_CHILD_GROUP"
36+
EPERSON_UUID = "UUID_OF_EPERSON_TO_FETCH"
37+
QUERY = "Administrator"
38+
SEARCH_EMAIL = "[email protected]"
39+
SEARCH_PERSON_QUERY = "Test"
40+
SEARCH_GROUP_QUERY = "Administrator"
41+
42+
# Configuration from environment variables
43+
URL = os.environ.get("DSPACE_API_ENDPOINT", DEFAULT_URL)
44+
USERNAME = os.environ.get("DSPACE_API_USERNAME", DEFAULT_USERNAME)
45+
PASSWORD = os.environ.get("DSPACE_API_PASSWORD", DEFAULT_PASSWORD)
46+
47+
# Instantiate DSpace client
48+
# Note the 'fake_user_agent' setting here -- this will set a string like the following,
49+
# to get by Cloudfront:
50+
# Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \
51+
# Chrome/39.0.2171.95 Safari/537.36
52+
# The default is to *not* fake the user agent, and instead use the default of
53+
# DSpace-Python-REST-Client/x.y.z.
54+
# To specify a custom user agent, set the USER_AGENT env variable and leave/set
55+
# fake_user_agent as False
56+
d = DSpaceClient(
57+
api_endpoint=URL, username=USERNAME, password=PASSWORD, fake_user_agent=True
58+
)
59+
60+
# Authenticate against the DSpace client
61+
authenticated = d.authenticate()
62+
if not authenticated:
63+
print("Error logging in! Giving up.")
64+
sys.exit(1)
65+
66+
# --- USER FUNCTIONS ---
67+
68+
# Get users with pagination
69+
logging.info("Fetching users...")
70+
users = d.get_users(page=0, size=5)
71+
for user in users:
72+
print(f"User: {user.uuid}, Name: {user.name}, Email: {user.email}")
73+
74+
# Get users using an iterator
75+
logging.info("Fetching users via iterator...")
76+
user_iter = d.get_users_iter()
77+
for user in user_iter:
78+
print(f"Iterated User: {user.uuid}, Name: {user.name}, Email: {user.email}")
79+
80+
# Get a user by UUID
81+
logging.info("Fetching user by UUID: %s", EPERSON_UUID)
82+
user = d.get_user_by_uuid(EPERSON_UUID)
83+
if user:
84+
print(f"Fetched User: {user.uuid}, Name: {user.name}, Email: {user.email}")
85+
86+
# Search for a user by email
87+
logging.info("Searching user by email: %s", SEARCH_EMAIL)
88+
user = d.search_user_by_email(SEARCH_EMAIL)
89+
if user:
90+
print(f"Found User: {user.uuid}, Name: {user.name}, Email: {user.email}")
91+
92+
# Search users by metadata
93+
logging.info("Searching users by metadata: %s", SEARCH_PERSON_QUERY)
94+
users = d.search_users_by_metadata(query=SEARCH_PERSON_QUERY)
95+
for user in users:
96+
print(f"Matched User: {user.uuid}, Name: {user.name}, Email: {user.email}")
97+
98+
# --- GROUP FUNCTIONS ---
99+
100+
# Get groups with pagination
101+
logging.info("Fetching groups...")
102+
groups = d.get_groups(page=0, size=5)
103+
for group in groups:
104+
print(f"Group: {group.uuid}, Name: {group.name}")
105+
106+
# Get a group by UUID
107+
logging.info("Fetching group by UUID: %s", GROUP_UUID)
108+
group = d.get_group_by_uuid(GROUP_UUID)
109+
if group:
110+
print(f"Fetched Group: {group.uuid}, Name: {group.name}")
111+
112+
# Create a new group
113+
logging.info("Creating a new group...")
114+
new_group = d.create_group({"name": NEW_GROUP_NAME})
115+
print(new_group)
116+
if new_group is not None:
117+
print(f"Created Group: {new_group.uuid}, Name: {new_group.name}")
118+
# Update group name
119+
new_group_uuid = new_group.uuid
120+
logging.info("Updating group name for %s...", new_group_uuid)
121+
updated_group = d.update_group_name(new_group_uuid, UPDATED_GROUP_NAME)
122+
if updated_group:
123+
print(f"Updated Group: {updated_group.uuid}, Name: {updated_group.name}")
124+
else:
125+
print("""Error creating group! This may be due to a group with the same name already existing.
126+
There is no update of the group name in this case.""")
127+
128+
# Add a subgroup
129+
logging.info("Adding subgroup %s to %s...", CHILD_GROUP_UUID, PARENT_GROUP_UUID)
130+
if d.add_subgroup(PARENT_GROUP_UUID, CHILD_GROUP_UUID):
131+
print(f"Subgroup {CHILD_GROUP_UUID} added to {PARENT_GROUP_UUID}")
132+
133+
# Fetch subgroups
134+
logging.info("Fetching subgroups of %s...", PARENT_GROUP_UUID)
135+
subgroups = d.get_subgroups(PARENT_GROUP_UUID)
136+
for subgroup in subgroups:
137+
print(f"Subgroup: {subgroup.uuid}, Name: {subgroup.name}")
138+
139+
# Remove a subgroup
140+
logging.info("Removing subgroup %s from %s...", CHILD_GROUP_UUID, PARENT_GROUP_UUID)
141+
if d.remove_subgroup(PARENT_GROUP_UUID, CHILD_GROUP_UUID):
142+
print(f"Subgroup {CHILD_GROUP_UUID} removed from {PARENT_GROUP_UUID}")
143+
144+
# Search groups by metadata
145+
logging.info("Searching groups by metadata: %s", QUERY)
146+
found_groups = d.search_groups_by_metadata(QUERY)
147+
for group in found_groups:
148+
print(f"Matched Group: {group.uuid}, Name: {group.name}")
149+
150+
# Get EPersons in a group
151+
logging.info("Fetching EPersons in group %s...", GROUP_UUID)
152+
epersons = d.get_epersons_in_group(GROUP_UUID)
153+
for eperson in epersons:
154+
print(f"EPerson: {eperson.uuid}, Name: {eperson.name}, Email: {eperson.email}")
155+
156+
# Add an EPerson to a group
157+
logging.info("Adding EPerson %s to group %s...", EPERSON_UUID, GROUP_UUID)
158+
if d.add_eperson_to_group(GROUP_UUID, EPERSON_UUID):
159+
print(f"EPerson {EPERSON_UUID} added to group {GROUP_UUID}")
160+
161+
# Remove an EPerson from a group
162+
logging.info("Removing EPerson %s from group %s...", EPERSON_UUID, GROUP_UUID)
163+
if d.remove_eperson_from_group(GROUP_UUID, EPERSON_UUID):
164+
print(f"EPerson {EPERSON_UUID} removed from group {GROUP_UUID}")
165+
166+
# Create a new person record
167+
user = {
168+
"canLogIn": True,
169+
"email": "[email protected]",
170+
"requireCertificate": False,
171+
"metadata": {
172+
"eperson.firstname": [{"value": "Test"}],
173+
"eperson.lastname": [{"value": "Dummy"}],
174+
},
175+
}
176+
logging.info("Creating a new person record...")
177+
new_person = d.create_user(user)
178+
if new_person:
179+
print(f"Created Person: {new_person.uuid}, Name: {new_person.name}")

0 commit comments

Comments
 (0)