Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #255 from czpython/feature/allow-disabling-automat…
Browse files Browse the repository at this point in the history
…ic-search-data

Feature/allow disabling automatic search data
  • Loading branch information
czpython committed Jul 11, 2015
2 parents f726513 + de1ba00 commit cc2b313
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
20 changes: 16 additions & 4 deletions aldryn_newsblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@
@python_2_unicode_compatible
@version_controlled_content
class Article(TranslationHelperMixin, TranslatableModel):
# when True, updates the article's search_data field
# whenever the article is saved or a plugin is saved
# on the article's content placeholder.
update_search_on_save = getattr(
settings,
'ALDRYN_NEWSBLOG_UPDATE_SEARCH_DATA_ON_SAVE',
True
)

translations = TranslatedFields(
title=models.CharField(_('title'), max_length=234),
slug=models.SlugField(
Expand Down Expand Up @@ -205,7 +214,8 @@ def get_search_data(self, language=None, request=None):

def save(self, *args, **kwargs):
# Update the search index
self.search_data = self.get_search_data()
if self.update_search_on_save:
self.search_data = self.get_search_data()

# Ensure there is an owner.
if self.app_config.create_authors and self.author is None:
Expand Down Expand Up @@ -512,14 +522,16 @@ def __str__(self):
return _('%s tags') % (self.app_config.get_app_title(), )


@receiver(post_save)
def update_seach_index(sender, instance, **kwargs):
@receiver(post_save, dispatch_uid='article_update_search_data')
def update_search_data(sender, instance, **kwargs):
"""
Upon detecting changes in a plugin used in an Article's content
(PlaceholderField), update the article's search_index so that we can
perform simple searches even without Haystack, etc.
"""
if issubclass(instance.__class__, CMSPlugin):
is_cms_plugin = issubclass(instance.__class__, CMSPlugin)

if Article.update_search_on_save and is_cms_plugin:
placeholder = (getattr(instance, '_placeholder_cache', None)
or instance.placeholder)
if hasattr(placeholder, '_attached_model_cache'):
Expand Down
48 changes: 48 additions & 0 deletions aldryn_newsblog/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,54 @@ def test_auto_new_author(self):
self.assertEquals(article.author.name,
u' '.join((user.first_name, user.last_name)))

def test_auto_search_data(self):
activate(self.language)

user = self.create_user()

lead_in = 'Hello! this text will be searchable.'

Article.update_search_on_save = True

article = Article.objects.create(
title=self.rand_str(),
owner=user,
lead_in=lead_in,
app_config=self.app_config,
publishing_date=now()
)
article.save()

search_data = article.get_search_data()

self.assertEquals(lead_in, search_data)
self.assertEquals(article.search_data, search_data)

def test_auto_search_data_off(self):
activate(self.language)
user = self.create_user()

lead_in = 'Hello! this text will not be searchable.'

Article.update_search_on_save = False

article = Article.objects.create(
title=self.rand_str(),
owner=user,
lead_in=lead_in,
app_config=self.app_config,
publishing_date=now()
)
article.save()

search_data = article.get_search_data()

# set it back to true
Article.update_search_on_save = True

self.assertEquals(lead_in, search_data)
self.assertNotEquals(article.search_data, search_data)

def test_has_content(self):
# Just make sure we have a known language
activate(self.language)
Expand Down

0 comments on commit cc2b313

Please sign in to comment.