Ejemplo n.º 1
0
def get_engine(engine_slug_):
    '''returns search engine with a given name'''
    try:
        return [
            x[1] for x in SearchEngine.get_created_engines()
            if x[0] == engine_slug_
        ][0]
    except IndexError:
        raise CommandError("Search Engine \"%s\" is not registered!" %
                           engine_slug_)
Ejemplo n.º 2
0
 def handle_noargs(self, **options):
     """Runs the management command."""
     verbosity = int(options.get("verbosity", 1))
     for engine_slug, search_engine in SearchEngine.get_created_engines():
         registered_models = search_engine.get_registered_models()
         # Rebuild the index for all registered models.
         refreshed_model_count = [0]  # HACK: Allows assignment to outer scope.
         def iter_search_entries():
             for model in registered_models:
                 local_refreshed_model_count = 0
                 for obj in model._default_manager.all().iterator():
                     for search_entry in search_engine._update_obj_index_iter(obj):
                         yield search_entry
                     local_refreshed_model_count += 1
                     if verbosity >= 3:
                         print("Refreshed search entry for {model} {obj} in {engine_slug!r} search engine.".format(
                             model = model._meta.verbose_name,
                             obj = obj,
                             engine_slug = engine_slug,
                         ))
                 refreshed_model_count[0] += local_refreshed_model_count
                 if verbosity == 2:
                     print("Refreshed {local_refreshed_model_count} {model} search entry(s) in {engine_slug!r} search engine.".format(
                         model = model._meta.verbose_name,
                         local_refreshed_model_count = local_refreshed_model_count,
                         engine_slug = engine_slug,
                     ))
         _bulk_save_search_entries(iter_search_entries())
         if verbosity == 1:
             print("Refreshed {refreshed_model_count} search entry(s) in {engine_slug!r} search engine.".format(
                 refreshed_model_count = refreshed_model_count[0],
                 engine_slug = engine_slug,
             ))
         # Clean out any search entries that exist for stale content types.
         valid_content_types = [ContentType.objects.get_for_model(model) for model in registered_models]
         stale_entries = SearchEntry.objects.filter(
             engine_slug = engine_slug,
         ).exclude(
             content_type__in = valid_content_types
         )
         stale_entry_count = stale_entries.count()
         if stale_entry_count > 0:
             stale_entries.delete()
         if verbosity >= 1:
             print("Deleted {stale_entry_count} stale search entry(s) in {engine_slug!r} search engine.".format(
                 stale_entry_count = stale_entry_count,
                 engine_slug = engine_slug,
             ))
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        """Runs the management command."""
        verbosity = int(options.get("verbosity", 1))

        # see if we're asked to use a specific search engine
        if options['engine']:
            engine_slug = options['engine']
            engine_selected = True
        else:
            engine_slug = "default"
            engine_selected = False

        # get the search engine we'll be checking registered models for, may be "default"
        search_engine = get_engine(engine_slug)

        models = []
        for model_name in args:
            try:
                model = get_model(
                    *model_name.split("."))  # app label, model name
            except TypeError:  # were we given only model name without app_name?
                registered_models = search_engine.get_registered_models()
                matching_models = [
                    x for x in registered_models if x.__name__ == model_name
                ]
                if len(matching_models) > 1:
                    raise CommandError(
                        "Model name \"%s\" is not unique, cannot continue!" %
                        model_name)
                if matching_models:
                    model = matching_models[0]
                else:
                    model = None
            if model is None or not search_engine.is_registered(model):
                raise CommandError(
                    "Model \"%s\" is not registered with django-watson search engine \"%s\"!"
                    % (model_name, engine_slug))
            models.append(model)

        refreshed_model_count = 0

        if models:  # request for (re-)building index for a subset of registered models
            if verbosity >= 3:
                print("Using search engine \"%s\"" % engine_slug)
            for model in models:
                refreshed_model_count += rebuild_index_for_model(
                    model, engine_slug, verbosity)

        else:  # full rebuild (for one or all search engines)
            if engine_selected:
                engine_slugs = [engine_slug]
                if verbosity >= 2:
                    # let user know the search engine if they selected one
                    print(
                        "Rebuilding models registered with search engine \"%s\""
                        % engine_slug)
            else:  # loop through all engines
                engine_slugs = [
                    x[0] for x in SearchEngine.get_created_engines()
                ]

            for engine_slug in engine_slugs:
                search_engine = get_engine(engine_slug)
                registered_models = search_engine.get_registered_models()
                # Rebuild the index for all registered models.
                for model in registered_models:
                    refreshed_model_count += rebuild_index_for_model(
                        model, engine_slug, verbosity)

            # Clean out any search entries that exist for stale content types. Only do it during full rebuild
            valid_content_types = [
                ContentType.objects.get_for_model(model)
                for model in registered_models
            ]
            stale_entries = SearchEntry.objects.filter(
                engine_slug=engine_slug, ).exclude(
                    content_type__in=valid_content_types)
            stale_entry_count = stale_entries.count()
            if stale_entry_count > 0:
                stale_entries.delete()
            if verbosity >= 1:
                print(
                    "Deleted {stale_entry_count} stale search entry(s) in {engine_slug!r} search engine."
                    .format(
                        stale_entry_count=stale_entry_count,
                        engine_slug=engine_slug,
                    ))

        if verbosity == 1:
            print(
                "Refreshed {refreshed_model_count} search entry(s) in {engine_slug!r} search engine."
                .format(
                    refreshed_model_count=refreshed_model_count,
                    engine_slug=engine_slug,
                ))
