Skip to content

Instantly share code, notes, and snippets.

@eckardm
Last active November 15, 2016 15:53
def _set_permissions(self, package):
try:
handle = package.misc_attributes['handle']
except KeyError:
LOGGER.warning('Cannot update permissions - package handle unknown')
return
# Only set if policy exists
if not self.metadata_policy:
LOGGER.info('Restricted metadata policy is empty (%s), not setting', self.metadata_policy)
return
# Set bitstream permissions for bitstreams attached to handle
parsed_url = urlparse.urlparse(self.sd_iri)
dspace_url = urlparse.urlunparse((parsed_url.scheme, parsed_url.netloc, '', '', '', ''))
# Log in to get DSpace REST API token
url = dspace_url + '/rest/login'
body = {'email': self.user, 'password': self.password}
try:
response = requests.post(url, json=body)
except Exception:
LOGGER.warning('Error logging in to DSpace REST API, aborting', exc_info=True)
return
rest_token = response.text
# Fetch bitstream information for item
url = dspace_url + '/rest/handle/' + handle
headers = {
'Accept': 'application/json',
'rest-dspace-token': rest_token,
}
params = {'expand': 'bitstreams'}
try:
response = requests.get(url, headers=headers, params=params)
except Exception:
LOGGER.warning('Error fetching bitstream information for handle %s', handle, exc_info=True)
LOGGER.debug('REST API handle mapping %s %s', response.status_code, response)
LOGGER.debug('Body %s', response.json())
# Add bitstream description for objects when depositing to DSpace
for bitstream in response.json()['bitstreams']:
if bitstream['name'] == 'objects.7z':
url = dspace_url + bitstream['link']
body = bitstream
body['description'] = 'Archival materials.'
LOGGER.debug('Posting objects bitstream description %s', body)
try:
response = requests.put(url, headers=headers, json=body)
except Exception:
LOGGER.warning('Error posting bitstream body', exc_info=True)
continue
LOGGER.debug('Response: %s %s', response.status_code, response.text)
# Update bitstream policies
for bitstream in response.json()['bitstreams']:
if bitstream['name'] != 'metadata.7z':
LOGGER.debug('skipping non-metadata bitstream named %s', bitstream['name'])
continue
url = dspace_url + bitstream['link']
LOGGER.debug('Bitstream policy URL %s', url)
body = bitstream
# Overwrite existing policies, instead of adding
body['policies'] = self.metadata_policy
# Add bitstream description for metadata when depositing to DSpace
body['description'] = 'Administrative information.'
LOGGER.debug('Posting bitstream body %s', body)
try:
response = requests.put(url, headers=headers, json=body)
except Exception:
LOGGER.warning('Error posting bitstream body', exc_info=True)
continue
LOGGER.debug('Response: %s %s', response.status_code, response.text)
# Logout from DSpace API
url = dspace_url + '/rest/logout'
try:
requests.post(url, headers=headers)
except Exception:
LOGGER.info('Error logging out of DSpace REST API', exc_info=True)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment