From e2f9e84fe3f0f18b5adc770b8f18a8c774f2a53a Mon Sep 17 00:00:00 2001
From: Benjamin Becker <benjamin.becker@dataport.de>
Date: Mon, 13 Dec 2021 12:29:29 +0000
Subject: [PATCH] adds filter for languages

---
 ckanext/odsh/helpers_tpsh.py                  | 22 ++++++++++---------
 ckanext/odsh/plugin.py                        |  2 ++
 .../odsh/templates/snippets/facet_list.html   |  6 +++++
 .../odsh/templates/snippets/search_form.html  |  4 +++-
 4 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/ckanext/odsh/helpers_tpsh.py b/ckanext/odsh/helpers_tpsh.py
index 695d372e..ce7a5c50 100644
--- a/ckanext/odsh/helpers_tpsh.py
+++ b/ckanext/odsh/helpers_tpsh.py
@@ -1,9 +1,7 @@
 # encoding: utf-8
 
 import csv
-import datetime
 import logging
-from string import lower
 import json
 import re
 import urllib2
@@ -13,21 +11,21 @@ import os
 
 from ckan.common import config
 import ckan.lib.helpers as helpers
-import ckan.logic.action.create as create
 import ckan.model as model
 import ckan.plugins.toolkit as toolkit
 
-
 import ckanext.odsh.helpers as odsh_helpers
 
+
 log = logging.getLogger(__name__)
 
+
 CKAN_TYPES = {'http://dcat-ap.de/def/datasetTypes/collection': 'collection'}
 
 
 def map_dct_type_to_ckan_type(dct_type):
     '''
-    matches the field dct:type from a harvested rdf file 
+    matches the field dct:type from a harvested rdf file
     to the corresponding ckan package type
     '''
     ckan_type = CKAN_TYPES.get(dct_type)
@@ -142,6 +140,10 @@ def _get_language_id(pkg_dict):
     language_id_cleaned = re.sub('[\[\]\"]', '', language_id)
     return language_id_cleaned
 
+def get_language_human_readable(language_id):
+    LANGUAGE_MAPPING = load_language_mapping()
+    return LANGUAGE_MAPPING.get(language_id)
+
 def get_spatial_for_selection():
     mapping_path = config.get('ckanext.odsh.spatial.mapping')
     try:
@@ -152,10 +154,10 @@ def get_spatial_for_selection():
     cr = csv.reader(mapping_file, delimiter="\t")
     spatial_mapping = list()
     for row in cr:
-        key  = row[0].decode('UTF-8')
+        key = row[0].decode('UTF-8')
         value = row[1].decode('UTF-8')
-        spatial_mapping.append({'key':key, 'value':value}) 
-    spatial_mapping.append({'key':'', 'value':''})   
+        spatial_mapping.append({'key': key, 'value': value}) 
+    spatial_mapping.append({'key': '', 'value': ''})   
     return spatial_mapping
 
 def get_subject_for_selection():
@@ -179,7 +181,7 @@ def get_package_dict(name):
     return package_dict
 
 def size_of_fmt(num, suffix='B'):
-    for unit in ['',' k',' M',' G',' T',' P',' E',' Z']:
+    for unit in ['', ' k', ' M', ' G', ' T', ' P', ' E', ' Z']:
         if abs(num) < 1000.0:
             return "%3.1f%s%s" % (num, unit, suffix)
         num /= 1000.0
@@ -188,7 +190,7 @@ def size_of_fmt(num, suffix='B'):
 def get_resource_size(resource):
     resource_size = resource.get('size')
     if resource_size:
-        return size_of_fmt(resource_size)    
+        return size_of_fmt(resource_size)
 
 
 def get_address_org(organization):
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index 800184e2..f0187774 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -177,6 +177,7 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
                             'groups': _('Kategorie'),
                             'subject_text': _('Informationsgegenstand'),
                             'tags': _('Tags'),
+                            'language': _('Language'),
                             })
 
     def group_facets(self, facets_dict, group_type, package_type):
@@ -385,6 +386,7 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
             'tpsh_get_body_mail':helpers_tpsh.get_body_mail,
             'tpsh_git_commit_hash': helpers_tpsh.git_commit_hash,
             'tpsh_is_informationsgegenstand_gerichtsurteil_selected': helpers_tpsh.is_informationsgegenstand_gerichtsurteil_selected,
+            'tpsh_get_language_human_readable': helpers_tpsh.get_language_human_readable,
         }
 
     
diff --git a/ckanext/odsh/templates/snippets/facet_list.html b/ckanext/odsh/templates/snippets/facet_list.html
index d2b85219..ee1bbcbd 100644
--- a/ckanext/odsh/templates/snippets/facet_list.html
+++ b/ckanext/odsh/templates/snippets/facet_list.html
@@ -44,9 +44,15 @@
           {% endif %}
           
           <label class="odsh-visible-label" for="check-{{ title.lower() }}-{{ loop.index }}">
+            {% if name=='language' %}
+            <a href="{{ href }}" title="{{ label if label != label_truncated else '' }}">
+              <span id="description-{{ title.lower() }}-{{ loop.index }}">{{ h.tpsh_get_language_human_readable(label) }}</span>
+            </a>
+            {% else %}
             <a href="{{ href }}" title="{{ label if label != label_truncated else '' }}">
               <span id="description-{{ title.lower() }}-{{ loop.index }}">{{ label }}</span>
             </a>
+            {% endif %}
           </label>
         </div>
         <div class="facet_count">
diff --git a/ckanext/odsh/templates/snippets/search_form.html b/ckanext/odsh/templates/snippets/search_form.html
index d14cbccc..ebdcbf4f 100644
--- a/ckanext/odsh/templates/snippets/search_form.html
+++ b/ckanext/odsh/templates/snippets/search_form.html
@@ -77,8 +77,10 @@
         {% set search_facets_items = facets.search.get(field)['items'] %}
         {% for value in facets.fields[field] %}
         <span class="filtered pill">
-            {% if(field=='openness')%}
+            {% if field == 'openness' %}
                 {{_(value)}}
+            {%- elif field == 'language' -%}
+                {{ h.tpsh_get_language_human_readable(value) }}
             {%- else -%}
                 {%- if facets.translated_fields and facets.translated_fields.has_key((field,value)) -%}
                 {{ facets.translated_fields[(field,value)] }}
-- 
GitLab