Esempio n. 1
0
    def render(self, context):
        text_block = self.text_block.resolve(context)
        query = self.query.resolve(context)
        kwargs = {}

        if self.html_tag is not None:
            kwargs['html_tag'] = self.html_tag.resolve(context)

        if self.css_class is not None:
            kwargs['css_class'] = self.css_class.resolve(context)

        if self.max_length is not None:
            kwargs['max_length'] = self.max_length.resolve(context)

        # Handle a user-defined highlighting function.
        if hasattr(settings, 'HAYSTACK_CUSTOM_HIGHLIGHTER') and settings.HAYSTACK_CUSTOM_HIGHLIGHTER:
            # Do the import dance.
            try:
                path_bits = settings.HAYSTACK_CUSTOM_HIGHLIGHTER.split('.')
                highlighter_path, highlighter_classname = '.'.join(path_bits[:-1]), path_bits[-1]
                highlighter_module = importlib.import_module(highlighter_path)
                highlighter_class = getattr(highlighter_module, highlighter_classname)
            except (ImportError, AttributeError) as e:
                raise ImproperlyConfigured("The highlighter '%s' could not be imported: %s" % (settings.HAYSTACK_CUSTOM_HIGHLIGHTER, e))
        else:
            from haystack.utils.highlighting import Highlighter
            highlighter_class = Highlighter

        highlighter = highlighter_class(query, **kwargs)
        highlighted_text = highlighter.highlight(text_block)
        return highlighted_text
Esempio n. 2
0
    def collect_indexes(self):
        indexes = []

        for app_mod in haystack_get_app_modules():
            try:
                search_index_module = importlib.import_module(
                    "%s.search_indexes" % app_mod.__name__)
            except ImportError:
                if module_has_submodule(app_mod, "search_indexes"):
                    raise

                continue

            for item_name, item in inspect.getmembers(search_index_module,
                                                      inspect.isclass):
                if getattr(item, "haystack_use_for_indexing",
                           False) and getattr(item, "get_model", None):
                    # We've got an index. Check if we should be ignoring it.
                    class_path = "%s.search_indexes.%s" % (app_mod.__name__,
                                                           item_name)

                    if class_path in self.excluded_indexes or self.excluded_indexes_ids.get(
                            item_name) == id(item):
                        self.excluded_indexes_ids[str(item_name)] = id(item)
                        continue

                    indexes.append(item())

        return indexes
Esempio n. 3
0
    def collect_indexes(self):
        indexes = []

        for app_mod in haystack_get_app_modules():
            try:
                search_index_module = importlib.import_module(
                    "%s.search_indexes" % app_mod.__name__
                )
            except ImportError:
                if module_has_submodule(app_mod, "search_indexes"):
                    raise

                continue

            for item_name, item in inspect.getmembers(
                search_index_module, inspect.isclass
            ):
                if getattr(item, "haystack_use_for_indexing", False) and getattr(
                    item, "get_model", None
                ):
                    # We've got an index. Check if we should be ignoring it.
                    class_path = "%s.search_indexes.%s" % (app_mod.__name__, item_name)

                    if class_path in self.excluded_indexes or self.excluded_indexes_ids.get(
                        item_name
                    ) == id(
                        item
                    ):
                        self.excluded_indexes_ids[str(item_name)] = id(item)
                        continue

                    indexes.append(item())

        return indexes
Esempio n. 4
0
def import_class(path):
    path_bits = path.split('.')
    # Cut off the class name at the end.
    class_name = path_bits.pop()
    module_path = '.'.join(path_bits)
    module_itself = importlib.import_module(module_path)

    if not hasattr(module_itself, class_name):
        raise ImportError("The Python module '%s' has no '%s' class." % (module_path, class_name))

    return getattr(module_itself, class_name)
Esempio n. 5
0
def import_class(path):
    path_bits = path.split('.')
    # Cut off the class name at the end.
    class_name = path_bits.pop()
    module_path = '.'.join(path_bits)
    module_itself = importlib.import_module(module_path)

    if not hasattr(module_itself, class_name):
        raise ImportError("The Python module '%s' has no '%s' class." % (module_path, class_name))

    return getattr(module_itself, class_name)
Esempio n. 6
0
 def haystack_get_app_modules():
     """Return the Python module for each installed app"""
     return [importlib.import_module(i) for i in settings.INSTALLED_APPS]
 def haystack_get_app_modules():
     """Return the Python module for each installed app"""
     return [importlib.import_module(i) for i in settings.INSTALLED_APPS]