Skip to content

Commit

Permalink
Merge pull request #1050 from ecds/bugfix/update-metadata-jsonfield
Browse files Browse the repository at this point in the history
Apply updates to metadata json field on set_metadata
  • Loading branch information
blms authored Jun 10, 2024
2 parents 0586db6 + 5a6d901 commit 8c6820c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions apps/ingest/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,27 @@ def set_metadata(manifest, metadata):
else:
# all other keys go into Manifest.metadata JSONField
if isinstance(manifest.metadata, list):
# add label and value to list
manifest.metadata.append({"label": key, "value": value})
found_index = next(
(
idx
for (idx, d) in enumerate(manifest.metadata)
if d["label"] == key
),
None,
)
if found_index:
# if value with this label exists, pop and re-insert
manifest.metadata.pop(found_index)
manifest.metadata.insert(
found_index, {"label": key, "value": value}
)
else:
# if not, add label and value to end of list
manifest.metadata.append({"label": key, "value": value})
elif isinstance(manifest.metadata, dict):
# convert to list of {label, value} as expected by iiif spec
manifest.metadata = [
*[
{"label": k, "value": v}
for (k, v) in manifest.metadata.items()
],
*[{"label": k, "value": v} for (k, v) in manifest.metadata.items()],
{"label": key, "value": value},
]
else:
Expand Down

0 comments on commit 8c6820c

Please sign in to comment.