source: main/trunk/openPLM/plmapp/search_indexes.py @ 476

Revision 476, 4.0 KB checked in by pcosquer, 9 years ago (diff)

search: users & groups: update the index only if the object have been modified/created by a controller
This should reduce the number of calls to update_index

Line 
1from django.conf import settings
2
3from haystack.indexes import *
4from haystack import site
5
6import openPLM.plmapp.models as models
7
8from django.db.models import signals
9from django.db.models.loading import get_model
10
11from haystack import indexes
12
13from openPLM.plmapp.tasks import update_index
14
15###########################
16# from https://github.com/mixcloud/django-celery-haystack-SearchIndex/
17# by sdcooke
18
19def remove_instance_from_index(instance):
20    model_class = get_model(instance._meta.app_label, instance._meta.module_name)
21    search_index = site.get_index(model_class)
22    search_index.remove_object(instance)
23
24class QueuedSearchIndex(indexes.SearchIndex):
25    """
26A ``SearchIndex`` subclass that enqueues updates for later processing.
27
28Deletes are handled instantly since a reference, not the instance, is put on the queue. It would not be hard
29to update this to handle deletes as well (with a delete task).
30"""
31    # We override the built-in _setup_* methods to connect the enqueuing operation.
32    def _setup_save(self, model):
33        signals.post_save.connect(self.enqueue_save, sender=model)
34
35    def _setup_delete(self, model):
36        signals.post_delete.connect(self.enqueue_delete, sender=model)
37
38    def _teardown_save(self, model):
39        signals.post_save.disconnect(self.enqueue_save, sender=model)
40
41    def _teardown_delete(self, model):
42        signals.post_delete.disconnect(self.enqueue_delete, sender=model)
43
44    def enqueue_save(self, instance, **kwargs):
45        if not getattr(instance, "no_index", False):
46            update_index.delay(instance._meta.app_label,
47                    instance._meta.module_name, instance._get_pk_val())
48
49    def enqueue_delete(self, instance, **kwargs):
50        remove_instance_from_index(instance)
51
52##################
53
54def set_template_name(index):
55    for name, field in index.fields.iteritems():
56        field.template_name = "search/indexes_%s.txt" % name
57
58
59class QueuedModelSearchIndex(ModelSearchIndex, QueuedSearchIndex):
60    pass
61
62class UserIndex(ModelSearchIndex):
63    class Meta:
64        pass
65   
66    rendered = CharField(use_template=True, indexed=False)
67    rendered_add = CharField(use_template=True, indexed=False)
68
69set_template_name(UserIndex)
70site.register(models.User, UserIndex)
71
72class GroupIndex(ModelSearchIndex):
73    class Meta:
74        pass
75   
76    rendered = CharField(use_template=True, indexed=False)
77    rendered_add = CharField(use_template=True, indexed=False)
78
79set_template_name(GroupIndex)
80site.register(models.GroupInfo, GroupIndex)
81
82
83
84for key, model in models.get_all_plmobjects().iteritems():
85    if model == models.GroupInfo:
86        continue
87    class ModelIndex(QueuedModelSearchIndex):
88        model = model
89        key = key
90        class Meta:
91            fields = set(model.get_creation_fields())
92            fields.update(model.get_modification_fields())
93       
94        owner = CharField(model_attr="owner")
95        creator = CharField(model_attr="creator")
96
97        rendered = CharField(use_template=True, indexed=False)
98        rendered_add = CharField(use_template=True, indexed=False)
99       
100        def prepare_owner(self, obj):
101            return obj.owner.username
102
103        def prepare_creator(self, obj):
104            return obj.creator.username
105
106        def index_queryset(self):
107            if "type" in self.model.get_creation_fields():
108                return self.model.objects.filter(type=self.key)
109            else:
110                return self.model.objects.all()
111
112    set_template_name(ModelIndex)
113    site.register(model, ModelIndex)
114
115from subprocess import Popen, PIPE
116class DocumentFileIndex(QueuedModelSearchIndex):
117    text = CharField(document=True, use_template=True)
118    filename = CharField(model_attr='filename')
119    file = CharField(model_attr='file')
120   
121    rendered = CharField(use_template=True, indexed=False)
122    rendered_add = CharField(use_template=True, indexed=False)
123
124    def prepare_file(self, obj):
125        p = Popen([settings.EXTRACTOR, obj.file.path], stdout=PIPE, close_fds=True)
126        return p.stdout.read()
127
128site.register(models.DocumentFile, DocumentFileIndex)
129
Note: See TracBrowser for help on using the repository browser.