From 8529df1f9cfb13f18d44f4de8c4f54812891ac12 Mon Sep 17 00:00:00 2001
From: anonymous <anonymous>
Date: Tue, 30 Oct 2018 14:17:17 +0100
Subject: [PATCH] ODPSH-8: adds qa-functionality to our templates

---
 ckanext/odsh/helpers.py                       | 20 +++++++++++++
 ckanext/odsh/plugin.py                        |  4 ++-
 ckanext/odsh/templates/qa/stars.html          | 28 +++++++++++++++++++
 .../odsh/templates/snippets/package_item.html |  3 +-
 licenses.json                                 | 10 +++----
 5 files changed, 58 insertions(+), 7 deletions(-)
 create mode 100644 ckanext/odsh/helpers.py
 create mode 100644 ckanext/odsh/templates/qa/stars.html

diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py
new file mode 100644
index 00000000..6a9786bb
--- /dev/null
+++ b/ckanext/odsh/helpers.py
@@ -0,0 +1,20 @@
+import logging
+import traceback
+import ast
+
+log = logging.getLogger(__name__)
+
+
+def odsh_openness_score_dataset_html(dataset):
+    score = 0
+    #dataset = json.loads(dataset)
+    for resource in dataset.get('resources'):
+        try:
+            qa = ast.literal_eval(resource.get('qa'))
+            resource_score = qa.get('openness_score')
+            if resource_score > score:
+                score = resource_score
+        except AttributeError, e:
+            log.error('Error while calculating openness score %s: %s\nException: %s',
+                  e.__class__.__name__,  unicode(e), traceback.format_exc())
+    return score
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index 8a441626..2e98cb6c 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -5,6 +5,7 @@ from ckan.lib.plugins import DefaultTranslation
 from ckan.lib.plugins import DefaultDatasetForm
 from ckan.common import OrderedDict
 import ckan.lib.helpers as helpers
+import helpers as odsh_helpers
 
 import logging
 
@@ -83,6 +84,7 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
                 'odsh_now': odsh_now,
                 'odsh_group_id_selected': odsh_group_id_selected,
                 'odsh_get_facet_items_dict': odsh_get_facet_items_dict,
+                'odsh_openness_score_dataset_html': odsh_helpers.odsh_openness_score_dataset_html,
         }
 
     def before_map(self, map):
@@ -104,7 +106,7 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
         return ['title','notes']
 
     def _extraFields(self):
-        return ['publish_date','access_constraints','temporal_start','temporal_end','spatial_extension']
+        return ['publish_date','access_constraints','temporal_start','temporal_end','spatial_extension', 'qa']
 
     def _update_schema(self,schema):
         for field in self._extraFields():
diff --git a/ckanext/odsh/templates/qa/stars.html b/ckanext/odsh/templates/qa/stars.html
new file mode 100644
index 00000000..48373d1f
--- /dev/null
+++ b/ckanext/odsh/templates/qa/stars.html
@@ -0,0 +1,28 @@
+{#
+Renders a set of stars for the current dataset or if it has none a string
+explaining the reason.
+
+stars  - The number of stars assigned to the dataset.
+reason - A reason why the dataset doesn't have a rating.
+
+{% snippet "qa/stars.html", stars=4 %}
+
+#}
+<span class="star-rating{% if stars == 0 %} no-stars{% endif %}">
+  {%- if stars == None -%}
+  {{ reason }}
+  {%- else -%}
+  <span class="star-rating-stars">
+    {%- for index in range(stars) -%}
+    <span class="fa fa-star odsh-star"></span>
+    {%- endfor -%}
+    {%- for index in range(5-stars) -%}
+    <span class="fa fa-star-o odsh-star"></span>
+    {%- endfor -%}
+    {%- if stars == 0 -%}
+    {# 0 stars looks awfully like 5 stars, so be explicit #}
+    {# {{ _("{0} out of {1}").format(stars, 5) }} #}
+    {%- endif -%}
+  </span>
+  {%- endif -%}
+</span>
diff --git a/ckanext/odsh/templates/snippets/package_item.html b/ckanext/odsh/templates/snippets/package_item.html
index 7eb2551d..9ce161b5 100644
--- a/ckanext/odsh/templates/snippets/package_item.html
+++ b/ckanext/odsh/templates/snippets/package_item.html
@@ -20,6 +20,7 @@ Example:
 {% set license = package.license or '-' %}
 {% set timerange = '-' %}
 {% set org = package.organization.title or package.organization.name or '-' %}
+{% set stars = h.odsh_openness_score_dataset_html(package) %}
 {% set access_count=(package.tracking_summary.total if package.tracking_summary) or '-' %}
 {% set timerange_label ='Zeitraum' %}
 {% set access_count_label ='Aufrufe' %}
@@ -81,7 +82,7 @@ Example:
       {% endblock %}
     </ul>
     <div class="dataset-stars">
-      <i class="fa fa-star odsh-star"></i><i class="fa fa-star odsh-star"></i><i class="fa fa-star odsh-star"></i><i class="fa fa-star odsh-star"></i><i class="fa fa-star-o odsh-star"></i>
+       {% snippet "qa/stars.html", stars=stars %}
     </div>
 
     <div class="dataset-info">
diff --git a/licenses.json b/licenses.json
index 54e1227c..a64f6d46 100644
--- a/licenses.json
+++ b/licenses.json
@@ -6,8 +6,8 @@
     "family": "",
     "id": "http://dcat-ap.de/def/licenses/dl-by-de/2.0",
     "is_generic": true,
-    "is_okd_compliant": true,
-    "is_osi_compliant": false,
+    "od_conformance": "approved",
+    "osd_conformance": "not reviewed",
     "maintainer": "",
     "status": "active",
     "title": "Datenlizenz Deutschland Namensnennung 2.0",
@@ -20,11 +20,11 @@
     "family": "",
     "id": "http://dcat-ap.de/def/licenses/dl-zero-de/2.0",
     "is_generic": true,
-    "is_okd_compliant": true,
-    "is_osi_compliant": true,
+    "od_conformance": "approved",
+    "osd_conformance": "approved",
     "maintainer": "",
     "status": "active",
     "title": "Datenlizenz Deutschland – Zero – Version 2.0",
     "url": "https://www.govdata.de/dl-de/zero-2-0"
   }
-]
\ No newline at end of file
+]
-- 
GitLab