Skip to content

Commit

Permalink
Adding extra code to search-options, pid (#231)
Browse files Browse the repository at this point in the history
* Adding extra code to search-options, pid
* Code formatted
  • Loading branch information
mesemus authored Oct 20, 2023
1 parent 448b830 commit f05946f
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 41 deletions.
10 changes: 8 additions & 2 deletions oarepo_model_builder/datatypes/components/facets/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion oarepo_model_builder/datatypes/components/model/facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions oarepo_model_builder/datatypes/components/model/pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions oarepo_model_builder/invenio/invenio_record_search_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ from flask_babelex import lazy_gettext as _
{{ facet.path }} = {{ facet.field }}
{% endfor %}

{#{{ vars.invenio_record_facets_extra_code }}#}
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ class {{ vars.search_options| class_header }}:
{% endfor %}
}

{{ vars.search_options.extra_code }}
Original file line number Diff line number Diff line change
@@ -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 }}"
pid_type = "{{ vars.pid.type }}"

{{ vars.pid.extra_code }}
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
)
)

{{ vars.json_serializer.extra_code }}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>, Miroslav Simek <[email protected]>
readme = README.md
Expand Down
1 change: 1 addition & 0 deletions tests/test_datatype_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def test_prepare_datatype():
"context-class": "PIDFieldContext",
"field-args": ["create=True"],
"field-class": "PIDField",
"extra-code": "",
"generate": True,
"imports": [
{
Expand Down
6 changes: 2 additions & 4 deletions tests/test_facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ def test_customizations_field():
""",
)


def test_facets_group():
schema = load_model(
"test.yaml",
Expand All @@ -1133,10 +1134,7 @@ def test_facets_group():
"properties": {
"b": {
"type": "keyword",
"facets": {
"facet-groups" : ["curator"]

},
"facets": {"facet-groups": ["curator"]},
},
"c": "fulltext",
},
Expand Down
2 changes: 2 additions & 0 deletions tests/test_model_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def test_model_saver():
},
{"import": "invenio_pidstore.providers.recordid_v2.RecordIdProviderV2"},
],
"extra-code": "",
},
"record-dumper": {
"generate": True,
Expand Down Expand Up @@ -541,6 +542,7 @@ def test_model_saver_invenio():
},
{"import": "invenio_pidstore.providers.recordid_v2.RecordIdProviderV2"},
],
"extra-code": "",
},
"record-dumper": {
"generate": True,
Expand Down
42 changes: 14 additions & 28 deletions tests/test_search_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -305,49 +308,31 @@ 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",
"facets": {"searchable": True},
"items": {
"type": "nested",
"properties": {
"d": {"type": "keyword", "facets" : {"facet": False}},
"d": {"type": "keyword", "facets": {"facet": False}},
"e": {
"type": "object",
"properties": {
Expand All @@ -357,7 +342,7 @@ def test_facet_groups():
},
},
},
}
},
},
},
isort=False,
Expand All @@ -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(
Expand Down Expand Up @@ -457,4 +443,4 @@ class TestSearchOptions(BaseSearchOptions):
}
""",
)
)

0 comments on commit f05946f

Please sign in to comment.