Skip to content

Instantly share code, notes, and snippets.

@davidread
Created March 27, 2012 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidread/2217684 to your computer and use it in GitHub Desktop.
Save davidread/2217684 to your computer and use it in GitHub Desktop.
More Like This in CKAN (DGU extension)
diff --git a/ckanext/dgu/plugin.py b/ckanext/dgu/plugin.py
index 1da26dc..ce1dfbf 100644
--- a/ckanext/dgu/plugin.py
+++ b/ckanext/dgu/plugin.py
@@ -13,6 +13,7 @@ from ckan.plugins import IMiddleware
from ckan.plugins import IAuthFunctions
from ckan.plugins import IPackageController
from ckan.plugins import ISession
+from ckan.plugins import IActions
from ckanext.dgu.middleware import AuthAPIMiddleware
from ckanext.dgu.auth import dgu_group_update, dgu_group_create, \
dgu_package_update, dgu_extra_fields_editable, \
@@ -198,6 +199,7 @@ class SearchPlugin(SingletonPlugin):
"""
implements(IPackageController)
+ implements(IActions)
def read(self, entity):
pass
@@ -342,3 +344,24 @@ class SearchPlugin(SingletonPlugin):
regex = re.compile(r'open government licen[sc]e', re.IGNORECASE)
return pkg_dict['license_id'] == 'uk-ogl' or \
bool(regex.search(pkg_dict.get('extras_access_constraints', '')))
+
+ def get_actions(self):
+ """
+ Should return a dict, the keys being the name of the logic
+ function and the values being the functions themselves.
+ """
+ def related_datasets(context, data_dict):
+ '''
+ Returns a list of datasets related to the one specified.
+ '''
+ from ckanext.dgu.lib.more_like_this import MoreLikeThisPackageQuery
+
+ model = context['model']
+ user = context['user']
+ dataset_name_or_id = data_dict.get('id') or data_dict.get('name') or data_dict['name_or_id']
+
+ results = MoreLikeThisPackageQuery().run(dataset_name_or_id)
+ return results
+
+ return {'related_datasets': related_datasets}
+
from pylons import config
import logging
from ckan.lib.search import make_connection
from ckan.lib.search.query import SearchQuery
from ckan.lib.helpers import json
log = logging.getLogger(__name__)
class MoreLikeThisPackageQuery(SearchQuery):
def run(self, dataset_ref, limit=5):
'''
Searches for datasets like the given one
@param dataset_ref - id or name for a dataset
@param limit - max number of results
@return - list of results, each one a dictionary containing
keys: name, id, title
May raise SearchQueryError or SearchError.
'''
from solr import SolrException
query = {
'rows': 1,
'q': 'name:%s OR id:%s' % (dataset_ref, dataset_ref),
'wt': 'json',
'fq': 'site_id:"%s"' % config.get('ckan.site_id'),
'fl': 'id, index_id',
'mlt': 'true',
'mlt.fl': 'name,title,tags,notes',
'mlt.mindf': 1,
'mlt.mintf': 1,
'mlt.count': limit,
}
conn = make_connection()
log.debug('More Like This query: %r' % query)
try:
solr_response = conn.raw_query(**query)
except SolrException, e:
raise SearchError('SOLR returned an error running query: %r Error: %r' %
(query, e.reason))
try:
data = json.loads(solr_response)
import pdb; pdb.set_trace()
response = data[u'moreLikeThis']
self.results = response.get('docs', [])
except Exception, e:
log.exception(e)
raise SearchError(e)
finally:
conn.close()
return self.results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment