diff --git a/oarepo_model_builder/datatypes/components/facets/field.py b/oarepo_model_builder/datatypes/components/facets/field.py index 1998d1bd..78d491b6 100644 --- a/oarepo_model_builder/datatypes/components/facets/field.py +++ b/oarepo_model_builder/datatypes/components/facets/field.py @@ -23,9 +23,15 @@ class Meta: imports = fields.List(fields.Nested(ImportSchema), required=False) path = fields.String(required=False) keyword = fields.String(required=False) - facet_groups = fields.List(fields.String(), required=False, data_key="facet-groups", attribute="facet-groups") + facet_groups = fields.List( + fields.String(), + required=False, + data_key="facet-groups", + attribute="facet-groups", + ) facet = ma.fields.Bool(required=False) + class RegularFacetsComponent(DataTypeComponent): eligible_datatypes = [] @@ -54,7 +60,7 @@ def process_facets(self, datatype, section, **__kwargs): searchable=facet_section.get("searchable"), imports=facet_section.get("imports", []), facet=facet_section.get("facet", None), - facet_groups= facet_section.get("facet-groups", ["default"]) + facet_groups=facet_section.get("facet-groups", ["default"]), ) # set the field on the definition diff --git a/oarepo_model_builder/datatypes/components/model/facets.py b/oarepo_model_builder/datatypes/components/model/facets.py index b061e6e4..c5dda28c 100644 --- a/oarepo_model_builder/datatypes/components/model/facets.py +++ b/oarepo_model_builder/datatypes/components/model/facets.py @@ -53,7 +53,10 @@ def build_facet_definition( ): if facet_definition.searchable is None: facet_definition.searchable = datatype.definition.get("searchable", True) - if facet_definition.searchable is not False and facet_definition.facet is not False: + if ( + facet_definition.searchable is not False + and facet_definition.facet is not False + ): return [facet_definition] else: # facet will not be generated diff --git a/oarepo_model_builder/datatypes/components/model/pid.py b/oarepo_model_builder/datatypes/components/model/pid.py index 595f7a82..6e729d78 100644 --- a/oarepo_model_builder/datatypes/components/model/pid.py +++ b/oarepo_model_builder/datatypes/components/model/pid.py @@ -56,6 +56,11 @@ class Meta: metadata={"doc": "Module where the pid provider will be placed"} ) skip = ma.fields.Boolean() + extra_code = ma.fields.String( + attribute="extra-code", + data_key="extra-code", + metadata={"doc": "Extra code to be copied below the permission class"}, + ) class PIDModelComponent(DataTypeComponent): @@ -98,6 +103,7 @@ def before_model_prepare(self, datatype, *, context, **kwargs): {"import": "invenio_pidstore.providers.recordid_v2.RecordIdProviderV2"}, ], ) + pid.setdefault("extra-code", "") def process_pid_type(pid_base): diff --git a/oarepo_model_builder/invenio/invenio_record_search_options.py b/oarepo_model_builder/invenio/invenio_record_search_options.py index 61a0b836..f6655232 100644 --- a/oarepo_model_builder/invenio/invenio_record_search_options.py +++ b/oarepo_model_builder/invenio/invenio_record_search_options.py @@ -17,11 +17,11 @@ def finish(self, **extra_kwargs): search_data = [] for f in facets: for group in f.facet_groups: - if group != 'default': + if group != "default": if group not in facet_groups.keys(): facet_groups[group] = {} facet_groups[group][f.path] = "facets." + f.path - if group == 'default': + if group == "default": default_group.append({f.path: "facets." + f.path}) search_data.append({f.path: "facets." + f.path}) if "sortable" in self.current_model.definition: diff --git a/oarepo_model_builder/invenio/templates/invenio_record_facets.py.jinja2 b/oarepo_model_builder/invenio/templates/invenio_record_facets.py.jinja2 index 448f35ef..30f37b70 100644 --- a/oarepo_model_builder/invenio/templates/invenio_record_facets.py.jinja2 +++ b/oarepo_model_builder/invenio/templates/invenio_record_facets.py.jinja2 @@ -10,4 +10,3 @@ from flask_babelex import lazy_gettext as _ {{ facet.path }} = {{ facet.field }} {% endfor %} -{#{{ vars.invenio_record_facets_extra_code }}#} \ No newline at end of file diff --git a/oarepo_model_builder/invenio/templates/invenio_record_search_options.py.jinja2 b/oarepo_model_builder/invenio/templates/invenio_record_search_options.py.jinja2 index f3fa2021..6333539c 100644 --- a/oarepo_model_builder/invenio/templates/invenio_record_search_options.py.jinja2 +++ b/oarepo_model_builder/invenio/templates/invenio_record_search_options.py.jinja2 @@ -47,3 +47,4 @@ class {{ vars.search_options| class_header }}: {% endfor %} } +{{ vars.search_options.extra_code }} \ No newline at end of file diff --git a/oarepo_model_builder/invenio/templates/pid_provider.py.jinja2 b/oarepo_model_builder/invenio/templates/pid_provider.py.jinja2 index b048aba9..4c6532f5 100644 --- a/oarepo_model_builder/invenio/templates/pid_provider.py.jinja2 +++ b/oarepo_model_builder/invenio/templates/pid_provider.py.jinja2 @@ -1,4 +1,6 @@ {{ vars.pid.imports|generate_import }} class {{ vars.pid|class_header(class_name="provider_class", base_classes_name="provider-base-classes") }}: - pid_type = "{{ vars.pid.type }}" \ No newline at end of file + pid_type = "{{ vars.pid.type }}" + +{{ vars.pid.extra_code }} \ No newline at end of file diff --git a/oarepo_model_builder/invenio/templates/ui_serializer.py.jinja2 b/oarepo_model_builder/invenio/templates/ui_serializer.py.jinja2 index c257e1e5..0cdd796b 100644 --- a/oarepo_model_builder/invenio/templates/ui_serializer.py.jinja2 +++ b/oarepo_model_builder/invenio/templates/ui_serializer.py.jinja2 @@ -11,4 +11,6 @@ class {{ vars.json_serializer|class_header }}: object_schema_cls={{vars.ui.marshmallow.class|base_name}}, list_schema_cls=BaseListSchema, schema_context={"object_key": "ui"}, - ) \ No newline at end of file + ) + +{{ vars.json_serializer.extra_code }} \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index dc1c02aa..d64bbd91 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = oarepo-model-builder -version = 4.0.54 +version = 4.0.55 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 fb5d554f..4c8a9a0a 100644 --- a/tests/test_datatype_prepare.py +++ b/tests/test_datatype_prepare.py @@ -121,6 +121,7 @@ def test_prepare_datatype(): "context-class": "PIDFieldContext", "field-args": ["create=True"], "field-class": "PIDField", + "extra-code": "", "generate": True, "imports": [ { diff --git a/tests/test_facets.py b/tests/test_facets.py index 6e895cfa..8d7546fe 100644 --- a/tests/test_facets.py +++ b/tests/test_facets.py @@ -1123,6 +1123,7 @@ def test_customizations_field(): """, ) + def test_facets_group(): schema = load_model( "test.yaml", @@ -1133,10 +1134,7 @@ def test_facets_group(): "properties": { "b": { "type": "keyword", - "facets": { - "facet-groups" : ["curator"] - - }, + "facets": {"facet-groups": ["curator"]}, }, "c": "fulltext", }, diff --git a/tests/test_model_saver.py b/tests/test_model_saver.py index 81b3a752..668ccd75 100644 --- a/tests/test_model_saver.py +++ b/tests/test_model_saver.py @@ -180,6 +180,7 @@ def test_model_saver(): }, {"import": "invenio_pidstore.providers.recordid_v2.RecordIdProviderV2"}, ], + "extra-code": "", }, "record-dumper": { "generate": True, @@ -541,6 +542,7 @@ def test_model_saver_invenio(): }, {"import": "invenio_pidstore.providers.recordid_v2.RecordIdProviderV2"}, ], + "extra-code": "", }, "record-dumper": { "generate": True, diff --git a/tests/test_search_options.py b/tests/test_search_options.py index 264e5db5..8f491abd 100644 --- a/tests/test_search_options.py +++ b/tests/test_search_options.py @@ -22,7 +22,10 @@ def test_sort(): }, "b": { "type": "keyword", - "facets": {"field": 'TermsFacet(field="cosi")', "facet-groups": ["curator"]}, + "facets": { + "field": 'TermsFacet(field="cosi")', + "facet-groups": ["curator"], + }, "sortable": {"key": "b_test", "order": "desc"}, }, "c": { @@ -305,41 +308,23 @@ def test_facet_groups(): "properties": { "a": { "type": "keyword", - "facets": { - "facet-groups": ["curator", "user"] - } + "facets": {"facet-groups": ["curator", "user"]}, }, "b": { "type": "keyword", - "facets": { - "searchable": True, - "facet-groups": [] - } + "facets": {"searchable": True, "facet-groups": []}, }, "c": { "type": "keyword", - "facets": { - "searchable": True, - "facet": False - - }, - }, - "d": { - "type" : "keyword", - "facets": { - "searchable": False - } + "facets": {"searchable": True, "facet": False}, }, + "d": {"type": "keyword", "facets": {"searchable": False}}, "g": { "type": "array", - "items": { "type": "keyword", - "facets": { - "facet-groups": ["curator"] - } - } - + "facets": {"facet-groups": ["curator"]}, + }, }, "arr": { "type": "array", @@ -347,7 +332,7 @@ def test_facet_groups(): "items": { "type": "nested", "properties": { - "d": {"type": "keyword", "facets" : {"facet": False}}, + "d": {"type": "keyword", "facets": {"facet": False}}, "e": { "type": "object", "properties": { @@ -357,7 +342,7 @@ def test_facet_groups(): }, }, }, - } + }, }, }, isort=False, @@ -382,6 +367,7 @@ def test_facet_groups(): os.path.join("test", "records", "mappings", "os-v2", "test", "test-1.0.0.json") ) import json + data3 = json.loads(data3) print(data3) assert strip_whitespaces(data) == strip_whitespaces( @@ -457,4 +443,4 @@ class TestSearchOptions(BaseSearchOptions): } """, - ) \ No newline at end of file + )