From 820bbdfb077ee9570b7fdea03c29cba86e8964df Mon Sep 17 00:00:00 2001 From: Dan Mihaila Date: Fri, 2 Aug 2024 16:55:08 +0300 Subject: [PATCH] HDX-10030 --- .../hdx_package/helpers/custom_validator.py | 25 +++++++++++++++++++ .../ckanext/hdx_package/plugin.py | 14 ++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/ckanext-hdx_package/ckanext/hdx_package/helpers/custom_validator.py b/ckanext-hdx_package/ckanext/hdx_package/helpers/custom_validator.py index efe14cdf2f..48890ad1f4 100644 --- a/ckanext-hdx_package/ckanext/hdx_package/helpers/custom_validator.py +++ b/ckanext-hdx_package/ckanext/hdx_package/helpers/custom_validator.py @@ -397,6 +397,31 @@ def hdx_resource_keep_prev_value_unless_sysadmin(key, data, errors, context): if key not in data: raise StopOnError +def hdx_resource_keep_prev_value_if_exist_unless_sysadmin(key, data, errors, context): + ''' + By default, this should inject the value from the previous version. + The exception is if the user is a sysadmin, then the new value is used. + ''' + + user = context.get('user') + ignore_auth = context.get('ignore_auth') + allowed_to_change = ignore_auth or (user and authz.is_sysadmin(user)) + + if not allowed_to_change or data[key] is missing: + data.pop(key, None) + resource_id = data.get(key[:-1] + ('id',)) + package_id = data.get(('id',)) + if resource_id: + specific_key = key[2] + context_key = 'resource_' + resource_id + resource_dict = context.get(context_key) + if not resource_dict: + resource_dict = __get_previous_resource_dict(context, package_id, resource_id) + context[context_key] = resource_dict + if resource_dict: + old_value = resource_dict.get(specific_key) + if old_value is not None: + data[key] = old_value # def hdx_update_field_if_value_wrapper(context_field, value): # def hdx_update_field_if_value(key, data, errors, context): diff --git a/ckanext-hdx_package/ckanext/hdx_package/plugin.py b/ckanext-hdx_package/ckanext/hdx_package/plugin.py index 69a93e2577..88da120e2e 100644 --- a/ckanext-hdx_package/ckanext/hdx_package/plugin.py +++ b/ckanext-hdx_package/ckanext/hdx_package/plugin.py @@ -275,7 +275,15 @@ def _modify_package_schema(self, schema): tk.get_validator('hdx_delete_unless_authorized_to_update_p_coded'), tk.get_validator('ignore_missing'), # if None, don't save 'None' string tk.get_validator('boolean_validator'), - ] + ], + 'qa_hapi_report': [ + # tk.get_validator('ignore_missing'), # if None, don't save 'None' string + tk.get_validator('hdx_resource_keep_prev_value_if_exist_unless_sysadmin'), + tk.get_validator('hdx_reset_on_file_upload'), + tk.get_validator('ignore_missing'), # if None, don't save 'None' string + + + ], } ) @@ -335,6 +343,9 @@ def show_package_schema(self): tk.get_validator('ignore_missing'), tk.get_validator('boolean_validator'), ], + 'qa_hapi_report': [ + tk.get_validator('ignore_missing'), + ], } ) @@ -482,6 +493,7 @@ def get_validators(self): 'hdx_assume_missing_is_true': vd.hdx_assume_missing_is_true, 'hdx_isodate_to_string_converter': vd.hdx_isodate_to_string_converter, 'hdx_resource_keep_prev_value_unless_sysadmin': vd.hdx_resource_keep_prev_value_unless_sysadmin, + 'hdx_resource_keep_prev_value_if_exist_unless_sysadmin': vd.hdx_resource_keep_prev_value_if_exist_unless_sysadmin, 'hdx_reset_on_file_upload': vd.reset_on_file_upload, 'hdx_keep_prev_value_if_empty': vd.hdx_keep_prev_value_if_empty, 'hdx_delete_unless_allow_broken_link': vd.hdx_delete_unless_field_in_context('allow_broken_link_field'),