Skip to content

Commit

Permalink
HDX-10030
Browse files Browse the repository at this point in the history
  • Loading branch information
danmihaila committed Aug 2, 2024
1 parent e0a5982 commit 820bbdf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 13 additions & 1 deletion ckanext-hdx_package/ckanext/hdx_package/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


],
}
)

Expand Down Expand Up @@ -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'),
],
}
)

Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit 820bbdf

Please sign in to comment.