示例#1
0
def get_model_index(instance):
    """
    Return the index responsible for this instance. Returns None if no index is registered.
    """
    from lego.utils.content_types import instance_to_content_type_string

    return index_registry.get(instance_to_content_type_string(instance))
示例#2
0
    def prepare(self, instance):
        """
        Prepare instance for indexing, this function returns a dict in a specific format.
        This is passed to the search-backend. The backend has to parse this and index it.
        """
        from lego.utils.content_types import instance_to_content_type_string

        serializer = self.get_serializer(instance)
        data = serializer.data

        def get_filter_data(data):
            get_func = data.get if self.get_index_filter_fields() else data.pop
            filter_fields = self.get_filter_fields()
            return {key: get_func(key) for key in filter_fields}

        prepared_instance = {
            "content_type":
            force_text(instance_to_content_type_string(instance)),
            "pk": force_text(instance.pk),
            "data": {
                "autocomplete": self.get_autocomplete(instance),
                "filters": {
                    k: v
                    for k, v in get_filter_data(data).items()
                    if v or not v == ""
                },
                "fields": {k: v
                           for k, v in data.items() if v or not v == ""},
            },
        }

        return prepared_instance
示例#3
0
def register_search_index(search_index_cls):
    """
    Register the search index in our index registry.
    """
    from lego.utils.content_types import instance_to_content_type_string

    search_index = search_index_cls()
    model = search_index.get_model()
    index_registry[instance_to_content_type_string(model)] = search_index
示例#4
0
    def remove_instance(self, pk):
        """
        Remove a single instance from the index. We use pks here because the instance may not
        exists in the database.
        """
        from lego.utils.content_types import instance_to_content_type_string

        self.get_backend().remove(content_type=instance_to_content_type_string(
            self.get_model()),
                                  pk=force_text(pk))
示例#5
0
文件: postgres.py 项目: webkom/lego
    def serialize_object(self, object, search_type):
        from lego.utils.content_types import instance_to_content_type_string

        content_type = instance_to_content_type_string(object)
        search_index = self.get_search_index(content_type)
        serializer = search_index.get_serializer(object)
        fields = (
            search_index.get_autocomplete_result_fields()
            if search_type == "autocomplete"
            else search_index.get_result_fields()
        )
        result = {field: serializer.data[field] for field in fields}
        result.update({"id": object.pk, "content_type": content_type, "text": "text"})
        return result