From 4f650df681330c783a75442b16d040635a77a2d4 Mon Sep 17 00:00:00 2001
From: anonymous <anonymous>
Date: Mon, 6 May 2019 16:07:03 +0200
Subject: [PATCH] fix facet filter

---
 ckanext/odsh/helpers.py                       | 19 ++++++++++++++++++-
 ckanext/odsh/plugin.py                        |  3 ++-
 .../odsh/templates/snippets/facet_list.html   |  2 +-
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py
index 138f96b5..12e9aebb 100644
--- a/ckanext/odsh/helpers.py
+++ b/ckanext/odsh/helpers.py
@@ -12,6 +12,7 @@ from ckan.common import config
 import urllib
 import hashlib
 import re
+from ckan.common import request
 
 get_action = logic.get_action
 log = logging.getLogger(__name__)
@@ -193,4 +194,20 @@ def presorted_license_options(existing_license_id=None):
     return [('','')]+[
         (license_id,
          register[license_id].title if license_id in register else license_id)
-        for license_id in license_ids]
\ No newline at end of file
+        for license_id in license_ids]
+
+
+def odsh_has_more_facets(facet, limit=None, exclude_active=False):
+    facets = []
+    for facet_item in c.search_facets.get(facet)['items']:
+        if not len(facet_item['name'].strip()) or facet_item['count']==0:
+            continue
+        if not (facet, facet_item['name']) in request.params.items():
+            facets.append(dict(active=False, **facet_item))
+        elif not exclude_active:
+            facets.append(dict(active=True, **facet_item))
+    if c.search_facets_limits and limit is None:
+        limit = c.search_facets_limits.get(facet)
+    if limit is not None and len(facets) > limit:
+        return True
+    return False
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index 335544fa..430fd4e5 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -248,7 +248,8 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
                 'odsh_create_checksum': odsh_helpers.odsh_create_checksum,
                 'presorted_license_options': odsh_helpers.presorted_license_options,
                 'odsh_tracking_id': odsh_helpers.odsh_tracking_id,
-                'odsh_tracking_url': odsh_helpers.odsh_tracking_url
+                'odsh_tracking_url': odsh_helpers.odsh_tracking_url,
+                'odsh_has_more_facets': odsh_helpers.odsh_has_more_facets
                 }
 
     def after_map(self, map):
diff --git a/ckanext/odsh/templates/snippets/facet_list.html b/ckanext/odsh/templates/snippets/facet_list.html
index 38a8e1a4..1742e444 100644
--- a/ckanext/odsh/templates/snippets/facet_list.html
+++ b/ckanext/odsh/templates/snippets/facet_list.html
@@ -42,7 +42,7 @@
 
 <div class="module-footer">
     {% if h.get_param_int('_%s_limit' % name) %}
-    {% if h.has_more_facets(name) %}
+    {% if h.odsh_has_more_facets(name) %}
     <a href="{{ h.remove_url_param('_%s_limit' % name, replace=0, extras=extras, alternative_url=alternative_url) }}"
         class="read-more">> {{ _('Show More') }}</a>
     {% endif %}
-- 
GitLab