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 #365 from aldryn/issue/plugins-queryset-filtering
Browse files Browse the repository at this point in the history
Article plugins: consider status of app page availability
  • Loading branch information
mikek committed Mar 11, 2016
2 parents f3d7224 + c401a74 commit 92b258b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
19 changes: 12 additions & 7 deletions aldryn_newsblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from aldryn_translation_tools.models import (
TranslationHelperMixin, TranslatedAutoSlugifyMixin,
)
from aldryn_newsblog.utils.utilities import get_valid_languages_from_request

from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin
Expand Down Expand Up @@ -379,10 +380,11 @@ def get_articles(self, request):
queryset = Article.objects
if not self.get_edit_mode(request):
queryset = queryset.published()
queryset = queryset.active_translations(get_current_language()).filter(
languages = get_valid_languages_from_request(
self.app_config.namespace, request)
queryset = queryset.translated(*languages).filter(
app_config=self.app_config,
is_featured=True,
)
is_featured=True)
return queryset[:self.article_count]

def __str__(self):
Expand Down Expand Up @@ -413,9 +415,10 @@ def get_articles(self, request):
queryset = Article.objects
if not self.get_edit_mode(request):
queryset = queryset.published()
queryset = queryset.active_translations(get_current_language()).filter(
app_config=self.app_config
)
languages = get_valid_languages_from_request(
self.app_config.namespace, request)
queryset = queryset.translated(*languages).filter(
app_config=self.app_config)
return queryset[:self.latest_articles]

def __str__(self):
Expand All @@ -434,7 +437,9 @@ def get_articles(self, article, request):
"""
Returns a queryset of articles that are related to the given article.
"""
qs = article.related.all()
languages = get_valid_languages_from_request(
article.app_config.namespace, request)
qs = article.related.translated(*languages)
if not self.get_edit_mode(request):
qs = qs.published()
return qs
Expand Down
50 changes: 47 additions & 3 deletions aldryn_newsblog/tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import datetime
import pytz

from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.utils.translation import force_text
from django.utils.translation import force_text, override

from aldryn_newsblog.models import NewsBlogConfig
from cms import api
Expand Down Expand Up @@ -205,6 +206,21 @@ def test_featured_articles_plugin(self):
for article in other_articles:
self.assertNotContains(response, article.title)

def test_featured_articles_plugin_unpublished_app_page(self):
with override('de'):
articles = [self.create_article(is_featured=True)
for _ in range(3)]

response = self.client.get(self.plugin_page.get_absolute_url())
for article in articles:
self.assertContains(response, article.title)

self.page.unpublish('de')
cache.clear()
response = self.client.get(self.plugin_page.get_absolute_url())
for article in articles:
self.assertNotContains(response, article.title)


class TestLatestArticlesPlugin(TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogLatestArticlesPlugin'
Expand All @@ -223,8 +239,22 @@ def test_latest_articles_plugin(self):
for article in another_articles:
self.assertNotContains(response, article.title)

def test_latest_articles_plugin_unpublished_app_page(self):
with override('de'):
articles = [self.create_article() for _ in range(3)]

class TestPrefixedLatestArticlesPlugin(TestLatestArticlesPlugin):
response = self.client.get(self.plugin_page.get_absolute_url())
for article in articles:
self.assertContains(response, article.title)

self.page.unpublish('de')
cache.clear()
response = self.client.get(self.plugin_page.get_absolute_url())
for article in articles:
self.assertNotContains(response, article.title)


class TestPrefixedLatestArticlesPlugin(TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogLatestArticlesPlugin'
plugin_params = {
"latest_articles": 7,
Expand Down Expand Up @@ -263,7 +293,15 @@ def test_related_articles_plugin(self):
a = self.create_article()
a.save()
main_article.related.add(a)
self.assertEquals(main_article.related.count(), 3)

another_language_articles = []
with override('de'):
for _ in range(4):
a = self.create_article()
main_article.related.add(a)
another_language_articles.append(a)

self.assertEquals(main_article.related.count(), 7)
unrelated = []
for _ in range(5):
unrelated.append(self.create_article())
Expand All @@ -274,6 +312,12 @@ def test_related_articles_plugin(self):
for article in unrelated:
self.assertNotContains(response, article.title)

self.page.unpublish('de')
cache.clear()
response = self.client.get(main_article.get_absolute_url())
for article in another_language_articles:
self.assertNotContains(response, article.title)


class TestTagsPlugin(TestAppConfigPluginsBase):
plugin_to_test = 'NewsBlogTagsPlugin'
Expand Down
16 changes: 16 additions & 0 deletions aldryn_newsblog/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.contrib.sites.models import Site
try:
from django.contrib.sites.shortcuts import get_current_site
except ImportError:
# Django 1.6
from django.contrib.sites.models import get_current_site
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db import models
from django.template import RequestContext
from django.test import RequestFactory
from django.utils import translation
try:
from django.utils.encoding import force_unicode
except ImportError:
Expand Down Expand Up @@ -174,6 +180,16 @@ def is_valid_namespace_for_language(namespace, language_code):
return is_valid_namespace(namespace)


def get_valid_languages_from_request(namespace, request):
language = translation.get_language_from_request(
request, check_path=True)
site_id = getattr(get_current_site(request), 'id', None)
return get_valid_languages(
namespace,
language_code=language,
site_id=site_id)


def get_valid_languages(namespace, language_code, site_id=None):
langs = [language_code]
if site_id is None:
Expand Down
16 changes: 3 additions & 13 deletions aldryn_newsblog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
from datetime import datetime, date
from dateutil.relativedelta import relativedelta

try:
from django.contrib.sites.shortcuts import get_current_site
except ImportError:
# Django 1.6
from django.contrib.sites.models import get_current_site
from django.db.models import Q
from django.http import (
Http404,
Expand All @@ -29,7 +24,7 @@
from aldryn_categories.models import Category
from aldryn_people.models import Person

from aldryn_newsblog.utils.utilities import get_valid_languages
from aldryn_newsblog.utils.utilities import get_valid_languages_from_request
from .models import Article
from .utils import add_prefix_to_path

Expand Down Expand Up @@ -80,13 +75,8 @@ def get_queryset(self):
class AppHookCheckMixin(object):

def dispatch(self, request, *args, **kwargs):
language = translation.get_language_from_request(
request, check_path=True)
site_id = getattr(get_current_site(request), 'id', None)
self.valid_languages = get_valid_languages(
self.namespace,
language_code=language,
site_id=site_id)
self.valid_languages = get_valid_languages_from_request(
self.namespace, request)
return super(AppHookCheckMixin, self).dispatch(
request, *args, **kwargs)

Expand Down

0 comments on commit 92b258b

Please sign in to comment.