Skip to content

Commit

Permalink
[Fixes GeoNode#8690] Dynamic metadata facet filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Feb 3, 2022
1 parent b502668 commit 73220a3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion geonode/api/resourcebase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def build_filters(self, filters=None, ignore_bad_filters=False, **kwargs):
orm_filters.update({'polymorphic_ctype__model__in': [filt.lower() for filt in filters.getlist('app_type__in')]})
if 'extent' in filters:
orm_filters.update({'extent': filters['extent']})

# getting metadata filters
_metadata = {f"metadata__{k}__in": filters.getlist(k) for k in filter(lambda x: x.startswith('metadata__'), filters)}
if _metadata:
orm_filters.update({"metadata_filters": _metadata})
Expand Down
15 changes: 10 additions & 5 deletions geonode/base/templatetags/base_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
HierarchicalKeyword, Menu, MenuItem
)
from geonode.security.utils import get_visible_resources
from collections import OrderedDict
from collections import OrderedDict, Counter

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -530,7 +530,12 @@ def dynamic_metadata_filters(context):
return output

def _get_filter_by_category(category, metadata_available):
metadata_for_category = metadata_available.filter(
metadata__filter_header=category
).distinct()
return [_el.metadata for _el in metadata_for_category]
metadata_for_category = metadata_available\
.filter(metadata__filter_header=category)

counters = Counter(metadata_for_category.values_list('metadata__field_name', 'metadata__field_value'))
out = []
for _el in metadata_for_category.distinct("metadata__field_name", "metadata__field_value"):
cnt = counters.get((_el.metadata['field_name'], _el.metadata['field_value']), 0)
out.append({**{"id": _el.id, "count": cnt}, **_el.metadata})
return out
7 changes: 4 additions & 3 deletions geonode/templates/search/_dynamic_metadata_filter.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
{% dynamic_metadata_filters as data %}
{% for key, values in data.items %}
<nav class="filter">
<h4><a href="#" class="toggle toggle-nav"><i class="fa fa-chevron-down"></i>{{key}}</a></h4>
<ul class="nav closed" id="dynamic_filter_{{ key }}">
<h4><a href="#" class="toggle toggle-nav"><i class="fa fa-chevron-right"></i>{{key}}</a></h4>
<ul class="nav closed" id="dynamic_filter_{{ key|slugify }}">
{% for el in values %}
<li>
<a id="dynamic_filter_{{ el.field_name }}" data-value="{{ el.field_value }}" data-filter="metadata__field_value" ng-click="multiple_choice_listener($event)">
<a id="dynamic_filter_{{ el.id }}" data-value="{{ el.field_value }}" data-filter="metadata__field_value" ng-click="multiple_choice_listener($event)">
{{ el.field_label }}
<span class="badge pull-right">{{el.count}}</span>
</a>
</li>
{% endfor %}
Expand Down

0 comments on commit 73220a3

Please sign in to comment.