Skip to content

Commit

Permalink
Add validation support for resource_update
Browse files Browse the repository at this point in the history
  • Loading branch information
fulior authored and ChasNelson1990 committed Jun 30, 2023
1 parent 9f7385b commit ac5c1aa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ckanext/unaids/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import ckan.lib.dictization.model_save as model_save
import ckan.lib.dictization.model_dictize as model_dictize
import ckan.plugins.toolkit as t
import ckanext.validation.helpers as validation_helpers
import ckanext.unaids.custom_user_profile as custom_user_profile
from ckan.common import _
from ckanext.versions.logic.dataset_version_action import get_activity_id_from_dataset_version_name, activity_dataset_show
from ckanext.unaids.logic import populate_data_dictionary_from_schema
from ckanext.unaids.helpers import validation_load_json_schema

NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized
Expand Down Expand Up @@ -41,7 +41,7 @@ def get_table_schema(context, data_dict):
schema_name = resource.get('schema')
schema = False
if schema_name:
schema = validation_helpers.validation_load_json_schema(schema_name)
schema = validation_load_json_schema(schema_name)
if not schema:
schema = {}
return schema
Expand Down
5 changes: 5 additions & 0 deletions ckanext/unaids/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def get_schema_filepath(schema):

def validation_load_json_schema(schema):
try:
# When updating a resource there's already an existing JSON schema
# attached to the resource
if type(schema) == dict:
return schema

if schema.startswith("http"):
r = requests.get(schema)
return r.json()
Expand Down
7 changes: 4 additions & 3 deletions ckanext/unaids/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ def _process_schema_fields(self, data_dict):
to allow for the fact that we just pass a schema name around rather than the schema url
or a schema JSON.
"""

schema = data_dict.pop("schema", None)
if schema:
schema_json = logic.validation_load_json_schema(schema)
data_dict[u'schema'] = schema_json
data_dict[u'schema_json'] = schema_json

return data_dict

Expand All @@ -212,7 +212,8 @@ def before_update(self, context, current, resource):
_giftless_upload(context, resource, current=current)
_update_resource_last_modified_date(resource, current=current)
logic.validate_resource_upload_fields(context, resource)
return resource
context["_resource_create_call"] = True
return self._process_schema_fields(resource)

def before_show(self, resource):
if _data_dict_is_resource(resource):
Expand Down

0 comments on commit ac5c1aa

Please sign in to comment.