Skip to content

Commit b8daf93

Browse files
committed
Add extra metadata to media.
1 parent faa4209 commit b8daf93

File tree

6 files changed

+442
-145
lines changed

6 files changed

+442
-145
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,4 @@ checklink/cookies.txt
142142
.gitconfig
143143

144144
*.sqlite
145+
omeka_s_tools.code-workspace

api.ipynb

+206-79
Large diffs are not rendered by default.

docs/api.html

+205-49
Large diffs are not rendered by default.

omeka_s_tools/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.1"
1+
__version__ = "0.2.0"

omeka_s_tools/api.py

+28-15
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ def add_item(self, payload, media_files=None, template_id=None, class_id=None, i
303303
'''
304304
if template_id:
305305
payload['o:resource_template'] = self.format_resource_id(template_id, 'resource_templates')
306+
# If class is not set explicitly, use class associated with template
307+
if not class_id:
308+
template = self.get_resource_by_id(template_id, 'resource_templates')
309+
class_id = template['o:resource_class']['o:id']
306310
if class_id:
307311
payload['o:resource_class'] = self.format_resource_id(class_id, 'resource_classes')
308312
if item_set_id:
@@ -447,38 +451,47 @@ def update_resource(self, payload, resource_type='items'):
447451
response = self.s.put(f'{self.api_url}/{resource_type}/{payload["o:id"]}', json=payload, params=self.params)
448452
return response.json()
449453

450-
def add_media_to_item(self, item_id, media_file):
454+
def add_media_to_item(self, item_id, media_file, payload={}, template_id=None, class_id=None):
451455
'''
452456
Upload a media file and associate it with an existing item.
453457
454458
Parameters:
455459
* `item_id` - the Omeka id of the item this media file should be added to
456-
* `media_file` - the media file to be uploaded, c
457-
458-
The value of `media_file` can be either:
459-
* a path to an image/media file (filename is used as title)
460-
* a dict containing `title` and `path` values
461-
462-
The path values can either be strings or pathlib Paths.
460+
* `media_path` - a path to an image/media file (string or pathlib Path)
461+
* `payload` (optional) - metadata to attach to media object, either
462+
a dict generated by `prepare_item_payload()` or `prepare_item_payload_using_template()`,
463+
or a string which is used as the value for `dcterms:title`.
464+
* `template_id` - internal Omeka identifier of a resource template you want to attach to this item
465+
* `class_id` - internal Omeka identifier of a resource class you want to attach to this item
463466
464467
Returns:
465468
* a dict providing a JSON-LD representation of the new media object
466469
'''
467470
files = {}
471+
# For backwards compatibility
468472
if isinstance(media_file, dict):
469-
title = media_file['title']
470-
path = Path(media_file['path'])
471-
else:
472-
path = Path(media_file)
473-
title = path.name
473+
path = media_file['path']
474+
payload = media_file['title']
475+
# Make sure path is a Path object
476+
path = Path(media_file)
477+
if isinstance(payload, str):
478+
payload = self.prepare_item_payload({'dcterms:title': [payload]})
479+
if template_id:
480+
payload['o:resource_template'] = self.format_resource_id(template_id, 'resource_templates')
481+
if not class_id:
482+
template = self.get_resource_by_id(template_id, 'resource_templates')
483+
class_id = template['o:resource_class']['o:id']
484+
if class_id:
485+
payload['o:resource_class'] = self.format_resource_id(class_id, 'resource_classes')
474486
file_data = {
475487
'o:ingester': 'upload',
476488
'file_index': '0',
489+
'o:source': path.name,
477490
'o:item': {'o:id': item_id},
478-
'dcterms:title': [{'property_id': 1, '@value': title, 'type': 'literal'}]
479491
}
492+
payload.update(file_data)
480493
files[f'file[0]'] = path.read_bytes()
481-
files['data'] = (None, json.dumps(file_data), 'application/json')
494+
files['data'] = (None, json.dumps(payload), 'application/json')
482495
response = self.s.post(f'{self.api_url}/media', files=files, params=self.params)
483496
return response.json()
484497

settings.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ author = Tim Sherratt
1313
author_email = [email protected]
1414
copyright = Tim Sherratt
1515
branch = master
16-
version = 0.1.1
16+
version = 0.2.0
1717
min_python = 3.8
1818
audience = Developers
1919
language = English

0 commit comments

Comments
 (0)