<commit_before>from haystack import indexes, signals
from .models import Unit, Service
from django.utils.translation import get_language
from django.db import models

class DeleteOnlySignalProcessor(signals.BaseSignalProcessor):
    """
    Delete models from index automatically.
    """
    def setup(self):
        models.signals.post_delete.connect(self.handle_delete)
        # TODO: ?
        # Efficient would be going through all backends & collecting all models
        # being used, then hooking up signals only for those.

    def teardown(self):
        # Naive (listen to all model saves).
        models.signals.post_delete.disconnect(self.handle_delete)


class ServiceMapBaseIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    autosuggest = indexes.EdgeNgramField(model_attr='name')
    extra_searchwords = indexes.CharField()
    autosuggest_extra_searchwords = indexes.CharField()

    def get_model(self):
        return None

    def _prepare_extra_searchwords(self, obj):
        return ' '.join([category.name for category in obj.keywords.filter(language=get_language())])

    def prepare_extra_searchwords(self, obj):
        return self._prepare_extra_searchwords(obj)

    def prepare_autosuggest_extra_searchwords(self, obj):
        return self._prepare_extra_searchwords(obj)


class UnitIndex(ServiceMapBaseIndex):

    def get_updated_field(self):
        return 'origin_last_modified_time'

    def get_model(self):
        return Unit


class ServiceIndex(ServiceMapBaseIndex):

    def get_updated_field(self):
        return 'last_modified_time'

    def get_model(self):
        return Service

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(identical_to=None)

    # def prepare(self, obj):
    #     obj.lang_keywords = obj.keywords.filter(language=get_language())
    #     data = super(ServiceIndex, self).prepare(obj)
    #     # if obj.name == 'NAME_TO_BOOST':
    #     #     data['boost'] = 1.1
    #     return data<commit_msg>Import models into index module lazily.<commit_after>from haystack import indexes, signals
from django.utils.translation import get_language
from django.db import models
from django.db.models.loading import get_model

class DeleteOnlySignalProcessor(signals.BaseSignalProcessor):
    """
    Delete models from index automatically.
    """
    def setup(self):
        models.signals.post_delete.connect(self.handle_delete)
        # TODO: ?
        # Efficient would be going through all backends & collecting all models
        # being used, then hooking up signals only for those.

    def teardown(self):
        # Naive (listen to all model saves).
        models.signals.post_delete.disconnect(self.handle_delete)


class ServiceMapBaseIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    autosuggest = indexes.EdgeNgramField(model_attr='name')
    extra_searchwords = indexes.CharField()
    autosuggest_extra_searchwords = indexes.CharField()

    def __init__(self, *args, **kwargs):
        super(*args, **kwargs)
        self.model = None

    def get_model(self):
        return self.model

    def _prepare_extra_searchwords(self, obj):
        return ' '.join([category.name for category in obj.keywords.filter(language=get_language())])

    def prepare_extra_searchwords(self, obj):
        return self._prepare_extra_searchwords(obj)

    def prepare_autosuggest_extra_searchwords(self, obj):
        return self._prepare_extra_searchwords(obj)


class UnitIndex(ServiceMapBaseIndex):

    def __init__(self, *args, **kwargs):
        super(*args, **kwargs)
        self.model = get_model('services', 'Unit')

    def get_updated_field(self):
        return 'origin_last_modified_time'

class ServiceIndex(ServiceMapBaseIndex):

    def __init__(self, *args, **kwargs):
        super(*args, **kwargs)
        self.model = get_model('services', 'Service')

    def get_updated_field(self):
        return 'last_modified_time'

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(identical_to=None)

    # def prepare(self, obj):
    #     obj.lang_keywords = obj.keywords.filter(language=get_language())
    #     data = super(ServiceIndex, self).prepare(obj)
    #     # if obj.name == 'NAME_TO_BOOST':
    #     #     data['boost'] = 1.1
    #     return data
{'fraction_non_alphanumeric': 0.09818018422826331, 'fraction_numerical': 0.0008986744551786115, 'mean_word_length': 3.584963954685891, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5365344', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1264, 'n_words': 285}