1
1
from django .db .models import signals
2
+ from django .db .models .loading import get_model
2
3
3
4
from haystack .exceptions import NotHandled
4
5
from haystack .signals import RealtimeSignalProcessor as HaystackRealtimeSignalProcessor
5
6
6
- from storybase_asset .models import HtmlAssetTranslation
7
- from storybase_geo .models import Location
8
- from storybase_help .models import Help , HelpTranslation
9
- from storybase_story .models import (SectionAsset , SectionTranslation ,
10
- Story , StoryTranslation )
11
7
12
8
class RealtimeSignalProcessor (HaystackRealtimeSignalProcessor ):
13
9
"""
@@ -21,15 +17,20 @@ class RealtimeSignalProcessor(HaystackRealtimeSignalProcessor):
21
17
and other related models.
22
18
"""
23
19
24
- # Translate sender classes to the classes that actually have an
25
- # index
20
+ # Translate sender model classes to the model classes that
21
+ # actually have an index
22
+ #
23
+ # The class names mapp to a list appropriate for the input
24
+ # to ``django.db.models.loading.get_model()`` to avoid
25
+ # importing the model directly and causing a circular
26
+ # import. See http://stackoverflow.com/a/17364366/386210
26
27
_SENDER_MAP = {
27
- 'HtmlAssetTranslation' : Story ,
28
- 'Location' : Story ,
29
- 'HelpTranslation' : Help ,
30
- 'SectionAsset' : Story ,
31
- 'SectionTranslation' : Story ,
32
- 'StoryTranslation' : Story ,
28
+ 'HtmlAssetTranslation' : [ 'storybase_story' , ' Story' ] ,
29
+ 'Location' : [ 'storybase_story' , ' Story' ] ,
30
+ 'HelpTranslation' : [ 'storybase_help' , ' Help' ] ,
31
+ 'SectionAsset' : [ 'storybase_story' , ' Story' ] ,
32
+ 'SectionTranslation' : [ 'storybase_story' , ' Story' ] ,
33
+ 'StoryTranslation' : [ 'storybase_story' , ' Story' ] ,
33
34
}
34
35
35
36
def _get_index (self , using , sender ):
@@ -39,7 +40,11 @@ def _get_index(self, using, sender):
39
40
This is needed because we update some indexes based on
40
41
changes on related models
41
42
"""
42
- index_class = self ._SENDER_MAP .get (sender .__name__ , sender )
43
+ if sender .__name__ in self ._SENDER_MAP :
44
+ index_class = get_model (* self ._SENDER_MAP [sender .__name__ ])
45
+ else :
46
+ index_class = sender
47
+
43
48
return self .connections [using ].get_unified_index ().get_index (index_class )
44
49
45
50
def handle_method (self , method_name , sender , instance , ** kwargs ):
@@ -92,6 +97,13 @@ def handle_location_save(self, sender, instance, **kwargs):
92
97
self .handle_method ('location_update_object' , sender , instance , ** kwargs )
93
98
94
99
def _setup_story (self ):
100
+ # HACK: Import here to avoid circular import
101
+ # See http://stackoverflow.com/a/17364366/386210
102
+ from storybase_asset .models import HtmlAssetTranslation
103
+ from storybase_geo .models import Location
104
+ from storybase_story .models import (SectionAsset , SectionTranslation ,
105
+ Story , StoryTranslation )
106
+
95
107
# Save signals
96
108
signals .post_save .connect (self .handle_save , sender = Story )
97
109
signals .post_save .connect (self .handle_translation_save ,
@@ -124,6 +136,10 @@ def _setup_story(self):
124
136
sender = StoryTranslation )
125
137
126
138
def _setup_help (self ):
139
+ # HACK: Import here to avoid circular import
140
+ # See http://stackoverflow.com/a/17364366/386210
141
+ from storybase_help .models import Help , HelpTranslation
142
+
127
143
signals .post_save .connect (self .handle_save , sender = Help )
128
144
signals .post_save .connect (self .handle_translation_save ,
129
145
sender = HelpTranslation )
@@ -132,6 +148,13 @@ def _setup_help(self):
132
148
sender = HelpTranslation )
133
149
134
150
def _teardown_story (self ):
151
+ # HACK: Import here to avoid circular import
152
+ # See http://stackoverflow.com/a/17364366/386210
153
+ from storybase_asset .models import HtmlAssetTranslation
154
+ from storybase_geo .models import Location
155
+ from storybase_story .models import (SectionAsset , SectionTranslation ,
156
+ Story , StoryTranslation )
157
+
135
158
signals .post_save .disconnect (self .handle_save , sender = Story )
136
159
signals .post_save .disconnect (self .handle_translation_save ,
137
160
sender = StoryTranslation )
@@ -160,6 +183,10 @@ def _teardown_story(self):
160
183
sender = StoryTranslation )
161
184
162
185
def _teardown_help (self ):
186
+ # HACK: Import here to avoid circular import
187
+ # See http://stackoverflow.com/a/17364366/386210
188
+ from storybase_help .models import Help , HelpTranslation
189
+
163
190
signals .post_save .disconnect (self .handle_save , sender = Help )
164
191
signals .post_save .disconnect (self .handle_translation_save ,
165
192
sender = HelpTranslation )
0 commit comments