Anonymous avatar Anonymous committed f37c617

added mechanisme to allow adding search functionlaity to models without modifiying them

Comments (0)

Files changed (2)


+from search.core import default_splitter, site_language, SearchManager, \
+    install_index_model
+def autodiscover():
+    """
+    Automatically add managers from search_index modules.
+    """
+    import imp
+    from django.conf import settings
+    from django.utils.importlib import import_module
+    for app in settings.INSTALLED_APPS:
+        # For each app, we need to look for an inside that app's
+        # package. We can't use os.path here -- recall that modules may be
+        # imported different ways (think zip files) -- so we need to get
+        # the app's __path__ and look for on that path.
+        # Step 1: find out the app's __path__ Import errors here will (and
+        # should) bubble up, but a missing __path__ (which is legal, but weird)
+        # fails silently -- apps that do weird things with __path__ might
+        # need to roll their own index registration.
+        try:
+            app_path = import_module(app).__path__
+        except AttributeError:
+            continue
+        # Step 2: use imp.find_module to find the app's For some
+        # reason imp.find_module raises ImportError if the app can't be found
+        # but doesn't actually try to import the module. So skip this app if
+        # its doesn't exist
+        try:
+            imp.find_module('search_index', app_path)
+        except ImportError:
+            continue
+        # Step 3: import the app's search_index file. If this has errors we want them
+        # to bubble up.
+        import_module("%s.search_index" % app)
+def register(model, mgr_name, fields_to_index, indexer=None,
+    splitter=default_splitter, relation_index=True, integrate='*', filters={},
+    language=site_language, **kwargs):
+    """
+    Add a search manager to the model.
+    """
+    if not hasattr(model, '_meta'):
+        raise AttributeError('The model being registered must derive from Model.')
+    if hasattr(model, mgr_name):
+        raise AttributeError('The model being registered already defines a'
+            ' property called %s.' % mgr_name)
+    model.add_to_class(mgr_name, SearchManager(fields_to_index, indexer, splitter,
+        relation_index, integrate, filters, language, **kwargs))
+    install_index_model(model)
     if needs_relation_index:
         signals.post_save.connect(post_save, sender=sender)
         signals.post_delete.connect(post_delete, sender=sender)
 class QueryTraits(object):
     def __iter__(self):
     # TODO: add keys_only query
 #    def values(self, fields):
 #        pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.