[ckan-changes] commit/ckan: 2 new changesets

Bitbucket commits-noreply at bitbucket.org
Tue Aug 23 17:22:09 UTC 2011


2 new changesets in ckan:

http://bitbucket.org/okfn/ckan/changeset/b3bdaec7f662/
changeset:   b3bdaec7f662
branch:      feature-1275-solr-search
user:        John Glover
date:        2011-08-23 18:59:20
summary:     [solr] [1154] Disable search/indexing if cannot connection to Solr
affected #:  6 files (1.1 KB)

--- a/ckan/config/environment.py	Tue Aug 23 15:20:50 2011 +0100
+++ b/ckan/config/environment.py	Tue Aug 23 17:59:20 2011 +0100
@@ -104,6 +104,14 @@
     # CONFIGURATION OPTIONS HERE (note: all config options will override
     # any Pylons config options)    
 
+    # check that search is available, disable if not
+    import ckan.lib.search as search
+    if not config.get('search_enabled', '') == 'False':
+        config['search_enabled'] = search.is_available()
+    else:
+        # save this as a boolean rather than a string
+        config['search_enabled'] = False
+
     # Setup the SQLAlchemy database engine
     engine = engine_from_config(config, 'sqlalchemy.')
 


--- a/ckan/lib/search/__init__.py	Tue Aug 23 15:20:50 2011 +0100
+++ b/ckan/lib/search/__init__.py	Tue Aug 23 17:59:20 2011 +0100
@@ -3,7 +3,7 @@
 from ckan.model import DomainObjectOperation
 from ckan.plugins import SingletonPlugin, implements, IDomainObjectModification
 from ckan.lib.dictization.model_dictize import package_to_api1
-from common import SearchError
+from common import SearchError, make_connection, is_available
 from index import PackageSearchIndex, NoopSearchIndex
 from query import TagSearchQuery, ResourceSearchQuery, PackageSearchQuery, QueryOptions
 
@@ -21,7 +21,7 @@
     'all_fields': False,
     'search_tags': True,
     'callback': None, # simply passed through
