Skip to content

Instantly share code, notes, and snippets.

@davidread
Created September 28, 2018 16:26
Show Gist options
  • Save davidread/389dd83ba4e1c311ee98dd194d69c764 to your computer and use it in GitHub Desktop.
Save davidread/389dd83ba4e1c311ee98dd194d69c764 to your computer and use it in GitHub Desktop.
validator
diff --git a/ckan/lib/navl/validators.py b/ckan/lib/navl/validators.py
index 172da1531..b041d478e 100644
--- a/ckan/lib/navl/validators.py
+++ b/ckan/lib/navl/validators.py
@@ -4,7 +4,7 @@ from six import text_type
import ckan.lib.navl.dictization_functions as df
-from ckan.common import _, json
+from ckan.common import _, json, config
missing = df.missing
StopOnError = df.StopOnError
@@ -163,3 +163,13 @@ def unicode_safe(value):
return text_type(value)
except Exception:
return u'\N{REPLACEMENT CHARACTER}'
+
+def limit_to_configured_maximum(config_option, default_limit):
+ def callable(key, data, errors, context):
+
+ value = data.get(key)
+ limit = config.get(config_option, default_limit)
+ if value > limit:
+ data[key] = limit
+
+ return callable
diff --git a/ckan/lib/search/query.py b/ckan/lib/search/query.py
index 599125bbe..6f61f3458 100644
--- a/ckan/lib/search/query.py
+++ b/ckan/lib/search/query.py
@@ -312,7 +312,7 @@ class PackageSearchQuery(SearchQuery):
query['q'] = "*:*"
# number of results
- rows_to_return = min(1000, int(query.get('rows', 10)))
+ rows_to_return = query['rows'] # defaulted & made an int by schema
if rows_to_return > 0:
# #1683 Work around problem of last result being out of order
# in SOLR 1.4
diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py
--- a/ckan/logic/schema.py
+++ b/ckan/logic/schema.py
@@ -627,12 +627,13 @@ def default_autocomplete_schema(
def default_package_search_schema(
ignore_missing, unicode_safe, list_of_strings,
natural_number_validator, int_validator, convert_to_json_if_string,
- convert_to_list_if_string):
+ convert_to_list_if_string, limit_to_configured_maximum, default):
return {
'q': [ignore_missing, unicode_safe],
'fl': [ignore_missing, convert_to_list_if_string],
'fq': [ignore_missing, unicode_safe],
- 'rows': [ignore_missing, natural_number_validator],
+ 'rows': [default(10), natural_number_validator,
+ limit_to_configured_maximum('ckan.search.rows_max', 1000)],
'sort': [ignore_missing, unicode_safe],
'start': [ignore_missing, natural_number_validator],
'qf': [ignore_missing, unicode_safe],
diff --git a/ckan/tests/logic/action/test_get.py b/ckan/tests/logic/action/test_get.py
index 70a85384a..69838752e 100644
--- a/ckan/tests/logic/action/test_get.py
+++ b/ckan/tests/logic/action/test_get.py
@@ -892,6 +892,25 @@ class TestPackageSearch(helpers.FunctionalTestBase):
# SOLR error is 'Missing sort order' or 'Missing_sort_order',
# depending on the solr version.
+ def _create_bulk_datasets(self, name, count):
+ from ckan import model
+ model.repo.new_revision()
+ pkgs = [model.Package(name='{}_{}'.format(name, i))
+ for i in range(count)]
+ model.Session.add_all(pkgs)
+ model.repo.commit_and_remove()
+
+ def test_rows_returned_default(self):
+ self._create_bulk_datasets('rows_default', 11)
+ results = logic.get_action('package_search')({}, {})
+ eq(len(results['results']), 10) # i.e. 'rows' default value
+
+ @helpers.change_config('ckan.search.rows_max', '12')
+ def test_rows_returned_limited(self):
+ self._create_bulk_datasets('rows_limited', 14)
+ results = logic.get_action('package_search')({}, {'rows': '15'})
+ eq(len(results['results']), 12) # i.e. ckan.search.rows_max
+
def test_facets(self):
org = factories.Organization(name='test-org-facet', title='Test Org')
factories.Dataset(owner_org=org['id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment