From c0c8f8039c9a2bb9211a49c800efc79dcea281bb Mon Sep 17 00:00:00 2001
From: Daniel Neuwirth <daniel.neuwirth@dataport.de>
Date: Wed, 30 Oct 2019 13:24:19 +0000
Subject: [PATCH] some bugfixes because of resources.pop() and added the config
 file editor

---
 .../odsh/harvest_templates/source/search.html |  2 +-
 ckanext/odsh/pdf_to_thumbnail/thumbnail.py    | 11 ++---
 ckanext/odsh/plugin.py                        |  1 +
 ckanext/odsh/tools.py                         | 41 +++++++++++++++----
 subject_mapping.json                          |  1 +
 5 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/ckanext/odsh/harvest_templates/source/search.html b/ckanext/odsh/harvest_templates/source/search.html
index 699fb0e4..6d1cb2d8 100644
--- a/ckanext/odsh/harvest_templates/source/search.html
+++ b/ckanext/odsh/harvest_templates/source/search.html
@@ -72,7 +72,7 @@
                   <div class="error-body"><h2>Seite nicht gefunden</h2>
                 <h3>Wie finde ich die gesuchten Inhalte im Landesportal?</h3>
 
-                <p><a class="" href="http://www.schleswig-holstein.de/odpstart" title="Zur Startseite">Zur Startseite des Open-Data-Portals</a></p>
+                <p><a class="" href="http://www.schleswig-holstein.de/trpstart" title="Zur Startseite">Zur Startseite des Transparenz-Portals</a></p>
 
                 <h3>Kontakt</h3>
                 <p>Bei Fragen oder Problemen mit dem Open-Data-Portal schicken Sie bitte eine E-Mail an die Adresse opendata@lr.landsh.de oder verwenden das Kontaktformular:</p>
diff --git a/ckanext/odsh/pdf_to_thumbnail/thumbnail.py b/ckanext/odsh/pdf_to_thumbnail/thumbnail.py
index 904e3170..f488252c 100644
--- a/ckanext/odsh/pdf_to_thumbnail/thumbnail.py
+++ b/ckanext/odsh/pdf_to_thumbnail/thumbnail.py
@@ -173,11 +173,12 @@ def update_last_resource_if_value_empty(context, resource, key, value):
     package_id = resource.get('package_id')
     package = toolkit.get_action('package_show')(context, {'id': package_id})
     resources = package.get('resources')
-    last_resource = resources.pop()
-    if not last_resource.get(key):
-        last_resource.update({key: value})
-    resources.append(last_resource)
-    package.update({'resources': resources})
+    if len(resources) > 0:
+        last_resource = resources.pop()
+        if not last_resource.get(key):
+            last_resource.update({key: value})
+        resources.append(last_resource)
+        package.update({'resources': resources})
     return package
 
 
diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index 54c71465..3fe483e3 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -202,6 +202,7 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
     def after_create(self, context, resource):
         if resource.get('package_id'):
             tools.save_hash_into_resource(context, resource)
+            tools.update_files_with_uploaded_files(context, resource)
 
     @staticmethod
     def _update_is_new_in_pkg_dict(pkg_dict):
diff --git a/ckanext/odsh/tools.py b/ckanext/odsh/tools.py
index 22b999b3..1831d6a3 100644
--- a/ckanext/odsh/tools.py
+++ b/ckanext/odsh/tools.py
@@ -2,17 +2,44 @@ import os
 from ckanext.odsh.pdf_to_thumbnail.thumbnail import get_filepath_to_resource, update_last_resource_if_value_empty
 from ckanext.odsh.lib.uploader import calculate_hash
 import ckan.plugins.toolkit as toolkit
+import ckan.authz as authz
+from ckan.common import config
+from shutil import copyfile
 
 def save_hash_into_resource(context, resource):
+    
     path = get_filepath_to_resource(resource)
     if os.path.exists(path):
         with open(path, 'rb') as file:
             hash = calculate_hash(file)
             package = update_last_resource_if_value_empty(context, resource, 'hash', hash)
-            resources = package.get('resources')
-            last_resource = resources.pop()    
-            if last_resource.get(hash) and not last_resource.get('hash_algorithm'): 
-                last_resource.update({'hash_algorithm': 'http://dcat-ap.de/def/hashAlgorithms/md/5'})
-                resources.append(last_resource)
-                package.update({'resources':resources})
-        toolkit.get_action('package_update')(context, package)
+    else:
+        package_id = resource.get('package_id')
+        package = toolkit.get_action('package_show')(context, {'id': package_id})
+
+    resources = package.get('resources')
+    last_resource = resources.pop() 
+    if last_resource.get(hash) and not last_resource.get('hash_algorithm'): 
+        last_resource.update({'hash_algorithm': 'http://dcat-ap.de/def/hashAlgorithms/md/5'})            
+    resources.append(last_resource)
+    package.update({'resources':resources})
+    toolkit.get_action('package_update')(context, package)
+
+
+def update_files_with_uploaded_files(context, resource):
+    package_id = resource.get('package_id')
+    package = toolkit.get_action('package_show')(context, {'id': package_id})
+    package_name = package.get('name')
+    user = context.get('user')
+    if authz.is_sysadmin(user) and package_name == "list_of_files_for_tpsh7":
+        origin_path = get_filepath_to_resource(resource)
+        if resource.get('name') == "subject_mapping.json":
+            target_path = config.get('ckanext.odsh.subject_mapping')
+        if resource.get('name') == "language_mapping.json":
+            target_path = config.get('ckanext.odsh.language_mapping')
+        if resource.get('name') == "spatial_mapping.csv":
+            target_path = config.get('ckanext.odsh.spatial.mapping')
+        if resource.get('name') == "licenses.json":
+            target_path = config.get('licenses_group_url')
+        if os.path.exists(origin_path) and target_path:
+                copyfile(origin_path, target_path)
\ No newline at end of file
diff --git a/subject_mapping.json b/subject_mapping.json
index 820b1eb3..1803e735 100644
--- a/subject_mapping.json
+++ b/subject_mapping.json
@@ -12,4 +12,5 @@
     "http://d-nb.info/gnd/4066490-9":"Wirtschaftspläne", 
     "http://d-nb.info/gnd/4128022-2":"Tätigkeitsbericht",
     "http://d-nb.info/gnd/4138783-1":"Gerichtsurteil"
+
 }
-- 
GitLab