Ejemplo n.º 4
0
        self.assertTrue(WatsonTestModel1 in watson.get_registered_models())
        self.assertTrue(
            isinstance(watson.get_adapter(WatsonTestModel1),
                       watson.SearchAdapter))
        # Unregister the model and text.
        watson.unregister(WatsonTestModel1)
        self.assertFalse(watson.is_registered(WatsonTestModel1))
        self.assertRaises(RegistrationError,
                          lambda: watson.unregister(WatsonTestModel1))
        self.assertTrue(WatsonTestModel1 not in watson.get_registered_models())
        self.assertRaises(
            RegistrationError,
            lambda: isinstance(watson.get_adapter(WatsonTestModel1)))


complex_registration_search_engine = SearchEngine("restricted")


class InstallUninstallTestBase(TestCase):
    def testUninstallAndInstall(self):
        # Not too much to test here, as some backends don't require installation.
        # Just make sure the commands don't error.
        call_command("uninstallwatson", verbosity=0)
        call_command("installwatson", verbosity=0)

    @skipUnless(get_backend().requires_installation,
                "search backend does not require installation")
    def testRealInstallAndUninstall(self):
        backend = get_backend()
        call_command("uninstallwatson", verbosity=0)
        self.assertFalse(backend.is_installed())
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        """Runs the management command."""
        verbosity = int(options.get("verbosity", 1))

        # see if we're asked to use a specific search engine
        if options['engine']:
            engine_slug = options['engine']
            engine_selected = True
        else:
            engine_slug = "default"
            engine_selected = False

        # get the search engine we'll be checking registered models for, may be "default"
        search_engine = get_engine(engine_slug)

        models = []
        for model_name in args:
            try:
                model = get_model(*model_name.split("."))  # app label, model name
            except TypeError:  # were we given only model name without app_name?
                registered_models = search_engine.get_registered_models()
                matching_models = [x for x in registered_models if x.__name__ == model_name]
                if len(matching_models) > 1:
                    raise CommandError("Model name \"%s\" is not unique, cannot continue!" % model_name)
                if matching_models:
                    model = matching_models[0]
                else:
                    model = None
            if model is None or not search_engine.is_registered(model):
                raise CommandError("Model \"%s\" is not registered with django-watson search engine \"%s\"!" % (model_name, engine_slug))
            models.append(model)
        
        refreshed_model_count = 0

        if models:  # request for (re-)building index for a subset of registered models
            if verbosity >= 3:
                print("Using search engine \"%s\"" % engine_slug)
            for model in models:
                refreshed_model_count += rebuild_index_for_model(model, engine_slug, verbosity)

        else:  # full rebuild (for one or all search engines)
            if engine_selected:
                engine_slugs = [engine_slug]
                if verbosity >= 2:
                    # let user know the search engine if they selected one
                    print("Rebuilding models registered with search engine \"%s\"" % engine_slug)
            else:  # loop through all engines
                engine_slugs = [x[0] for x in SearchEngine.get_created_engines()]

            for engine_slug in engine_slugs:
                search_engine = get_engine(engine_slug)
                registered_models = search_engine.get_registered_models()
                # Rebuild the index for all registered models.
                for model in registered_models:
                    refreshed_model_count += rebuild_index_for_model(model, engine_slug, verbosity)

            # Clean out any search entries that exist for stale content types. Only do it during full rebuild
            valid_content_types = [ContentType.objects.get_for_model(model) for model in registered_models]
            stale_entries = SearchEntry.objects.filter(
                engine_slug = engine_slug,
            ).exclude(
                content_type__in = valid_content_types
            )
            stale_entry_count = stale_entries.count()
            if stale_entry_count > 0:
                stale_entries.delete()
            if verbosity >= 1:
                print("Deleted {stale_entry_count} stale search entry(s) in {engine_slug!r} search engine.".format(
                    stale_entry_count = stale_entry_count,
                    engine_slug = engine_slug,
                ))

        if verbosity == 1:
            print("Refreshed {refreshed_model_count} search entry(s) in {engine_slug!r} search engine.".format(
                refreshed_model_count = refreshed_model_count,
                engine_slug = engine_slug,
            ))
