Skip to content

Commit

Permalink
Merge pull request #228 from oarepo/alzbetapokorna/be-115-facet-group…
Browse files Browse the repository at this point in the history
…s-in-model-builder

facet groups
  • Loading branch information
Alzpeta authored Oct 4, 2023
2 parents dfb2385 + 4edd447 commit 1fb3ff1
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 24 deletions.
2 changes: 2 additions & 0 deletions oarepo_model_builder/datatypes/components/facets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class FacetDefinition:
dot_path: str
searchable: bool
imports: List[Dict[str, str]]
facet_groups: List[str]
facet: Optional[bool]
field: Optional[str] = None

def update(self, facet_section):
Expand Down
5 changes: 4 additions & 1 deletion oarepo_model_builder/datatypes/components/facets/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ 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 = ma.fields.Bool(required=False)

class RegularFacetsComponent(DataTypeComponent):
eligible_datatypes = []
Expand Down Expand Up @@ -52,6 +53,8 @@ def process_facets(self, datatype, section, **__kwargs):
dot_path=datatype.path,
searchable=facet_section.get("searchable"),
imports=facet_section.get("imports", []),
facet=facet_section.get("facet", None),
facet_groups= facet_section.get("facet-groups", ["default"])
)

# set the field on the definition
Expand Down
2 changes: 1 addition & 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,7 @@ 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:
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
13 changes: 11 additions & 2 deletions oarepo_model_builder/invenio/invenio_record_search_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ class InvenioRecordSearchOptionsBuilder(InvenioBaseClassPythonBuilder):

def finish(self, **extra_kwargs):
facets: List[FacetDefinition] = get_distinct_facets(self.current_model)

facet_groups = {}
default_group = []
search_data = []
for f in facets:
for group in f.facet_groups:
if group != 'default':
if group not in facet_groups.keys():
facet_groups[group] = {}
facet_groups[group][f.path] = "facets." + f.path
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:
sort_options = self.current_model.definition["sortable"]
else:
sort_options = {}
extra_kwargs["search_data"] = search_data
extra_kwargs["facet_groups"] = facet_groups
extra_kwargs["default_group"] = default_group
extra_kwargs["sort_definition"] = sort_options
super().finish(**extra_kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ from . import facets
class {{ vars.search_options| class_header }}:
"""{{ vars.record.class|base_name }} search options."""

facet_groups ={
{% for group in facet_groups %}
'{{ group }}': {
{% for dict in facet_groups[group].keys() %}
'{{ dict}}' : {{ facet_groups[group][dict] }},

{% endfor %}
{% for base_class in vars.search_options.base_classes %}
**getattr(({{ base_class|base_name }}, 'facet_groups', {}).get('{{group }}', {}))
{% endfor %}
},
{% endfor %}
}

facets = {
{% for dict in search_data %}
{% for dict in default_group %}
{% for key, value in dict.items()%}
'{{ key }}': {{ value }},
{% endfor %}
Expand Down
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.52
version = 4.0.53
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
65 changes: 65 additions & 0 deletions tests/test_facets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,5 +1120,70 @@ def test_customizations_field():
""",
)

def test_facets_group():
schema = load_model(
"test.yaml",
model_content={
"record": {
"use": "invenio",
"module": {"qualified": "test"},
"properties": {
"b": {
"type": "keyword",
"facets": {
"facet-groups" : ["curator"]

},
},
"c": "fulltext",
},
},
},
isort=False,
black=False,
autoflake=False,
)

filesystem = InMemoryFileSystem()
builder = create_builder_from_entrypoints(filesystem=filesystem)
builder.build(schema, "record", ["record"], "")

data = (
builder.filesystem.open(
os.path.join("test", "services", "records", "facets.py")
)
.read()
.replace("'", '"')
)
print(data)
assert re.sub(r"\s", "", data) == re.sub(
r"\s",
"",
"""
\"""Facet definitions.\"""
from invenio_search.engine import dsl
from flask_babelex import lazy_gettext as _
from invenio_records_resources.services.records.facets import TermsFacet
from oarepo_runtime.facets.date import DateTimeFacet
_schema = TermsFacet(field="$schema", label =_("$schema.label"))
b = TermsFacet(field="b", label =_("b.label"))
created = DateTimeFacet(field="created", label =_("created.label"))
_id = TermsFacet(field="id", label =_("id.label"))
updated = DateTimeFacet(field="updated", label =_("updated.label"))
""",
)
Loading

0 comments on commit 1fb3ff1

Please sign in to comment.