From 6330f91be7e2528835b7510ef8a8bf63f11707fd Mon Sep 17 00:00:00 2001
From: Thorge Petersen <petersen@rz.uni-kiel.de>
Date: Fri, 8 Dec 2023 12:45:37 +0100
Subject: [PATCH] Added helper functions for loading applicableLegislation and
 hvdCategory mappings

---
 ckanext/odsh/helpers.py | 63 +++++++++++++++++++++++++++++++++++++++++
 ckanext/odsh/plugin.py  |  2 ++
 2 files changed, 65 insertions(+)

diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py
index 7ade4ebf..2ffbe79c 100644
--- a/ckanext/odsh/helpers.py
+++ b/ckanext/odsh/helpers.py
@@ -500,6 +500,69 @@ def short_name_for_category(category_name):
     }
     return translations.get(category_name)
 
+def odsh_load_applicable_legislations():
+    '''
+    Load applicable legislations.
+    '''
+
+    extension_path = pkg_resources.resource_filename('ckanext.odsh', '')
+    default_mapping_file_path = extension_path + '/resources/applicable_legislations.json'
+    mapping_file_path = config.get(
+        'ckanext.odsh.applicable_legislations_file_path', default_mapping_file_path)
+
+    try:
+        with open(mapping_file_path) as mapping_json:
+            MAPPING = json.loads(
+                mapping_json.read(), object_pairs_hook=OrderedDict)
+            default = [{'value': 'Geltende Rechtsvorschrift wählen..', 'key': ''}]
+            options = [{'key': key, 'value': MAPPING[key]}
+                   for key in MAPPING]
+            result = default+options
+    except IOError as err:
+        log.error(
+            'Could not load mapping file from {}'
+            .format(mapping_file_path)
+        )
+        raise
+    except ValueError as err:
+        log.error(
+            'Could not convert mapping file from json. \nMapping file: {}'
+            .format(mapping_file_path)
+        )
+        raise
+    return result
+
+def odsh_load_hvd_categories():
+    '''
+    Load HVD categories.
+    '''
+
+    extension_path = pkg_resources.resource_filename('ckanext.odsh', '')
+    default_mapping_file_path = extension_path + '/resources/hvd_categories.json'
+    mapping_file_path = config.get(
+        'ckanext.odsh.hvd_categories_file_path', default_mapping_file_path)
+
+    try:
+        with open(mapping_file_path) as mapping_json:
+            MAPPING = json.loads(
+                mapping_json.read(), object_pairs_hook=OrderedDict)
+            default = [{'value': 'HVD Kategorie wählen..', 'key': ''}]
+            options = [{'key': key, 'value': MAPPING[key]}
+                   for key in MAPPING]
+            result = default+options
+    except IOError as err:
+        log.error(
+            'Could not load mapping file from {}'
+            .format(mapping_file_path)
+        )
+        raise
+    except ValueError as err:
+        log.error(
+            'Could not convert mapping file from json. \nMapping file: {}'
+            .format(mapping_file_path)
+        )
+        raise
+    return result
 
 def odsh_load_mdk_sample_dataset():
     '''
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index e71e2322..705e794c 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -348,6 +348,8 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
           'get_resource_size': helpers_odsh.get_resource_size,
           'get_address_org':helpers_odsh.get_address_org,
           'get_body_mail':helpers_odsh.get_body_mail,
+          'odsh_load_applicable_legislations': helpers_odsh.odsh_load_applicable_legislations,
+          'odsh_load_hvd_categories': helpers_odsh.odsh_load_hvd_categories,
           'odsh_load_mdk_sample_dataset': helpers_odsh.odsh_load_mdk_sample_dataset,
           'odsh_load_raw_mdk_sample_dataset': helpers_odsh.odsh_load_raw_mdk_sample_dataset,
           'format_resource_format': helpers_odsh.format_resource_format,
-- 
GitLab