diff --git a/ckanext/odsh/controller.py b/ckanext/odsh/controller.py index d1a81c5db2743b6624504d009f9f99bcc7437d26..cfbea624a4a682c46f28bb522d6a575a6c5e56ba 100644 --- a/ckanext/odsh/controller.py +++ b/ckanext/odsh/controller.py @@ -15,10 +15,15 @@ from ckan.common import c, request, config import hashlib import ckan.plugins.toolkit as toolkit from ckanext.dcat.controllers import DCATController +from ckan.lib.search.common import ( + make_connection, SearchError, SearchQueryError +) +import pysolr abort = base.abort log = logging.getLogger(__name__) + class OdshRouteController(HomeController): def info_page(self): h.redirect_to('http://www.schleswig-holstein.de/odpinfo') @@ -27,6 +32,7 @@ class OdshRouteController(HomeController): def not_found(self): abort(404) + class OdshUserController(UserController): def me(self, locale=None): if not c.user: @@ -65,9 +71,11 @@ class OdshUserController(UserController): abort(404) return super(OdshUserController,self).activity(id, offset) + class OdshPackageController(PackageController): pass + class OdshApiController(ApiController): def action(self, logic_function, ver=None): try: @@ -92,12 +100,14 @@ class OdshApiController(ApiController): log.error(e) return ApiController.action(self, logic_function, ver) - + + class OdshDCATController(DCATController): def read_catalog(self, _format): matomo.create_matomo_request() return DCATController.read_catalog(self,_format) + class OdshFeedController(FeedController): def custom(self): matomo.create_matomo_request() @@ -155,3 +165,21 @@ class OdshFeedController(FeedController): feed_guid=_create_atom_id(atom_url), feed_url=feed_url, navigation_urls=navigation_urls) + + +class OdshAutocompleteController(ApiController): + def autocomplete(self, q): + query = { + 'spellcheck.q': q, + 'wt': 'json'} + + conn = make_connection(decode_dates=False) + log.debug('Suggest query: %r' % query) + try: + solr_response = conn.search('', search_handler='suggest', **query) + except pysolr.SolrError as e: + raise SearchError('SOLR returned an error running query: %r Error: %r' % + (query, e)) + + suggest = solr_response.raw_response.get('spellcheck') + return base.response.body_file.write(str(suggest)) diff --git a/ckanext/odsh/fanstatic/autocomplete.js b/ckanext/odsh/fanstatic/autocomplete.js new file mode 100644 index 0000000000000000000000000000000000000000..282b4844d0f3fe49af34f197abfcafdab8965049 --- /dev/null +++ b/ckanext/odsh/fanstatic/autocomplete.js @@ -0,0 +1,21 @@ +'use strict'; + +$(function () { + + // Activate search suggestions for the search bar in the header and for the + // search bar used in the body. + $('.site-search input, .search').autocomplete({ + delay: 500, + html: true, + minLength: 2, + source: function (request, response) { + var url = ckan.SITE_ROOT + '/autocomplete/' + request.term; + $.getJSON(url) + .done(function (data) { + console.log(data); + response(data['result']); + }); + } + }); + +}) diff --git a/ckanext/odsh/plugin.py b/ckanext/odsh/plugin.py index 67bce258c81375ca1a830608aa4f2362e98f2de8..bfa3e27f553f736e61687950601520be456ef604 100644 --- a/ckanext/odsh/plugin.py +++ b/ckanext/odsh/plugin.py @@ -76,12 +76,23 @@ def remove_route(map,routename): map._routenames.pop(route.name) break + class OdshIcapPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IUploader, inherit=True) def get_resource_uploader(self, data_dict): return ODSHResourceUpload(data_dict) + +class OdshAutocompletePlugin(plugins.SingletonPlugin): + plugins.implements(plugins.IRoutes, inherit=True) + + def before_map(self, map): + controller = 'ckanext.odsh.controller:OdshAutocompleteController' + map.connect('/autocomplete/{q}', controller=controller, action='autocomplete') + return map + + class OdshPlugin(plugins.SingletonPlugin, DefaultTranslation, DefaultDatasetForm): plugins.implements(plugins.IConfigurer) plugins.implements(plugins.ITemplateHelpers) diff --git a/ckanext/odsh/public/autocomplete.css b/ckanext/odsh/public/autocomplete.css new file mode 100644 index 0000000000000000000000000000000000000000..3ae7be7ccffad1a21d200cc9648a6a07cb42bc4d --- /dev/null +++ b/ckanext/odsh/public/autocomplete.css @@ -0,0 +1,8 @@ +.ui-autocomplete .ui-menu-item a, +.ui-autocomplete .ui-menu-item a:active, +.ui-autocomplete .ui-menu-item a:focus, +.ui-autocomplete .ui-menu-item a:hover, +.ui-autocomplete .ui-menu-item a:visited { + display: block; + text-decoration: none; +} diff --git a/ckanext/odsh/templates/base.html b/ckanext/odsh/templates/base.html index 5962c139dbd823b28c0289e0794ea1e724b245c9..a060e606c21670d86f8f1ac6192b0009b939a4bb 100644 --- a/ckanext/odsh/templates/base.html +++ b/ckanext/odsh/templates/base.html @@ -6,6 +6,8 @@ <link rel="stylesheet" href="/odsh.css?refresh={{ range(1,10000) | random }}" /> <link rel="stylesheet" href="/odsh_header.css?refresh={{ range(1,10000) | random }}" /> <link rel="stylesheet" href="/bootstrap-multiselect.css" /> +<link rel="stylesheet" href="/autocomplete.css" /> +{% resource 'odsh/autocomplete.js' %} {% endblock %} {% block head_extras %} diff --git a/setup.py b/setup.py index c22bfcb5c6bacabfacd49c55dd9c7336beecbba3..2ecc2219a828bd1e9fb3315d0cc0bdbcd5614915 100755 --- a/setup.py +++ b/setup.py @@ -84,6 +84,7 @@ setup( odsh_icap=ckanext.odsh.plugin:OdshIcapPlugin statistikamtnord_harvester=ckanext.odsh.harvesters:StatistikamtNordHarvester kiel_harvester=ckanext.odsh.harvesters:KielHarvester + odsh_autocomplete=ckanext.odsh.plugin:OdshAutocompletePlugin [paste.paster_command] odsh_initialization = ckanext.odsh.commands.initialization:Initialization