source: main/branches/3D/openPLM/plmapp/search_indexes.py @ 595

Revision 595, 4.1 KB checked in by pcosquer, 8 years ago (diff)

3D branch: merge changes from trunk rev 594

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        state = CharField(model_attr="state__name")
97        lifecycle = CharField(model_attr="lifecycle__name")
98
99        rendered = CharField(use_template=True, indexed=False)
100        rendered_add = CharField(use_template=True, indexed=False)
101       
102        def prepare_owner(self, obj):
103            return obj.owner.username
104
105        def prepare_creator(self, obj):
106            return obj.creator.username
107
108        def index_queryset(self):
109            if "type" in self.model.get_creation_fields():
110                return self.model.objects.filter(type=self.key)
111            else:
112                return self.model.objects.all()
113
114    set_template_name(ModelIndex)
115    site.register(model, ModelIndex)
116
117from subprocess import Popen, PIPE
118class DocumentFileIndex(QueuedModelSearchIndex):
119    text = CharField(document=True, use_template=True)
120    filename = CharField(model_attr='filename')
121    file = CharField(model_attr='file')
122   
123    rendered = CharField(use_template=True, indexed=False)
124    rendered_add = CharField(use_template=True, indexed=False)
125
126    def prepare_file(self, obj):
127        p = Popen([settings.EXTRACTOR, obj.file.path], stdout=PIPE, close_fds=True)
128        return p.stdout.read()
129
130site.register(models.DocumentFile, DocumentFileIndex)
131
Note: See TracBrowser for help on using the repository browser.