Skip to content

Commit

Permalink
GeoNode#10995 Faceting: allow AND filtering in API for tkeywords
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed May 11, 2023
1 parent 634baf7 commit 4fa7a86
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions geonode/base/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ def filter_queryset(self, request, queryset, view):
return queryset


class TKeywordsFilter(BaseFilterBackend):
"""
TKeywords are a ManyToMany relation but DREST can't handle AND filtering the way we need.
We'll be using "filter{tkeywords}" to have Resources having all of the tkeywords
"""

def filter_queryset(self, request, queryset, view):
f = request.GET.pop("filter{tkeywords}", None)
if f:
if not isinstance(f, list):
f = [f]
for v in f:
queryset = queryset.filter(tkeywords__id=v)
return queryset


class FavoriteFilter(BaseFilterBackend):
"""
Filter that only allows users to see their own objects.
Expand Down
9 changes: 8 additions & 1 deletion geonode/base/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@
from geonode.thumbs.utils import _decode_base64, BASE64_PATTERN
from geonode.groups.conf import settings as groups_settings
from geonode.base.models import HierarchicalKeyword, Region, ResourceBase, TopicCategory, ThesaurusKeyword
from geonode.base.api.filters import DynamicSearchFilter, ExtentFilter, FacetVisibleResourceFilter, FavoriteFilter
from geonode.base.api.filters import (
DynamicSearchFilter,
ExtentFilter,
FacetVisibleResourceFilter,
FavoriteFilter,
TKeywordsFilter,
)
from geonode.groups.models import GroupProfile, GroupMember
from geonode.people.utils import get_available_users
from geonode.security.permissions import get_compact_perms_list, PermSpec, PermSpecCompact
Expand Down Expand Up @@ -346,6 +352,7 @@ class ResourceBaseViewSet(DynamicModelViewSet):
authentication_classes = [SessionAuthentication, BasicAuthentication, OAuth2Authentication]
permission_classes = [IsAuthenticatedOrReadOnly, UserHasPerms]
filter_backends = [
TKeywordsFilter,
DynamicFilterBackend,
DynamicSortingFilter,
DynamicSearchFilter,
Expand Down

0 comments on commit 4fa7a86

Please sign in to comment.