From 04d9b87b2455cc637cdd9eb9ebeef861ada494ba Mon Sep 17 00:00:00 2001 From: abram axel booth Date: Thu, 3 Oct 2024 14:32:15 -0400 Subject: [PATCH] osf:hasOsfAddon supplementary metadata [ENG-6188] --- osf/metadata/osf_gathering.py | 18 ++++++++++++++++++ .../project_monthly_supplement.turtle | 1 + .../project_supplement.turtle | 10 +++++++++- osf_tests/metadata/test_osf_gathering.py | 19 +++++++++++++++++++ .../metadata/test_serialized_metadata.py | 1 + 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/osf/metadata/osf_gathering.py b/osf/metadata/osf_gathering.py index ac60bbe3b44..94e2e0e7831 100644 --- a/osf/metadata/osf_gathering.py +++ b/osf/metadata/osf_gathering.py @@ -228,15 +228,19 @@ def pls_get_magic_metadata_basket(osf_item) -> gather.Basket: OSFMAP_MONTHLY_SUPPLEMENT = { OSF.Project: { OSF.usage: None, + OSF.hasOsfAddon: None, }, OSF.ProjectComponent: { OSF.usage: None, + OSF.hasOsfAddon: None, }, OSF.Registration: { OSF.usage: None, + OSF.hasOsfAddon: None, }, OSF.RegistrationComponent: { OSF.usage: None, + OSF.hasOsfAddon: None, }, OSF.Preprint: { OSF.usage: None, @@ -1116,3 +1120,17 @@ def gather_last_month_usage(focus): yield (_usage_report_ref, OSF.viewSessionCount, _usage_report.view_session_count) yield (_usage_report_ref, OSF.downloadCount, _usage_report.download_count) yield (_usage_report_ref, OSF.downloadSessionCount, _usage_report.download_session_count) + + +@gather.er(OSF.hasOsfAddon) +def gather_addons(focus): + # note: when gravyvalet exists, use `iterate_addons_for_resource` + # from osf.external.gravy_valet.request_helpers and get urls like + # "https://addons.osf.example/v1/addon-imps/..." instead of a urn + for _addon_settings in focus.dbmodel.get_addons(): + if not _addon_settings.config.added_default: # skip always-on addons + _addon_ref = rdflib.URIRef(f'urn:osf.io/addons/{_addon_settings.short_name}') + yield (OSF.hasOsfAddon, _addon_ref) + yield (_addon_ref, RDF.type, OSF.AddonImplementation) + yield (_addon_ref, DCTERMS.identifier, _addon_settings.short_name) + yield (_addon_ref, SKOS.prefLabel, _addon_settings.config.full_name) diff --git a/osf_tests/metadata/expected_metadata_files/project_monthly_supplement.turtle b/osf_tests/metadata/expected_metadata_files/project_monthly_supplement.turtle index dd9c54b1f93..9e6b67e1831 100644 --- a/osf_tests/metadata/expected_metadata_files/project_monthly_supplement.turtle +++ b/osf_tests/metadata/expected_metadata_files/project_monthly_supplement.turtle @@ -2,6 +2,7 @@ @prefix dcterms: . @prefix foaf: . @prefix osf: . +@prefix skos: . @prefix xsd: . osf:usage [ dcterms:temporal "2123-05"^^xsd:gYearMonth ; diff --git a/osf_tests/metadata/expected_metadata_files/project_supplement.turtle b/osf_tests/metadata/expected_metadata_files/project_supplement.turtle index 662c197699d..2fbe79be692 100644 --- a/osf_tests/metadata/expected_metadata_files/project_supplement.turtle +++ b/osf_tests/metadata/expected_metadata_files/project_supplement.turtle @@ -1 +1,9 @@ -# correctly empty (for now) +@prefix dcterms: . +@prefix osf: . +@prefix skos: . + + osf:hasOsfAddon ; + + a osf:AddonImplementation ; + dcterms:identifier "gitlab" ; + skos:prefLabel "GitLab" . diff --git a/osf_tests/metadata/test_osf_gathering.py b/osf_tests/metadata/test_osf_gathering.py index c2b55a852ee..5a1d58fed07 100644 --- a/osf_tests/metadata/test_osf_gathering.py +++ b/osf_tests/metadata/test_osf_gathering.py @@ -55,6 +55,8 @@ def setUpTestData(cls): ) # project (with components): cls.project = factories.ProjectFactory(creator=cls.user__admin, is_public=True) + cls.project.add_addon('box', auth=None) + cls.project.add_addon('gitlab', auth=None) cls.project.add_contributor(cls.user__readwrite, permissions=permissions.WRITE) cls.project.add_contributor(cls.user__readonly, permissions=permissions.READ, visible=False) cls.component = factories.ProjectFactory(parent=cls.project, creator=cls.user__admin, is_public=True) @@ -786,3 +788,20 @@ def test_gather_last_month_usage(self): (_usage_bnode, OSF.downloadCount, Literal(43)), (_usage_bnode, OSF.downloadSessionCount, Literal(11)), }) + + def test_gather_addons(self): + # registration (without non-default addon) + assert_triples(osf_gathering.gather_addons(self.registrationfocus), set()) + # project (with non-default addons) + _box_ref = rdflib.URIRef('urn:osf.io/addons/box') + _gitlab_ref = rdflib.URIRef('urn:osf.io/addons/gitlab') + assert_triples(osf_gathering.gather_addons(self.projectfocus), { + (self.projectfocus.iri, OSF.hasOsfAddon, _box_ref), + (_box_ref, RDF.type, OSF.AddonImplementation), + (_box_ref, DCTERMS.identifier, Literal('box')), + (_box_ref, SKOS.prefLabel, Literal('Box')), + (self.projectfocus.iri, OSF.hasOsfAddon, _gitlab_ref), + (_gitlab_ref, RDF.type, OSF.AddonImplementation), + (_gitlab_ref, DCTERMS.identifier, Literal('gitlab')), + (_gitlab_ref, SKOS.prefLabel, Literal('GitLab')), + }) diff --git a/osf_tests/metadata/test_serialized_metadata.py b/osf_tests/metadata/test_serialized_metadata.py index e7e34245d12..d66f45e1619 100644 --- a/osf_tests/metadata/test_serialized_metadata.py +++ b/osf_tests/metadata/test_serialized_metadata.py @@ -199,6 +199,7 @@ def setUp(self): category='doi', value=f'10.70102/FK2osf.io/{self.project._id}', ) + self.project.add_addon('gitlab', auth=None) self.file = create_test_file( self.project, self.user,