Skip to content

Commit

Permalink
[Fixes GeoNode#8690] first implementation for dynamic filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Feb 2, 2022
1 parent e462385 commit 810999e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 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']})
_metadata = {f"metadata__{_k}": _v for _k, _v in filters.items() if _k.startswith('metadata')}
_metadata = {f"metadata__{_k}": _v for _k, _v in filters.items() if _k.startswith('metadata__')}
if _metadata:
orm_filters.update({"metadata_filters": _metadata})
orm_filters['f_method'] = filters['f_method'] if 'f_method' in filters else 'and'
Expand Down
41 changes: 40 additions & 1 deletion geonode/base/templatetags/base_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#
#########################################################################

from enum import Enum
import logging

from django import template
from django.db.models import Q
from django.conf import settings
from django.db.models import Count
from django.forms import model_to_dict
from django.utils.translation import ugettext
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
Expand All @@ -31,7 +33,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 @@ -55,6 +57,12 @@
'wms': _('WMS Cascade Layer')
}

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


@register.filter(name='template_trans')
def template_trans(text):
Expand Down Expand Up @@ -495,3 +503,34 @@ 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
metadata_available = metadata_available\
.filter(resource__polymorphic_ctype__model=resource_type)

if not metadata_available.exists():
return []

categories = metadata_available.values_list('metadata__category', 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__category=category
)
return [_el.metadata for _el in metadata_for_category]
1 change: 0 additions & 1 deletion geonode/maps/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@ def test_that_non_admin_user_can_create_edit_keyword_when_freetext_keywords_read
test_map.refresh_from_db()
self.assertEqual("map", test_map.title)


@patch('geonode.thumbs.thumbnails.create_thumbnail')
def test_map_metadata(self, thumbnail_mock):
"""Test that map metadata can be properly rendered
Expand Down
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 810999e

Please sign in to comment.