Skip to content

Commit

Permalink
[1669] Merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Feb 1, 2012
2 parents 995a388 + 64abd2f commit 9b2d5db
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 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
2 changes: 1 addition & 1 deletion ckan/templates/package/new_package_form.html
Expand Up @@ -197,7 +197,7 @@ <h3>Tags</h3>
<dd>
<p>Do you really want to change the state of this dataset? &nbsp;&nbsp;<button class="dataset-delete pretty-button">Yes!</button></p>
This dataset is&nbsp;&nbsp;
<select class="dataset-delete" id="state" name="state" style="display:inline;">
<select id="state" class="dataset-delete" name="state" style="display:inline;">
<option py:attrs="{'selected': 'selected' if data.get('state') == 'active' else None}" value="active">active</option>
<option py:attrs="{'selected': 'selected' if data.get('state') == 'deleted' else None}" value="deleted">deleted</option>
</select>
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
10 changes: 5 additions & 5 deletions ckan/tests/functional/test_user.py
Expand Up @@ -70,11 +70,11 @@ 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/'
res = self.app.get(offset, status=[200,302], extra_environ={'REMOTE_USER': str(user.name)})
# main_res = self.main_div(res)
# assert 'annafan' in res.body, res.body
# assert 'My Account' in res.body, res.body
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
assert 'My Account' in main_res, main_res

def test_user_read_logged_in(self):
user = model.User.by_name(u'annafan')
Expand Down
29 changes: 29 additions & 0 deletions ckan/tests/logic/test_action.py
Expand Up @@ -1192,6 +1192,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 @@ -1267,3 +1271,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 9b2d5db

Please sign in to comment.