Skip to content

Commit

Permalink
[3.3.x] [Fixes #8690] Custom filters available for the “ResourceBase”…
Browse files Browse the repository at this point in the history
… opaque filters (#8733)

* [Fixes #8689] Extend the ResourceBase metadata model with an opaque JSONField

* [Fixes #8689] Fix missing resource_type for new form instances

* [Fixes #8689] Add test and UI fix for doc, maps and geoapps

* [Fixes #8689] Fix flakee8 formatting

* [Fixes #8689] Extra metadata json saved with format

* [Fixes #8689] Refactor validation def, start defining endpoint for API

* [Fixes #8689] Definition of extra-metadata endpoints for resources

* [Fixes #8689] Converting metadata from jsonfield to manytomany relation

* [Fixes #8689] Fix views with new relation and prettify json on UI

* [Fixes #8689] Fix serializer

* [Fixes #8689] Fix custom metadata endpoint, update metadata schema

* [Fixes #8689] Fix flake8 issues

* [Fixes #8689] Remove endpoint from each resorce, keep it only on base resource

* [Fixes #8689] Fix broken tests

* [Fixes #8689] Add metadata filtering in API v1

* [Fixes #8689] Add test for metadata filtering in API v1

* [Fixes #8689] Fix some of broken tests

* [Fixes #8689] fix flake8

* [Fixes #8689] fix tests

* [Fixes #8689] removed typo on settings.py

* [Fixes #8689] fix broken build

* [Fixes #8689] fix minor error on filter convertion

* [Fixes #8690] first implementation for dynamic filtering

* [Fixes #8690] first implementation for dynamic filtering

* [Fixes #8689] fix flake8

* [Fixes #8689] Update default schema structure

* [Fixes #8690] first implementation for dynamic filtering

* [Fixes #8690] Dynamic metadata facet filtering

* [Fixes #8690] Minor fix for geoapps

* [Fixes #8690] Flake8 format fix
  • Loading branch information
mattiagiupponi authored Feb 4, 2022
1 parent 6d2fe7f commit 34a9746
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
3 changes: 2 additions & 1 deletion geonode/api/resourcebase_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ 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']})
_metadata = {f"metadata__{_k}": _v for _k, _v in filters.items() if _k.startswith('metadata__')}
# 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})
orm_filters['f_method'] = filters['f_method'] if 'f_method' in filters else 'and'
Expand Down
53 changes: 51 additions & 2 deletions geonode/base/templatetags/base_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
#########################################################################

from enum import Enum
import logging

from django import template
Expand All @@ -31,7 +32,7 @@
from pinax.ratings.models import Rating
from guardian.shortcuts import get_objects_for_user

from geonode.base.models import ResourceBase
from geonode.base.models import ExtraMetadata, ResourceBase
from geonode.base.bbox_utils import filter_bbox
from geonode.layers.models import Layer
from geonode.maps.models import Map
Expand All @@ -41,7 +42,9 @@
HierarchicalKeyword, Menu, MenuItem
)
from geonode.security.utils import get_visible_resources
from collections import OrderedDict
from collections import OrderedDict, Counter

from geonode.utils import get_geoapps_models

logger = logging.getLogger(__name__)

Expand All @@ -56,6 +59,13 @@
}


class FACET_TO_RESOURCE_TYPE(Enum):
layers = 'layer'
maps = 'map'
documents = 'document'
geoapps = 'geoapp'


@register.filter(name='template_trans')
def template_trans(text):
try:
Expand Down Expand Up @@ -495,3 +505,42 @@ def get_layer_count_by_services(service_id, user):
queryset=Layer.objects.filter(remote_service=service_id),
user=user
).count()


@register.simple_tag(takes_context=True)
def dynamic_metadata_filters(context):

facet_type = context.get('facet_type', 'all')

metadata_available = ExtraMetadata.objects.all()

if facet_type != 'all':
resource_type = [getattr(FACET_TO_RESOURCE_TYPE, facet_type).value]
if 'geoapp' in resource_type:
resource_type = [''.join(list(x.models)) for x in get_geoapps_models()]
metadata_available = metadata_available\
.filter(resource__polymorphic_ctype__model__in=resource_type)

if not metadata_available.exists():
return []

categories = metadata_available.values_list('metadata__filter_header', flat=True).distinct()

output = {}

for _cat in categories:
output[_cat] = _get_filter_by_category(_cat, metadata_available)

return output


def _get_filter_by_category(category, metadata_available):
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
18 changes: 18 additions & 0 deletions geonode/templates/search/_dynamic_metadata_filter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% load i18n %}
{% load base_tags %}
{% 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-right"></i>{{key}}</a></h4>
<ul class="nav closed" id="dynamic_filter_{{ key|slugify }}">
{% for el in values %}
<li>
<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 %}
</ul>
</nav>
{% endfor %}
1 change: 1 addition & 0 deletions geonode/templates/search/_search_content.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
{% if SEARCH_FILTERS.EXTENT_ENABLED %}
{% include "search/_extent_filter.html" %}
{% endif %}
{% include "search/_dynamic_metadata_filter.html" %}
{% endblock search_facet_lists %}
</div>
{% endif %}
Expand Down

0 comments on commit 34a9746

Please sign in to comment.