Skip to content

Commit

Permalink
Merge pull request #437 from okfn/437-lib-search-imports
Browse files Browse the repository at this point in the history
lib.search.__init__ messy imports
  • Loading branch information
vitorbaptista committed Feb 25, 2013
2 parents 1c00ca5 + e50860d commit 7e8f984
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions ckan/lib/search/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import logging
from pylons import config, c
import sys
import cgitb
import warnings
import xml.dom.minidom
import urllib2

from pylons import config
from paste.deploy.converters import asbool

from ckan import model
from ckan.plugins import SingletonPlugin, implements, IDomainObjectModification
from ckan.logic import get_action
import ckan.model.domain_object as domain_object
import ckan.model as model
import ckan.plugins as p
import ckan.logic as logic

from common import (SearchIndexError, SearchError, SearchQueryError,
make_connection, is_available, SolrSettings)
Expand All @@ -15,9 +20,6 @@

log = logging.getLogger(__name__)

import sys
import cgitb
import warnings


def text_traceback():
Expand Down Expand Up @@ -63,7 +65,7 @@ def text_traceback():


def _normalize_type(_type):
if isinstance(_type, domain_object.DomainObject):
if isinstance(_type, model.domain_object.DomainObject):
_type = _type.__class__
if isinstance(_type, type):
_type = _type.__name__
Expand Down Expand Up @@ -95,11 +97,11 @@ def dispatch_by_operation(entity_type, entity, operation):
"""Call the appropriate index method for a given notification."""
try:
index = index_for(entity_type)
if operation == domain_object.DomainObjectOperation.new:
if operation == model.domain_object.DomainObjectOperation.new:
index.insert_dict(entity)
elif operation == domain_object.DomainObjectOperation.changed:
elif operation == model.domain_object.DomainObjectOperation.changed:
index.update_dict(entity)
elif operation == domain_object.DomainObjectOperation.deleted:
elif operation == model.domain_object.DomainObjectOperation.deleted:
index.remove_dict(entity)
else:
log.warn("Unknown operation: %s" % operation)
Expand All @@ -110,22 +112,22 @@ def dispatch_by_operation(entity_type, entity, operation):
raise


class SynchronousSearchPlugin(SingletonPlugin):
class SynchronousSearchPlugin(p.SingletonPlugin):
"""Update the search index automatically."""
implements(IDomainObjectModification, inherit=True)
p.implements(p.IDomainObjectModification, inherit=True)

def notify(self, entity, operation):
if not isinstance(entity, model.Package):
return
if operation != domain_object.DomainObjectOperation.deleted:
if operation != model.domain_object.DomainObjectOperation.deleted:
dispatch_by_operation(
entity.__class__.__name__,
get_action('package_show')(
logic.get_action('package_show')(
{'model': model, 'ignore_auth': True, 'validate': False},
{'id': entity.id}),
operation
)
elif operation == domain_object.DomainObjectOperation.deleted:
elif operation == model.domain_object.DomainObjectOperation.deleted:
dispatch_by_operation(entity.__class__.__name__,
{'id': entity.id}, operation)
else:
Expand All @@ -142,13 +144,12 @@ def rebuild(package_id=None, only_missing=False, force=False, refresh=False, def
True, if an exception is found, the exception will be logged, but
the process will carry on.
'''
from ckan import model
log.info("Rebuilding search index...")

package_index = index_for(model.Package)

if package_id:
pkg_dict = get_action('package_show')(
pkg_dict = logic.get_action('package_show')(
{'model': model, 'ignore_auth': True, 'validate': False},
{'id': package_id})
log.info('Indexing just package %r...', pkg_dict['name'])
Expand Down Expand Up @@ -177,7 +178,7 @@ def rebuild(package_id=None, only_missing=False, force=False, refresh=False, def
for pkg_id in package_ids:
try:
package_index.update_dict(
get_action('package_show')(
logic.get_action('package_show')(
{'model': model, 'ignore_auth': True,
'validate': False},
{'id': pkg_id}
Expand All @@ -203,7 +204,6 @@ def commit():
log.info('Commited pending changes on the search index')

def check():
from ckan import model
package_query = query_for(model.Package)

log.debug("Checking packages search index...")
Expand All @@ -220,14 +220,12 @@ def check():


def show(package_reference):
from ckan import model
package_query = query_for(model.Package)

return package_query.get_index(package_reference)


def clear(package_reference=None):
from ckan import model
package_index = index_for(model.Package)
if package_reference:
log.debug("Clearing search index for dataset %s..." %
Expand Down Expand Up @@ -259,7 +257,6 @@ def check_solr_schema_version(schema_file=None):
be only used for testing purposes (Default is None)
'''

import urllib2

if SIMPLE_SEARCH:
# Not using the SOLR search backend
Expand Down Expand Up @@ -290,7 +287,6 @@ def check_solr_schema_version(schema_file=None):
url = 'file://%s' % schema_file
res = urllib2.urlopen(url)

import xml.dom.minidom
tree = xml.dom.minidom.parseString(res.read())

version = tree.documentElement.getAttribute('version')
Expand Down

0 comments on commit 7e8f984

Please sign in to comment.