Skip to content

Commit

Permalink
support sending supplementary metadata to trove
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Oct 1, 2024
1 parent cfd914b commit 5bf0022
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
27 changes: 18 additions & 9 deletions api/share/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ def _enqueue_update_share(osfresource):


@celery_app.task(bind=True, max_retries=4, acks_late=True)
def task__update_share(self, guid: str, is_backfill=False):
def task__update_share(self, guid: str, is_backfill=False, is_supplementary=False):
"""
This function updates share takes Preprints, Projects and Registrations.
:param self:
:param guid:
:return:
"""
resp = _do_update_share(guid, is_backfill=is_backfill)
resp = _do_update_share(guid, is_backfill=is_backfill, is_supplementary=is_supplementary)
try:
resp.raise_for_status()
except Exception as e:
Expand All @@ -97,18 +97,22 @@ def task__update_share(self, guid: str, is_backfill=False):
return resp


def pls_send_trove_indexcard(osf_item, *, is_backfill=False):
def pls_send_trove_indexcard(osf_item, *, is_backfill=False, is_supplementary=False):
try:
_iri = osf_item.get_semantic_iri()
except (AttributeError, ValueError):
raise ValueError(f'could not get iri for {osf_item}')
_metadata_record = pls_gather_metadata_file(osf_item, 'turtle')
_metadata_record = pls_gather_metadata_file(osf_item, 'turtle', {
'is_supplementary': is_supplementary,
})
_queryparams = {
'focus_iri': _iri,
'record_identifier': _shtrove_record_identifier(osf_item),
'record_identifier': _shtrove_record_identifier(osf_item, is_supplementary=is_supplementary),
}
if is_backfill:
_queryparams['nonurgent'] = True
if is_supplementary:
_queryparams['is_supplementary'] = True
return requests.post(
shtrove_ingest_url(),
params=_queryparams,
Expand All @@ -130,7 +134,7 @@ def pls_delete_trove_indexcard(osf_item):
)


def _do_update_share(osfguid: str, *, is_backfill=False):
def _do_update_share(osfguid: str, *, is_backfill=False, is_supplementary=False):
logger.debug('%s._do_update_share("%s", is_backfill=%s)', __name__, osfguid, is_backfill)
_guid_instance = apps.get_model('osf.Guid').load(osfguid)
if _guid_instance is None:
Expand All @@ -139,13 +143,18 @@ def _do_update_share(osfguid: str, *, is_backfill=False):
_response = (
pls_delete_trove_indexcard(_resource)
if _should_delete_indexcard(_resource)
else pls_send_trove_indexcard(_resource, is_backfill=is_backfill)
else pls_send_trove_indexcard(
_resource,
is_backfill=is_backfill,
is_supplementary=is_supplementary,
)
)
return _response


def _shtrove_record_identifier(osf_item):
return osf_item.guids.values_list('_id', flat=True).first()
def _shtrove_record_identifier(osf_item, *, is_supplementary=False):
_id = osf_item.guids.values_list('_id', flat=True).first()
return (f'{_id}/supplement' if is_supplementary else _id)


def _shtrove_auth_headers(osf_item):
Expand Down
22 changes: 22 additions & 0 deletions osf/metadata/osf_gathering.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def osfmap_for_type(rdftype_iri: str):
raise ValueError(f'invalid OSFMAP type! expected one of {set(OSFMAP.keys())}, got {rdftype_iri}')


def osfmap_supplement_for_type(rdftype_iri: str):
try:
return OSFMAP_SUPPLEMENT[rdftype_iri]
except KeyError:
return {}


##### END "public" api #####


Expand Down Expand Up @@ -211,6 +218,21 @@ def osfmap_for_type(rdftype_iri: str):
},
}

OSFMAP_SUPPLEMENT = {
OSF.Project: {
},
OSF.ProjectComponent: {
},
OSF.Registration: {
},
OSF.RegistrationComponent: {
},
OSF.Preprint: {
},
OSF.File: {
},
}

OSF_ARTIFACT_PREDICATES = {
ArtifactTypes.ANALYTIC_CODE: OSF.hasAnalyticCodeResource,
ArtifactTypes.DATA: OSF.hasDataResource,
Expand Down
7 changes: 5 additions & 2 deletions osf/metadata/serializers/turtle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from osf.metadata.osf_gathering import osfmap_for_type
from osf.metadata.osf_gathering import osfmap_for_type, osfmap_supplement_for_type
from osf.metadata.serializers import _base


Expand All @@ -9,5 +9,8 @@ def filename_for_itemid(self, itemid: str):
return f'{itemid}-metadata.ttl'

def serialize(self) -> str:
self.basket.pls_gather(osfmap_for_type(self.basket.focus.rdftype))
if self.serializer_config.get('is_supplementary', False):
self.basket.pls_gather(osfmap_supplement_for_type(self.basket.focus.rdftype))
else:
self.basket.pls_gather(osfmap_for_type(self.basket.focus.rdftype))
return self.basket.gathered_metadata.serialize(format='turtle')

0 comments on commit 5bf0022

Please sign in to comment.