Skip to content

Commit

Permalink
services: added record extension registry
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo committed Jul 25, 2024
1 parent 715f6a7 commit 31f88d5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
21 changes: 21 additions & 0 deletions invenio_rdm_records/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# it under the terms of the MIT License; see LICENSE file for more details.

"""DataCite-based data model for Invenio."""
from importlib_metadata import entry_points
import pkg_resources
from flask import Blueprint
from flask_iiif import IIIF
from flask_principal import identity_loaded
Expand Down Expand Up @@ -95,6 +97,7 @@ def __init__(self, app=None):
def init_app(self, app):
"""Flask application initialization."""
self.init_config(app)
self.init_record_service_registry(app)
self.init_services(app)
self.init_resource(app)
app.extensions["invenio-rdm-records"] = self
Expand Down Expand Up @@ -279,6 +282,24 @@ def fix_datacite_configs(self, app):
if config_item in app.config:
app.config[config_item] = str(app.config[config_item])

def init_record_service_registry(self, app):
# TODO load entry points
self.record_service_registry = {}
self._register_entry_point(
self.record_service_registry,
"invenio_rdm_records.services.record_service_registry",
)
# Discussed w/Alex
# Interface could be the function itself that modifies the record class and returns it

def _register_entry_point(self, registry, ep_name):
"""Load entry points into the given registry."""
for ep in set(entry_points(group=ep_name)):
ext_name = ep.name
callback = ep.load()
assert callable(callback)
registry.setdefault(ext_name, callback)


def finalize_app(app):
"""Finalize app.
Expand Down
17 changes: 15 additions & 2 deletions invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
StatusParam,
)
from .sort import VerifiedRecordsSortParam
from werkzeug.utils import cached_property


def is_draft_and_has_review(record, ctx):
Expand Down Expand Up @@ -277,8 +278,20 @@ class RDMFileRecordServiceConfig(FileServiceConfig, ConfiguratorMixin):
class RDMRecordServiceConfig(RecordServiceConfig, ConfiguratorMixin):
"""RDM record draft service config."""

# Record and draft classes
record_cls = FromConfig("RDM_RECORD_CLS", default=RDMRecord)
@cached_property
def record_cls(self):
"""Record class."""
cfg_cls = self._app.config.get("RDM_RECORD_CLS", RDMRecord)
exts = getattr(
self._app.extensions["invenio-rdm-records"], "record_service_registry", {}
)
new_cls = cfg_cls
for name, _ext in exts.items():
tmp = _ext(self._app, new_cls)
assert type(tmp) == type(cfg_cls), f"Invalid record type. Expected: {type(cfg_cls)}, got: {type(tmp)}"
new_cls = tmp
return new_cls

draft_cls = FromConfig("RDM_DRAFT_CLS", default=RDMDraft)

# Schemas
Expand Down

0 comments on commit 31f88d5

Please sign in to comment.