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

Revision 471, 3.7 KB checked in by pcosquer, 9 years ago (diff)

csv: work on #46
this commit enables mails sending/index updating after the database commit
a new task (update_indexes) is available to update several indexes at once

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(QueuedModelSearchIndex):
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
72
73for key, model in models.get_all_plmobjects().iteritems():
74    class ModelIndex(QueuedModelSearchIndex):
75        model = model
76        key = key
77        class Meta:
78            fields = set(model.get_creation_fields())
79            fields.update(model.get_modification_fields())
80       
81        owner = CharField(model_attr="owner")
82        creator = CharField(model_attr="creator")
83
84        rendered = CharField(use_template=True, indexed=False)
85        rendered_add = CharField(use_template=True, indexed=False)
86       
87        def prepare_owner(self, obj):
88            return obj.owner.username
89
90        def prepare_creator(self, obj):
91            return obj.creator.username
92
93        def index_queryset(self):
94            if "type" in self.model.get_creation_fields():
95                return self.model.objects.filter(type=self.key)
96            else:
97                return self.model.objects.all()
98
99    set_template_name(ModelIndex)
100    site.register(model, ModelIndex)
101
102from subprocess import Popen, PIPE
103class DocumentFileIndex(QueuedModelSearchIndex):
104    text = CharField(document=True, use_template=True)
105    filename = CharField(model_attr='filename')
106    file = CharField(model_attr='file')
107   
108    rendered = CharField(use_template=True, indexed=False)
109    rendered_add = CharField(use_template=True, indexed=False)
110
111    def prepare_file(self, obj):
112        p = Popen([settings.EXTRACTOR, obj.file.path], stdout=PIPE, close_fds=True)
113        return p.stdout.read()
114
115site.register(models.DocumentFile, DocumentFileIndex)
116
Note: See TracBrowser for help on using the repository browser.