Skip to content

Commit

Permalink
Merge branch 'feature-1738-add-before-index-extension'
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Jan 31, 2012
2 parents 117dce4 + dd2c0c6 commit 64abd2f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ckan/lib/search/index.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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])
Expand Down
1 change: 1 addition & 0 deletions ckan/logic/action/create.py
Expand Up @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions ckan/plugins/interfaces.py
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions ckan/tests/functional/test_package.py
Expand Up @@ -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 = ('<label class="field_opt" for="Package-%(package_id)s-extras-%(key)s">%(capitalized_key)s</label>', '<input id="Package-%(package_id)s-extras-%(key)s" name="Package-%(package_id)s-extras-%(key)s" size="20" type="text" value="%(value)s">')

Expand Down
29 changes: 29 additions & 0 deletions ckan/tests/logic/test_action.py
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 64abd2f

Please sign in to comment.