Permalink
Browse files

Don't reindex stories if they don't have a translation.

Fixes #108

Story objects get saved before their translation is attached.  The
RealTimeSearchIndex was trying to reindex on the post_save signal, but
this was raising an exception because it was trying to access fields
that are fetched from the related translation object.

The fix was really simple.  In the RealTimeSearchIndex subclass,
implement ``should_update()`` and return false if no translation exists.
This causes the story instance to only be indexed when the translation
exists. The story is saved again as soon as the translation is attached,
so the signal handler is re-run, this time passing the
``should_update()`` test.
  • Loading branch information...
1 parent b654782 commit 3515f65a8ce750d47fa4dc01cc7dfb2672be6ef6 @ghing ghing committed May 15, 2012
Showing with 12 additions and 0 deletions.
  1. +12 −0 apps/storybase_story/search_indexes.py
@@ -8,6 +8,8 @@ class StoryIndex(RealTimeSearchIndex):
text = CharField(document=True, use_template=True)
author = FacetCharField(model_attr='author')
published = FacetDateTimeField(model_attr='published')
+ created = FacetDateTimeField(model_attr='created')
+ last_edited = FacetDateTimeField(model_attr='last_edited')
# TODO: Use a meta class to dynamically populate these from "official"
# tag sets
topic_ids = FacetMultiValueField()
@@ -29,6 +31,16 @@ def prepare_language_ids(self, obj):
def index_queryset(self):
return Story.objects.filter(status__exact='published')
+
+ def should_update(self, instance, **kwargs):
+ """
+ Determine if an object should be updated in the index.
+ """
+ should_update = True
+ translation_set = getattr(instance, instance.translation_set)
+ if translation_set.count() == 0:
+ should_update = False
+ return should_update
site.register(Story, StoryIndex)

0 comments on commit 3515f65

Please sign in to comment.