Skip to content

Commit

Permalink
Merge pull request #262 from oarepo/small_facet_template_fix
Browse files Browse the repository at this point in the history
Update invenio_record_search_options.py.jinja2
  • Loading branch information
Alzpeta authored Mar 27, 2024
2 parents 3d4d2c3 + 34b55c5 commit 8be8e7c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 61 deletions.
118 changes: 60 additions & 58 deletions oarepo_model_builder/invenio/invenio_record_search_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,66 +13,68 @@ class InvenioRecordSearchOptionsBuilder(InvenioBaseClassPythonBuilder):

def finish(self, **extra_kwargs):
facets: List[FacetDefinition] = get_distinct_facets(self.current_model)
facet_groups = {}
default_group = []
search_data = []

# gather all facet group names
facet_group_names = set()
for f in facets:
facet_group_names.update(f.facet_groups.keys())

# there might be a definition of facet groups on the model's facets -> facet-groups
# if there is, we need to merge those
top_level_facet_groups = self.current_model.definition.get("facets", {}).get(
"facet-groups", {}
)
facet_group_names.update(top_level_facet_groups.keys())
path_to_facet_group = defaultdict(dict)

for group, group_def in top_level_facet_groups.items():
for path, priority in group_def.items():
path_to_facet_group[group][path] = priority

# for each group name, gather all facets with that group name
# sort them by their order in the group
# and add them to the facet_groups dict
for group in sorted(facet_group_names):
# skip the default group
if group == "_default":
continue

# gather all facets with this facet_group_name
group_members = []
for f in facets:
if group in f.facet_groups:
group_members.append((f, f.facet_groups[group]))

# if there is a definition of facet groups on the model's facets -> add the selected facets as well
for path, priority in path_to_facet_group.get(group, {}).items():
if f.dot_path.startswith(path):
group_members.append((f, priority))

# sort the group members by their order in the facet_group
group_members.sort(key=lambda x: x[1])

if group not in facet_groups.keys():
facet_groups[group] = {}

for f, _ in group_members:
facet_groups[group][f.path] = "facets." + f.path

for f in facets:
if len(f.facet_groups) > 0:
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 = {}
facet_groups, default_group, sort_options = facet_data(facets, self.current_model)

extra_kwargs["facet_groups"] = facet_groups
extra_kwargs["default_group"] = default_group
extra_kwargs["sort_definition"] = sort_options
super().finish(**extra_kwargs)

def facet_data(facets, current_model):
facet_groups = {}
default_group = []
search_data = []

# gather all facet group names
facet_group_names = set()
for f in facets:
facet_group_names.update(f.facet_groups.keys())

top_level_facet_groups = current_model.definition.get("facets", {}).get(
"facet-groups", {}
)
facet_group_names.update(top_level_facet_groups.keys())
path_to_facet_group = defaultdict(dict)

for group, group_def in top_level_facet_groups.items():
for path, priority in group_def.items():
path_to_facet_group[group][path] = priority

# for each group name, gather all facets with that group name
# sort them by their order in the group
# and add them to the facet_groups dict
for group in sorted(facet_group_names):
# skip the default group
if group == "_default":
continue

# gather all facets with this facet_group_name
group_members = []
for f in facets:
if group in f.facet_groups:
group_members.append((f, f.facet_groups[group]))

# if there is a definition of facet groups on the model's facets -> add the selected facets as well
for path, priority in path_to_facet_group.get(group, {}).items():
if f.dot_path.startswith(path):
group_members.append((f, priority))

# sort the group members by their order in the facet_group
group_members.sort(key=lambda x: x[1])

if group not in facet_groups.keys():
facet_groups[group] = {}

for f, _ in group_members:
facet_groups[group][f.path] = "facets." + f.path

for f in facets:
if len(f.facet_groups) > 0:
default_group.append({f.path: "facets." + f.path})
search_data.append({f.path: "facets." + f.path})

if "sortable" in current_model.definition:
sort_options = current_model.definition["sortable"]
else:
sort_options = {}
return facet_groups, default_group, sort_options
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ from . import facets
class {{ vars.search_options| class_header }}:
"""{{ vars.record.class|base_name }} search options."""

facet_groups ={
facet_groups = {
{% for group in facet_groups %}
'{{ group }}': {
{% for dict in facet_groups[group].keys() %}
Expand Down Expand Up @@ -57,4 +57,4 @@ class {{ vars.search_options| class_header }}:
{{ extra_field_name }} = {{ extra_field_value|extra_code }}
{% endfor %}

{{ vars.search_options|extra_code }}
{{ vars.search_options|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.82
version = 4.0.83
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

0 comments on commit 8be8e7c

Please sign in to comment.