Ejemplo n.º 6
0
def get_engine(engine_slug_):
    '''returns search engine with a given name'''
    try:
        return [x[1] for x in SearchEngine.get_created_engines() if x[0] == engine_slug_][0]
    except IndexError:
        raise CommandError("Search Engine \"%s\" is not registered!" % engine_slug_)
Ejemplo n.º 7
0
"""Admin integration for django-watson."""

from __future__ import unicode_literals

from django.contrib import admin
from django.contrib.admin.views.main import ChangeList

from watson.registration import SearchEngine, SearchAdapter


admin_search_engine = SearchEngine("admin")


class WatsonSearchChangeList(ChangeList):

    """A change list that takes advantage of django-watson full text search."""
        
    def get_query_set(self, *args, **kwargs):
        """Creates the query set."""
        # Do the basic searching.
        search_fields = self.search_fields
        self.search_fields = ()
        try:
            qs = super(WatsonSearchChangeList, self).get_query_set(*args, **kwargs)
        finally:
            self.search_fields = search_fields
        # Do the full text searching.
        if self.query.strip():
            qs = self.model_admin.search_engine.filter(qs, self.query, ranking=False)
        return qs
Ejemplo n.º 8
0
    def handle_noargs(self, **options):
        """Runs the management command."""
        verbosity = int(options.get("verbosity", 1))
        for engine_slug, search_engine in SearchEngine.get_created_engines():
            registered_models = search_engine.get_registered_models()
            # Rebuild the index for all registered models.
            refreshed_model_count = [
                0
            ]  # HACK: Allows assignment to outer scope.

            def iter_search_entries():
                for model in registered_models:
                    local_refreshed_model_count = 0
                    for obj in model._default_manager.all().iterator():
                        for search_entry in search_engine._update_obj_index_iter(
                                obj):
                            yield search_entry
                        local_refreshed_model_count += 1
                        if verbosity >= 3:
                            print(
                                "Refreshed search entry for {model} {obj} in {engine_slug!r} search engine."
                                .format(
                                    model=model._meta.verbose_name,
                                    obj=obj,
                                    engine_slug=engine_slug,
                                ))
                    refreshed_model_count[0] += local_refreshed_model_count
                    if verbosity == 2:
                        print(
                            "Refreshed {local_refreshed_model_count} {model} search entry(s) in {engine_slug!r} search engine."
                            .format(
                                model=model._meta.verbose_name,
                                local_refreshed_model_count=
                                local_refreshed_model_count,
                                engine_slug=engine_slug,
                            ))

            _bulk_save_search_entries(iter_search_entries())
            if verbosity == 1:
                print(
                    "Refreshed {refreshed_model_count} search entry(s) in {engine_slug!r} search engine."
                    .format(
                        refreshed_model_count=refreshed_model_count[0],
                        engine_slug=engine_slug,
                    ))
            # Clean out any search entries that exist for stale content types.
            valid_content_types = [
                ContentType.objects.get_for_model(model)
                for model in registered_models
            ]
            stale_entries = SearchEntry.objects.filter(
                engine_slug=engine_slug, ).exclude(
                    content_type__in=valid_content_types)
            stale_entry_count = stale_entries.count()
            if stale_entry_count > 0:
                stale_entries.delete()
            if verbosity >= 1:
                print(
                    "Deleted {stale_entry_count} stale search entry(s) in {engine_slug!r} search engine."
                    .format(
                        stale_entry_count=stale_entry_count,
                        engine_slug=engine_slug,
                    ))