Skip to content

Commit

Permalink
GeoNode#10995 Faceting: topiccategory
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed May 3, 2023
1 parent 118a45c commit fc03dc2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions geonode/facets/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from django.apps import AppConfig

from geonode.facets.models import FacetProvider
from geonode.facets.providers.category import CategoryFacetProvider

logger = logging.getLogger(__name__)

Expand All @@ -44,6 +45,7 @@ def init_providers():
from geonode.facets.providers.thesaurus import create_thesaurus_providers
from geonode.facets.providers.users import OwnerFacetProvider

register_facet_provider(CategoryFacetProvider())
register_facet_provider(OwnerFacetProvider())

# Thesaurus providers initialiazion should be called at startup and whenever records in Thesaurus or ThesaurusLabel change
Expand Down
61 changes: 61 additions & 0 deletions geonode/facets/providers/category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import logging

from django.db.models import Count

from geonode.facets.models import FacetProvider, DEFAULT_FACET_PAGE_SIZE, FACET_TYPE_CATEGORY

logger = logging.getLogger(__name__)


class CategoryFacetProvider(FacetProvider):
"""
Implements faceting for resource's topicCategory
"""

@property
def name(self) -> str:
return "category"

def get_info(self, lang="en") -> dict:
return {
"name": self.name,
"key": "filter{category__identifier}",
"label": "Category",
"type": FACET_TYPE_CATEGORY,
"hierarchical": False,
"order": 2,
}

def get_facet_items(
self,
queryset=None,
start: int = 0,
end: int = DEFAULT_FACET_PAGE_SIZE,
lang="en",
topic_contains: str = None,
) -> (int, list):
logger.debug("Retrieving facets for %s", self.name)

q = queryset.values("category__identifier", "category__gn_description", "category__fa_class")
if topic_contains:
q = q.filter(category__gn_description=topic_contains)
q = q.annotate(count=Count("owner")).order_by("-count")

cnt = q.count()

logger.info("Found %d facets for %s", cnt, self.name)
logger.debug(" ---> %s\n\n", q.query)
logger.debug(" ---> %r\n\n", q.all())

topics = [
{
"key": r["category__identifier"],
"label": r["category__gn_description"],
"localized_label": r["category__gn_description"],
"count": r["count"],
"fa_class": r["category__fa_class"],
}
for r in q[start:end].all()
]

return cnt, topics

0 comments on commit fc03dc2

Please sign in to comment.