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 #374 from aldryn/issue/plugin-language-filtering
Browse files Browse the repository at this point in the history
Plugin language filtering
  • Loading branch information
mikek committed Mar 24, 2016
2 parents e7b9949 + 6da76e8 commit e29e319
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions aldryn_newsblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ def get_articles(self, request):
queryset = queryset.published()
languages = get_valid_languages_from_request(
self.app_config.namespace, request)
if self.language not in languages:
return queryset.none()
queryset = queryset.translated(*languages).filter(
app_config=self.app_config,
is_featured=True)
Expand Down Expand Up @@ -417,6 +419,8 @@ def get_articles(self, request):
queryset = queryset.published()
languages = get_valid_languages_from_request(
self.app_config.namespace, request)
if self.language not in languages:
return queryset.none()
queryset = queryset.translated(*languages).filter(
app_config=self.app_config)
return queryset[:self.latest_articles]
Expand All @@ -439,6 +443,8 @@ def get_articles(self, article, request):
"""
languages = get_valid_languages_from_request(
article.app_config.namespace, request)
if self.language not in languages:
return Article.objects.none()
qs = article.related.translated(*languages)
if not self.get_edit_mode(request):
qs = qs.published()
Expand Down
42 changes: 39 additions & 3 deletions aldryn_newsblog/tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ def setUp(self):
namespace=self.rand_str())


class TestPluginLanguageHelperMixin(object):
def _test_plugin_languages_with_article(self, article):
"""Set up conditions to test plugin languages edge cases"""
# Add 'de' translation to one of the articles
title_de = 'title-de'
title_en = article.title
article.set_current_language('de')
article.title = title_de
article.save()

# Unpublish page with newsblog apphook
self.page.unpublish('en')
cache.clear()
response = self.client.get(self.plugin_page.get_absolute_url())

# This article should not be visible on 'en' page/plugin
self.assertNotContains(response, title_en)


class TestArchivePlugin(TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogArchivePlugin'

Expand Down Expand Up @@ -176,7 +195,8 @@ def test_categories_plugin(self):
self.assertRegexpMatches(response_content, needle2)


class TestFeaturedArticlesPlugin(TestAppConfigPluginsBase):
class TestFeaturedArticlesPlugin(TestPluginLanguageHelperMixin,
TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogFeaturedArticlesPlugin'
plugin_params = {
"article_count": 5,
Expand Down Expand Up @@ -221,8 +241,13 @@ def test_featured_articles_plugin_unpublished_app_page(self):
for article in articles:
self.assertNotContains(response, article.title)

def test_featured_articles_plugin_language(self):
article = self.create_article(is_featured=True)
self._test_plugin_languages_with_article(article)


class TestLatestArticlesPlugin(TestAppConfigPluginsBase):
class TestLatestArticlesPlugin(TestPluginLanguageHelperMixin,
TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogLatestArticlesPlugin'
plugin_params = {
"latest_articles": 7,
Expand Down Expand Up @@ -253,6 +278,10 @@ def test_latest_articles_plugin_unpublished_app_page(self):
for article in articles:
self.assertNotContains(response, article.title)

def test_latest_articles_plugin_language(self):
article = self.create_article()
self._test_plugin_languages_with_article(article)


class TestPrefixedLatestArticlesPlugin(TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogLatestArticlesPlugin'
Expand All @@ -270,7 +299,8 @@ def test_latest_articles_plugin(self):
self.assertContains(response, 'This is dummy latest articles plugin')


class TestRelatedArticlesPlugin(NewsBlogTestCase):
class TestRelatedArticlesPlugin(TestPluginLanguageHelperMixin,
NewsBlogTestCase):

def test_related_articles_plugin(self):
main_article = self.create_article(app_config=self.app_config)
Expand Down Expand Up @@ -318,6 +348,12 @@ def test_related_articles_plugin(self):
for article in another_language_articles:
self.assertNotContains(response, article.title)

def test_latest_articles_plugin_language(self):
main_article, related_article = [
self.create_article() for _ in range(2)]
main_article.related.add(related_article)
self._test_plugin_languages_with_article(related_article)


class TestTagsPlugin(TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogTagsPlugin'
Expand Down

0 comments on commit e29e319

Please sign in to comment.