Skip to content

Commit

Permalink
Merge pull request #59 from fjelltopp/ADX-738-api-uploads
Browse files Browse the repository at this point in the history
ADX-738 api uploads
  • Loading branch information
jonathansberry authored Sep 2, 2021
2 parents b661172 + e3fe977 commit ae0f20d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ckanext/scheming/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def get_validators(self):
'autogenerate': unaids_validators.autogenerate,
'unique_combination': unaids_validators.unique_combination,
'auto_create_valid_name': unaids_validators.auto_create_valid_name,
'scheming_shapefile': unaids_validators.scheming_shapefile
'scheming_shapefile': unaids_validators.scheming_shapefile,
'autofill': unaids_validators.autofill
}

@run_once_for_caller('_scheming_add_template_directory', lambda: None)
Expand Down
3 changes: 2 additions & 1 deletion ckanext/scheming/presets.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@
{
"preset_name": "hidden_value",
"values": {
"form_snippet": "hidden_fields.html"
"form_snippet": "hidden_fields.html",
"validators": "autofill"
}
},
{
Expand Down
44 changes: 44 additions & 0 deletions ckanext/scheming/tests/schemas/autofill_validator.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"scheming_version": 1,
"dataset_type": "autofill-validator",
"name": "Auto create valid name",
"link_name": "Auto create valid name",
"about": "",
"about_url": "http://github.com/ckan/ckanext-scheming",
"add_data_on_create": false,
"dataset_fields": [{
"field_name": "title",
"label": "Title:",
"form_snippet": "text.html",
"classes": ["hidden"]
},
{
"field_name": "name",
"label": "URL",
"form_snippet": "text.html",
"validators": "auto_create_valid_name not_empty unicode name_validator package_name_validator",
"classes": ["hidden"]
},
{
"label": "Location",
"field_name": "location"
},
{
"label": "Year",
"field_name": "year",
"validators": "autofill"
},
{
"label": "Schema",
"field_name": "schema",
"field_value": "art_3",
"validators": "autofill"
},
{
"label": "Type",
"field_name": "dataset_type",
"default": "data-type",
"validators": "autofill"
}
]
}
23 changes: 23 additions & 0 deletions ckanext/scheming/tests/test_unaids_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,26 @@ def test_handles_deleted_datasets(self):
type="auto-create-valid-name", year="2020", location="north-pole"
)
assert dataset3['name'] == u'north-pole-autogenerate-2020-1'


@pytest.mark.usefixtures(u'clean_db')
@pytest.mark.usefixtures(u'clean_index')
class TestAutofill(object):
def test_autofilling(self):
lc = LocalCKAN()
dataset = lc.action.package_create(
type="autofill-validator", title="AutoFill", name='autofill', location="north-pole"
)
assert dataset['schema'] == u'art_3'
assert dataset['dataset_type'] == u'data-type'
assert dataset['year'] == u''

def test_not_overwriting(self):
lc = LocalCKAN()
dataset = lc.action.package_create(
type="autofill-validator", title="AutoFill", name='autofill', location="north-pole",
schema="art_4", dataset_type="different-data-type", year="1984"
)
assert dataset['schema'] == u'art_4'
assert dataset['dataset_type'] == u'different-data-type'
assert dataset['year'] == u'1984'
11 changes: 11 additions & 0 deletions ckanext/scheming/unaids_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ def validator(key, data, errors, context):
return validator


@scheming_validator
def autofill(field, schema):
field_value = field.get(u'field_value', field.get('default', ''))

def validator(key, data, errors, context):
if not data.get(key):
data[key] = field_value

return validator


def __lower_formatter(input):
return input.lower()

Expand Down
1 change: 1 addition & 0 deletions test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ scheming.dataset_schemas = ckanext.scheming:ckan_dataset.json
ckanext.scheming.tests:test_schema.json
ckanext.scheming.tests:test_datastore_choices.json
ckanext.scheming.tests.schemas:auto_unique_validator.json
ckanext.scheming.tests.schemas:autofill_validator.json
scheming.organization_schemas = ckanext.scheming:org_with_dept_id.json
ckanext.scheming:custom_org_with_address.json
scheming.group_schemas = ckanext.scheming:group_with_bookface.json
Expand Down

0 comments on commit ae0f20d

Please sign in to comment.