From dd2c0c677117f06a52aa22b3b2717bb605263570 Mon Sep 17 00:00:00 2001 From: kindly Date: Tue, 31 Jan 2012 21:42:58 +0000 Subject: [PATCH 1/2] [#1738] before indes extension made --- ckan/lib/search/index.py | 7 +++++++ ckan/logic/action/create.py | 1 + ckan/plugins/interfaces.py | 10 +++++++++ ckan/tests/functional/test_package.py | 4 ++++ ckan/tests/logic/test_action.py | 29 +++++++++++++++++++++++++++ 5 files changed, 51 insertions(+) diff --git a/ckan/lib/search/index.py b/ckan/lib/search/index.py index 2a538f3e55d..9e80f2e2347 100644 --- a/ckan/lib/search/index.py +++ b/ckan/lib/search/index.py @@ -7,6 +7,8 @@ from common import SearchIndexError, make_connection from ckan.model import PackageRelationship +from ckan.plugins import (PluginImplementations, + IPackageController) log = logging.getLogger(__name__) @@ -138,6 +140,11 @@ def index_package(self, pkg_dict): import hashlib pkg_dict['index_id'] = hashlib.md5('%s%s' % (pkg_dict['id'],config.get('ckan.site_id'))).hexdigest() + for item in PluginImplementations(IPackageController): + pkg_dict = item.before_index(pkg_dict) + + assert pkg_dict, 'Plugin must return non empty package dict on index' + # send to solr: try: conn.add_many([pkg_dict]) diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index cb0417a4525..2628a45137a 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -66,6 +66,7 @@ def package_create(context, data_dict): model.setup_default_user_roles(pkg, admins) # Needed to let extensions know the package id model.Session.flush() + for item in PluginImplementations(IPackageController): item.create(pkg) diff --git a/ckan/plugins/interfaces.py b/ckan/plugins/interfaces.py index 94c7d693e45..d18452f599f 100644 --- a/ckan/plugins/interfaces.py +++ b/ckan/plugins/interfaces.py @@ -262,6 +262,16 @@ def after_search(self, search_results, search_params): return search_results + def before_index(self, pkg_dict): + ''' + Extensions will recieve what will be given to the solr for indexing. + This is essentially a flattened dict (except for multlivlaued fields such as tags + of all the terms sent to the indexer. The extension can modify this by returning + an altered version. + ''' + return pkg_dict + + class IPluginObserver(Interface): """ Plugin to the plugin loading mechanism diff --git a/ckan/tests/functional/test_package.py b/ckan/tests/functional/test_package.py index cc075f97743..81c67d93a2d 100644 --- a/ckan/tests/functional/test_package.py +++ b/ckan/tests/functional/test_package.py @@ -57,6 +57,10 @@ def after_search(self, search_results, search_params): self.calls['after_search'] += 1 return search_results + def before_index(self, search_params): + self.calls['before_index'] += 1 + return search_params + existing_extra_html = ('', '') diff --git a/ckan/tests/logic/test_action.py b/ckan/tests/logic/test_action.py index 70881ef6298..37ed555beeb 100644 --- a/ckan/tests/logic/test_action.py +++ b/ckan/tests/logic/test_action.py @@ -1193,6 +1193,10 @@ def test_4_sort_by_metadata_modified(self): class MockPackageSearchPlugin(SingletonPlugin): implements(IPackageController, inherit=True) + def before_index(self, data_dict): + data_dict['extras_test'] = 'abcabcabc' + return data_dict + def before_search(self, search_params): if 'extras' in search_params and 'ext_avoid' in search_params['extras']: assert 'q' in search_params @@ -1268,3 +1272,28 @@ def test_search_plugin_interface_abort(self): assert res_dict['count'] == 0 assert len(res_dict['results']) == 0 plugins.unload(plugin) + + def test_before_index(self): + plugin = MockPackageSearchPlugin() + plugins.load(plugin) + # no datasets get aaaaaaaa + search_params = '%s=1' % json.dumps({ + 'q': 'aaaaaaaa', + }) + + res = self.app.post('/api/action/package_search', params=search_params) + + res_dict = json.loads(res.body)['result'] + assert res_dict['count'] == 0 + assert len(res_dict['results']) == 0 + plugins.unload(plugin) + + # all datasets should get abcabcabc + search_params = '%s=1' % json.dumps({ + 'q': 'abcabcabc', + }) + res = self.app.post('/api/action/package_search', params=search_params) + + res_dict = json.loads(res.body)['result'] + assert res_dict['count'] == 2 + assert len(res_dict['results']) == 2 From 117dce4d64de731e7b0a3c55175a1d093f2bf540 Mon Sep 17 00:00:00 2001 From: kindly Date: Tue, 31 Jan 2012 22:00:26 +0000 Subject: [PATCH 2/2] [#1739] fix errors due to tests not being run --- ckan/templates/package/new_package_form.html | 2 +- ckan/tests/functional/test_user.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ckan/templates/package/new_package_form.html b/ckan/templates/package/new_package_form.html index 788e590162c..80ce7c217a1 100644 --- a/ckan/templates/package/new_package_form.html +++ b/ckan/templates/package/new_package_form.html @@ -197,7 +197,7 @@

Tags

Do you really want to change the state of this dataset?   

This dataset is   - diff --git a/ckan/tests/functional/test_user.py b/ckan/tests/functional/test_user.py index 123947e1104..915a40bc643 100644 --- a/ckan/tests/functional/test_user.py +++ b/ckan/tests/functional/test_user.py @@ -70,7 +70,7 @@ def test_user_read_me_without_id(self): def test_user_read_without_id_but_logged_in(self): user = model.User.by_name(u'annafan') - offset = '/user/' + offset = '/user/annafan' res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': str(user.name)}) main_res = self.main_div(res) assert 'annafan' in main_res, main_res