forked from aptivate/intranet-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeypatches.py
143 lines (117 loc) · 5.73 KB
/
monkeypatches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from binder.monkeypatch import before, after, patch
from haystack.backends.whoosh_backend import WhooshSearchBackend
def build_schema_with_debugging(original_function, self, fields):
"""
print "build_schema fields = %s" % fields
from haystack import connections
unified = connections[self.connection_alias].get_unified_index()
print "indexes = %s" % unified.indexes
# self.content_field_name, self.schema = self.build_schema(connections[self.connection_alias].get_unified_index().all_searchfields())
print "collect_indexes = %s" % unified.collect_indexes()
print "apps = %s" % settings.INSTALLED_APPS
import inspect
try:
from django.utils import importlib
except ImportError:
from haystack.utils import importlib
search_index_module = importlib.import_module("documents.search_indexes")
for item_name, item in inspect.getmembers(search_index_module, inspect.isclass):
print "%s: %s" % (item_name,
getattr(item, 'haystack_use_for_indexing', False))
if getattr(item, 'haystack_use_for_indexing', False):
# We've got an index. Check if we should be ignoring it.
class_path = "documents.search_indexes.%s" % (item_name)
print "excluded_index %s = %s" % (class_path,
class_path in unified.excluded_indexes)
print "excluded_indexes_id %s = %s" % (str(item_name),
unified.excluded_indexes_ids.get(item_name) == id(item))
"""
from django.conf import settings
print "build_schema: settings = %s" % settings
print "build_schema: INSTALLED_APPS = %s" % settings.INSTALLED_APPS
# import pdb; pdb.set_trace()
return original_function(self, fields)
# patch(WhooshSearchBackend, 'build_schema', build_schema_with_debugging)
from haystack.utils.loading import UnifiedIndex
def build_with_debugging(original_function, self, indexes=None):
print "UnifiedIndex build(%s)" % indexes
import traceback
traceback.print_stack()
original_function(self, indexes)
print "UnifiedIndex built: indexes = %s" % self.indexes
# patch(UnifiedIndex, 'build', build_with_debugging)
def collect_indexes_with_debugging(original_function, self):
from django.conf import settings
print "collect_indexes: settings = %s" % settings
print "collect_indexes: INSTALLED_APPS = %s" % settings.INSTALLED_APPS
# import pdb; pdb.set_trace()
from haystack import connections
from django.utils.module_loading import module_has_submodule
import inspect
try:
from django.utils import importlib
except ImportError:
from haystack.utils import importlib
for app in settings.INSTALLED_APPS:
print "collect_indexes: trying %s" % app
mod = importlib.import_module(app)
try:
search_index_module = importlib.import_module("%s.search_indexes" % app)
except ImportError:
if module_has_submodule(mod, 'search_indexes'):
raise
continue
for item_name, item in inspect.getmembers(search_index_module, inspect.isclass):
print "collect_indexes: %s: %s" % (item_name,
getattr(item, 'haystack_use_for_indexing', False))
if getattr(item, 'haystack_use_for_indexing', False):
# We've got an index. Check if we should be ignoring it.
class_path = "%s.search_indexes.%s" % (app, item_name)
print "excluded_index %s = %s" % (class_path,
class_path in self.excluded_indexes)
print "excluded_indexes_id %s = %s" % (str(item_name),
self.excluded_indexes_ids.get(item_name) == id(item))
if class_path in self.excluded_indexes or self.excluded_indexes_ids.get(item_name) == id(item):
self.excluded_indexes_ids[str(item_name)] = id(item)
continue
return original_function(self)
# patch(UnifiedIndex, 'collect_indexes', collect_indexes_with_debugging)
from south.management.commands.syncdb import Command as SouthSyncdbCommand
# @after(SouthSyncdbCommand, 'handle_noargs')
def haystack_reset_after_syncdb(self, migrate_all=False, **options):
"""
Work around https://github.com/toastdriven/django-haystack/issues/495 and
http://south.aeracode.org/ticket/1023 by resetting the UnifiedIndex
after South's syncdb has run.
"""
from haystack import connections
for conn in connections.all():
conn.get_unified_index().teardown_indexes()
conn.get_unified_index().reset()
conn.get_unified_index().setup_indexes()
@before(SouthSyncdbCommand, 'handle_noargs')
def haystack_init_before_syncdb(self, migrate_all=False, **options):
"""
Alternative workaround, that initialises Haystack first, so that
fixtures can be indexed.
"""
# breakpoint()
from haystack import connections
for conn in connections.all():
conn.get_unified_index().setup_indexes()
# from haystack.indexes import SearchIndex
# before(SearchIndex, 'update_object')(breakpoint)
# from haystack.indexes import RealTimeSearchIndex
# before(RealTimeSearchIndex, '_setup_save')(breakpoint)
# from django.forms.forms import BoundField
# before(BoundField, 'value')(breakpoint)
from haystack.management.commands.update_index import Command as UpdateCommand
@before(UpdateCommand, 'handle')
def dont_die_on_missing_files_in_update_index(self, *items, **options):
def replacement_prepare_text(original_function, self, document):
try:
return original_function(self, document)
except IOError as e: # missing file, cannot index
return e
from documents.search_indexes import DocumentIndex
patch(DocumentIndex, 'prepare_text', replacement_prepare_text)