-    }
+}
 
 _INDICES = {
     'package': PackageSearchIndex
@@ -71,6 +71,10 @@
             log.warn("Unknown operation: %s" % operation)
     except Exception, ex:
         log.exception(ex)
+        # we really need to know about any exceptions, so reraise
+        # (see #1172)
+        raise
+        
 
 class SynchronousSearchPlugin(SingletonPlugin):
     """Update the search index automatically."""


--- a/ckan/lib/search/common.py	Tue Aug 23 15:20:50 2011 +0100
+++ b/ckan/lib/search/common.py	Tue Aug 23 17:59:20 2011 +0100
@@ -1,9 +1,30 @@
+from pylons import config
 from solr import SolrConnection
 import logging
 log = logging.getLogger(__name__)
 
 class SearchError(Exception): pass
 
+def is_available():
+    """
+    Return true if we can successfully connect to Solr.
+    """
+    try:
+        conn = make_connection(config)
+        conn.query("*:*", rows=1)
+        conn.close()
+        return True
+    except Exception, e:
+        log.error("Solr not available, disabling package search.")
+        log.exception(e)
+        return False
+
+def is_enabled():
+    """
+    Return true if search is enabled in ckan config.
+    """
+    return config.get('search_enabled')
+
 def make_connection(config):
     url = config.get('solr_url', 'http://localhost:8983/solr')
     user = config.get('solr_user')


--- a/ckan/lib/search/index.py	Tue Aug 23 15:20:50 2011 +0100
+++ b/ckan/lib/search/index.py	Tue Aug 23 17:59:20 2011 +0100
@@ -1,7 +1,7 @@
 from pylons import config
 import itertools
 import string
-from common import make_connection
+from common import is_enabled, make_connection
 import logging
 log = logging.getLogger(__name__)
 
@@ -82,7 +82,7 @@
         self.index_package(pkg_dict, config)
 
     def index_package(self, pkg_dict, config):
-        if pkg_dict is None:  
+        if (not is_enabled()) or (pkg_dict is None):  
             return 
         if (not pkg_dict.get('state')) or ('active' not in pkg_dict.get('state')):
             return self.delete_package(pkg_dict, config)
@@ -140,6 +140,9 @@
         log.debug("Updated index for %s" % pkg_dict.get('name'))
 
     def delete_package(self, pkg_dict, config):
+        if not is_enabled():
+            return
+
         conn = make_connection(config)
         query = "+%s:%s +id:\"%s\" +site_id:\"%s\"" % (TYPE_FIELD, PACKAGE_TYPE,
                                                        pkg_dict.get('id'),


--- a/ckan/lib/search/query.py	Tue Aug 23 15:20:50 2011 +0100
+++ b/ckan/lib/search/query.py	Tue Aug 23 17:59:20 2011 +0100
@@ -4,7 +4,7 @@
 from paste.deploy.converters import asbool
 from ckan import model
 from ckan.authz import Authorizer
-from common import make_connection, SearchError
+from common import is_enabled, make_connection, SearchError
 import logging
 log = logging.getLogger(__name__)
 
@@ -267,6 +267,9 @@
 
 class PackageSearchQuery(SearchQuery):
     def _run(self):
+        if not is_enabled():
+            return
+
         fq = ""
 
         # Filter for options


--- a/ckan/tests/lib/test_solr_search_index.py	Tue Aug 23 15:20:50 2011 +0100
+++ b/ckan/tests/lib/test_solr_search_index.py	Tue Aug 23 17:59:20 2011 +0100
@@ -10,8 +10,10 @@
     """
     def test_solr_url_exists(self):
         assert config.get('solr_url')
-        # solr.SolrConnection will throw an exception if it can't connect
-        solr.SolrConnection(config.get('solr_url'))
+        # solr.SolrConnection.query will throw an exception if it can't connect
+        conn = solr.SolrConnection(config.get('solr_url'))
+        q = conn.query("*:*", rows=1)
+        conn.close()
 
 
 class TestSolrSearchIndex(TestController):


http://bitbucket.org/okfn/ckan/changeset/14c1b88a0f72/
changeset:   14c1b88a0f72
branch:      feature-1275-solr-search
user:        John Glover
date:        2011-08-23 19:19:44
summary:     [solr] [1154] Hide search links/forms in templates if search is not enabled
affected #:  4 files (191 bytes)

--- a/ckan/config/environment.py	Tue Aug 23 17:59:20 2011 +0100
+++ b/ckan/config/environment.py	Tue Aug 23 18:19:44 2011 +0100
@@ -72,6 +72,14 @@
         if ':' in ckan_host:
             ckan_host, port = ckan_host.split(':')
         config['ckan.site_id'] = ckan_host
+
+    # check that search is available, disable if not
+    import ckan.lib.search as search
+    if not config.get('search_enabled', '') == 'False':
+        config['search_enabled'] = search.is_available()
+    else:
+        # save this as a boolean rather than a string
+        config['search_enabled'] = False
     
     config['routes.map'] = make_map()
     config['pylons.app_globals'] = app_globals.Globals()
@@ -104,14 +112,6 @@
     # CONFIGURATION OPTIONS HERE (note: all config options will override
     # any Pylons config options)    
 
-    # check that search is available, disable if not
-    import ckan.lib.search as search
-    if not config.get('search_enabled', '') == 'False':
-        config['search_enabled'] = search.is_available()
-    else:
-        # save this as a boolean rather than a string
-        config['search_enabled'] = False
-
     # Setup the SQLAlchemy database engine
     engine = engine_from_config(config, 'sqlalchemy.')
 


--- a/ckan/lib/app_globals.py	Tue Aug 23 17:59:20 2011 +0100
+++ b/ckan/lib/app_globals.py	Tue Aug 23 18:19:44 2011 +0100
@@ -29,6 +29,8 @@
         self.site_id = config.get('ckan.site_id')
 
         self.template_footer_end = config.get('ckan.template_footer_end', '')
+
+        self.search_enabled = config.get('search_enabled', True)
         
         # hide these extras fields on package read
         self.package_hide_extras = config.get('package_hide_extras', '').split()


--- a/ckan/templates/home/index.html	Tue Aug 23 17:59:20 2011 +0100
+++ b/ckan/templates/home/index.html	Tue Aug 23 18:19:44 2011 +0100
@@ -48,9 +48,11 @@
       especially in ways that are machine automatable.
     </p>
     
-    <p i18n:msg="package_count"><strong>${c.package_count} registered data packages</strong> available.</p>
+    <div py:if="g.search_enabled">
+      <p i18n:msg="package_count"><strong>${c.package_count} registered data packages</strong> available.</p>
 
-    <xi:include href="../package/search_form.html" />
+      <xi:include href="../package/search_form.html" />
+    </div><py:if test="len(c.facets.get('tags', {}))"><h4>Top Tags</h4>


--- a/ckan/templates/layout_base.html	Tue Aug 23 17:59:20 2011 +0100
+++ b/ckan/templates/layout_base.html	Tue Aug 23 18:19:44 2011 +0100
@@ -83,7 +83,7 @@
         </span></div>
 
-      <div class="search-form">
+      <div class="search-form" py:if="g.search_enabled"><form action="${url(controller='package', action='search')}" method="GET"><input type="search" class="search" name="q" value="" autocomplete="off" results="5" placeholder="Search..."/></form>
@@ -97,7 +97,7 @@
       <div class="menu"><ul><li>${h.nav_link(c, _('Home'), controller='home', action='index', id=None)}</li>
-          <li>${h.nav_link(c, _('Search'), controller='package', action='index', id=None, highlight_actions = 'search index')}</li>
+          <li py:if="g.search_enabled">${h.nav_link(c, _('Search'), controller='package', action='index', id=None, highlight_actions = 'search index')}</li><?python
 access_package_create = h.check_access('package_create')
 ?>
@@ -167,7 +167,7 @@
           <h3 class="widget-title">Packages</h3><div class="textwidget"><ul>
-                <li>${h.nav_link(c, _('Search'), controller='package', action='search', id=None)}</li>                
+                <li py:if="g.search_enabled">${h.nav_link(c, _('Search'), controller='package', action='search', id=None)}</li><li py:if="access_package_create">${h.nav_link(c, _('Register a new Package'), controller='package', action='new', id=None)}</li><li>${h.nav_link(c, _('Revision History'), controller='revision', action='index', id=None)}</li><li>${h.link_to(_('API'), h.url_for(controller='api', action='get_api', id=None))}</li>

Repository URL: https://bitbucket.org/okfn/ckan/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.




More information about the ckan-changes mailing list