From d422e181f84ce10b86c718fc41be22a54f7fa182 Mon Sep 17 00:00:00 2001 From: Ronald Krist Date: Wed, 20 Mar 2024 17:30:39 +0100 Subject: [PATCH] customizable args for record index field --- oarepo_model_builder/datatypes/components/model/mapping.py | 7 +++++++ oarepo_model_builder/invenio/edtf_interval_dumper.py | 5 ++++- oarepo_model_builder/invenio/templates/record.py.jinja2 | 4 +++- setup.cfg | 2 +- tests/test_datatype_prepare.py | 1 + tests/test_model_saver.py | 2 ++ tests/test_simple_builders.py | 4 ++-- 7 files changed, 20 insertions(+), 5 deletions(-) diff --git a/oarepo_model_builder/datatypes/components/model/mapping.py b/oarepo_model_builder/datatypes/components/model/mapping.py index f4bd813a..4b14fb16 100644 --- a/oarepo_model_builder/datatypes/components/model/mapping.py +++ b/oarepo_model_builder/datatypes/components/model/mapping.py @@ -31,6 +31,12 @@ class ModelMappingSchema(ma.Schema): metadata={"doc": "Mapping template, merged with generated mapping"}, ) skip = ma.fields.Boolean() + index_field_arguments = ma.fields.List( + ma.fields.String(), + data_key="index-field-arguments", + attribute="index-field-arguments", + metadata={"doc": "Other arguments for index field"}, + ) class Meta: unknown = ma.RAISE @@ -89,3 +95,4 @@ def before_model_prepare(self, datatype, *, context, **kwargs): f"{short_index_name}.json", ), ) + mapping.setdefault("index-field-args", []) diff --git a/oarepo_model_builder/invenio/edtf_interval_dumper.py b/oarepo_model_builder/invenio/edtf_interval_dumper.py index 5a4d71c8..5fa82401 100644 --- a/oarepo_model_builder/invenio/edtf_interval_dumper.py +++ b/oarepo_model_builder/invenio/edtf_interval_dumper.py @@ -17,7 +17,10 @@ def get_paths(self, parent_node) -> Iterator[str]: children = parent_node for c in children: node = children[c] - if node.model_type == "edtf-interval" or node.model_type == "edtf-time-interval": + if ( + node.model_type == "edtf-interval" + or node.model_type == "edtf-time-interval" + ): yield self.to_path(node.path) elif node.children != {}: yield from self.get_paths(node.children) diff --git a/oarepo_model_builder/invenio/templates/record.py.jinja2 b/oarepo_model_builder/invenio/templates/record.py.jinja2 index 1a8433b6..4b705aa1 100644 --- a/oarepo_model_builder/invenio/templates/record.py.jinja2 +++ b/oarepo_model_builder/invenio/templates/record.py.jinja2 @@ -22,7 +22,9 @@ class {{ vars.record|class_header }}: schema = ConstantField("$schema", "{{ settings.schema_server }}{{ vars.json_schema_settings.name }}") {% endif %} {% if not vars.mapping.skip %} - index = IndexField("{{ vars.mapping.index }}") + index = IndexField("{{ vars.mapping.index }}", {% if vars.mapping.index_field_args %} + {{ vars.mapping.index_field_args|generate_list }} + {% endif %}) {% endif %} {% if not vars.pid.skip %} pid = {{ vars.pid.field_class|base_name }}( diff --git a/setup.cfg b/setup.cfg index d57ce90d..31bb8dcb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-model-builder -version = 4.0.81 +version = 4.0.82 description = A utility library that generates OARepo required data model files from a JSON specification file authors = Miroslav Bauer , Miroslav Simek readme = README.md diff --git a/tests/test_datatype_prepare.py b/tests/test_datatype_prepare.py index 15787854..721c0a0c 100644 --- a/tests/test_datatype_prepare.py +++ b/tests/test_datatype_prepare.py @@ -263,6 +263,7 @@ def test_prepare_datatype(): "module": "my.test.records.mappings", "index": "my_test_record-test-1.0.0", "file": "my/test/records/mappings/os-v2/my_test_record/test-1.0.0.json", + "index-field-args": [], }, "marshmallow": { "generate": True, diff --git a/tests/test_model_saver.py b/tests/test_model_saver.py index 9c2602b0..5688e158 100644 --- a/tests/test_model_saver.py +++ b/tests/test_model_saver.py @@ -255,6 +255,7 @@ def test_model_saver(): "module": "test.records.mappings", "index": "test-test-1.0.0", "file": "test/records/mappings/os-v2/test/test-1.0.0.json", + "index-field-args": [], }, "marshmallow": { "generate": True, @@ -616,6 +617,7 @@ def test_model_saver_invenio(): "module": "test.records.mappings", "index": "test-test-1.0.0", "file": "test/records/mappings/os-v2/test/test-1.0.0.json", + "index-field-args": [], }, "permissions": { "generate": True, diff --git a/tests/test_simple_builders.py b/tests/test_simple_builders.py index d72e17de..a8272019 100644 --- a/tests/test_simple_builders.py +++ b/tests/test_simple_builders.py @@ -92,7 +92,7 @@ def test_record_builder(): class TestRecord(InvenioRecord): model_cls = TestMetadata schema = ConstantField("$schema", "local://test-1.0.0.json") - index = IndexField("test-test-1.0.0") + index = IndexField("test-test-1.0.0",) pid = PIDField( provider=TestIdProvider, context_cls=PIDFieldContext, @@ -131,7 +131,7 @@ class TestIdProvider(RecordIdProviderV2): class TestRecord(InvenioRecord): model_cls = TestMetadata schema = ConstantField("$schema", "local://test-1.0.0.json") - index = IndexField("test-test-1.0.0") + index = IndexField("test-test-1.0.0",) pid = PIDField( provider=TestIdProvider, context_cls=PIDFieldContext,