diff --git a/oarepo_runtime/records/mappings/__init__.py b/oarepo_runtime/records/mappings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oarepo_runtime/records/mappings/rdm_parent_mapping.json b/oarepo_runtime/records/mappings/rdm_parent_mapping.json new file mode 100644 index 0000000..0656466 --- /dev/null +++ b/oarepo_runtime/records/mappings/rdm_parent_mapping.json @@ -0,0 +1,39 @@ +{ + "is_verified": { + "type": "boolean" + }, + "access": { + "properties": { + "owned_by": { + "properties": { + "user": { + "type": "keyword" + } + } + }, + "grants": { + "properties": { + "subject": { + "type": "keyword" + }, + "id": { + "type": "keyword" + }, + "level": { + "type": "keyword" + } + } + }, + "grant_tokens": { + "type": "keyword" + }, + "links": { + "properties": { + "id": { + "type": "keyword" + } + } + } + } + } +} \ No newline at end of file diff --git a/oarepo_runtime/services/components.py b/oarepo_runtime/services/components.py index 2442447..db4f4bd 100644 --- a/oarepo_runtime/services/components.py +++ b/oarepo_runtime/services/components.py @@ -21,28 +21,6 @@ class OwnersComponent(ServiceComponent): - def create(self, identity, *, record, **kwargs): - """Create handler.""" - self.add_owner(identity, record) - - def add_owner(self, identity, record, commit=False): - if not hasattr(identity, "id") or not isinstance(identity.id, int): - return - - owners = getattr(record.parent, "owners", None) - if owners is not None: - user = User.query.filter_by(id=identity.id).first() - record.parent.owners.add(user) - if commit: - self.uow.register(ParentRecordCommitOp(record.parent)) - - def update(self, identity, *, record, **kwargs): - """Update handler.""" - self.add_owner(identity, record, commit=True) - - def update_draft(self, identity, *, record, **kwargs): - """Update handler.""" - self.add_owner(identity, record, commit=True) def search_drafts(self, identity, search, params, **kwargs): new_term = RecordOwners().query_filter(identity) diff --git a/oarepo_runtime/services/custom_fields/mappings.py b/oarepo_runtime/services/custom_fields/mappings.py index 4dbdd00..8d82cc5 100644 --- a/oarepo_runtime/services/custom_fields/mappings.py +++ b/oarepo_runtime/services/custom_fields/mappings.py @@ -1,8 +1,13 @@ +import json +import os import inspect + from typing import Iterable import click import deepmerge + +from deepmerge import always_merger from invenio_records_resources.proxies import current_service_registry from invenio_records_resources.services.custom_fields.mappings import ( Mapping as InvenioMapping, @@ -14,7 +19,7 @@ from invenio_search.utils import build_alias_name from oarepo_runtime.records.systemfields.mapping import MappingSystemFieldMixin - +from pathlib import Path class Mapping(InvenioMapping): @classmethod @@ -116,6 +121,16 @@ def prepare_cf_index(record_class, config, path=[]): def prepare_parent_mapping(parent_class, config): if not parent_class: return + + script_dir = str(Path(__file__).resolve().parent) + path_parts = script_dir.split('/') + path_parts = path_parts[:-2] + base_path = '/'.join(path_parts) + mapping_path = f"{base_path}/records/mappings/rdm_parent_mapping.json" + + with open(mapping_path, 'r') as f: + rdm_parent = json.load(f) + parent_mapping = { "parent": { "type": "object", @@ -143,6 +158,11 @@ def prepare_parent_mapping(parent_class, config): } } + parent_mapping_merged = always_merger.merge(parent_mapping, { + "parent": { + "properties": rdm_parent + } + }) # upload mapping try: record_index = dsl.Index( @@ -151,7 +171,7 @@ def prepare_parent_mapping(parent_class, config): ), using=current_search_client, ) - update_index(record_index, {}, parent_mapping) + update_index(record_index, {}, parent_mapping_merged) if hasattr(config, "draft_cls"): draft_index = dsl.Index( @@ -160,7 +180,7 @@ def prepare_parent_mapping(parent_class, config): ), using=current_search_client, ) - update_index(draft_index, {}, parent_mapping) + update_index(draft_index, {}, parent_mapping_merged) except search.RequestError as e: click.secho("An error occurred while creating parent mapping.", fg="red") diff --git a/oarepo_runtime/services/permissions/generators.py b/oarepo_runtime/services/permissions/generators.py index 5254552..5bc245e 100644 --- a/oarepo_runtime/services/permissions/generators.py +++ b/oarepo_runtime/services/permissions/generators.py @@ -12,7 +12,7 @@ def needs(self, record=None, **kwargs): # 'record' is required, so if not passed we default to empty array, # i.e. superuser-access. return [] - owners = getattr(record.parent, "owners", None) + owners = getattr(record.parent.access, "owned_by", None) if owners is not None: return [UserNeed(owner.id) for owner in owners] return [] @@ -21,7 +21,7 @@ def query_filter(self, identity=None, **kwargs): """Filters for current identity as owner.""" users = [n.value for n in identity.provides if n.method == "id"] if users: - return dsl.Q("terms", **{"parent.owners.user": users}) + return dsl.Q("terms", **{"parent.access.owned_by.user": users}) class UserWithRole(Generator): diff --git a/oarepo_runtime/services/schema/rdm.py b/oarepo_runtime/services/schema/rdm.py index ba082f6..a9d756d 100644 --- a/oarepo_runtime/services/schema/rdm.py +++ b/oarepo_runtime/services/schema/rdm.py @@ -10,5 +10,4 @@ class RDMRecordMixin(ma.Schema): versions = NestedAttribute(VersionsSchema, dump_only=True) - deletion_status = ma_fields.Nested(DeletionStatusSchema, dump_only=True) - access = NestedAttribute(AccessSchema) \ No newline at end of file + deletion_status = ma_fields.Nested(DeletionStatusSchema, dump_only=True) \ No newline at end of file