Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new rdm changes #231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
39 changes: 39 additions & 0 deletions oarepo_runtime/records/mappings/rdm_parent_mapping.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
}
22 changes: 0 additions & 22 deletions oarepo_runtime/services/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 23 additions & 3 deletions oarepo_runtime/services/custom_fields/mappings.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions oarepo_runtime/services/permissions/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand All @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions oarepo_runtime/services/schema/rdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
deletion_status = ma_fields.Nested(DeletionStatusSchema, dump_only=True)
Loading