From 664e2d3a84017ac4b6a42868d8891d9fde87c384 Mon Sep 17 00:00:00 2001
From: Benjamin Becker <benjamin.becker@dataport.de>
Date: Mon, 22 Jul 2019 08:20:24 +0000
Subject: [PATCH] tries to fix validation of subject for harvesters

---
 ckanext/odsh/plugin.py     |  1 -
 ckanext/odsh/validation.py | 22 +++++++++++-----------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py
index 6e514cc9..5356bcbe 100644
--- a/ckanext/odsh/plugin.py
+++ b/ckanext/odsh/plugin.py
@@ -421,7 +421,6 @@ class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm
             'key': [
                 toolkit.get_converter('known_spatial_uri'),
                 toolkit.get_converter('validate_licenseAttributionByText'),
-                toolkit.get_converter('tpsh_convert_subjectID_to_subjectText'),
             ]
         })
         schema.update(
diff --git a/ckanext/odsh/validation.py b/ckanext/odsh/validation.py
index 1cddc561..c9d41a2d 100644
--- a/ckanext/odsh/validation.py
+++ b/ckanext/odsh/validation.py
@@ -268,8 +268,7 @@ def tag_string_convert(key, data, errors, context):
         tag_name_validator(tag, context)
 
 
-def convert_subjectID_to_subjectText(key, data, errors, context):
-    subject_id = _extract_value(data, 'subject')
+def _convert_subjectID_to_subjectText(subject_id, flattened_data):
     default_subject_mapping_file_path = '/usr/lib/ckan/default/src/ckanext-odsh/subject_mapping.json'
     subject_mapping_file_path = config.get(
         'ckanext.odsh.subject_mapping_file_path', default_subject_mapping_file_path)
@@ -299,21 +298,23 @@ def convert_subjectID_to_subjectText(key, data, errors, context):
         )
         subject_text = ""
 
-    new_index = next_extra_index(data)
-    data[('extras', new_index, 'key')] = 'subject_text'
-    data[('extras', new_index, 'value')] = subject_text
+    new_index = next_extra_index(flattened_data)
+    flattened_data[('extras', new_index, 'key')] = 'subject_text'
+    flattened_data[('extras', new_index, 'value')] = subject_text
+    return flattened_data
 
 
-def validate_subject(value):
+def validate_subject(key, flattened_data, errors, context):
+    subject_id = flattened_data[key]
     require_subject = toolkit.asbool(
         config.get('ckanext.odsh.require_subject', True)
     )
     if not require_subject:
-        return value
-    if not value:
+        flattened_data = _convert_subjectID_to_subjectText(subject_id, flattened_data)
+        return
+    if not subject_id:
         raise toolkit.Invalid(_('Subject must not be empty.'))
-    return value
-
+    flattened_data = _convert_subjectID_to_subjectText(subject_id, flattened_data)
 
 
 def get_validators():
@@ -323,5 +324,4 @@ def get_validators():
         'odsh_validate_extras': validate_extras,
         'validate_licenseAttributionByText': validate_licenseAttributionByText,
         'tpsh_validate_subject': validate_subject,
-        'tpsh_convert_subjectID_to_subjectText': convert_subjectID_to_subjectText,
     }
-- 
GitLab