diff --git a/djangocms_blog/managers.py b/djangocms_blog/managers.py index b9a9458..d2420ed 100644 --- a/djangocms_blog/managers.py +++ b/djangocms_blog/managers.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import django from django.contrib.sites.models import Site from django.db.models import Q @@ -124,9 +125,8 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager): return super(GenericDateTaggedManager, self).get_queryset(*args, **kwargs) except AttributeError: return super(GenericDateTaggedManager, self).get_query_set(*args, **kwargs) - - def get_query_set(self, *args, **kwargs): - return self.get_queryset(*args, **kwargs) + if django.VERSION < (1, 8): + get_query_set = get_queryset def published(self): return self.get_queryset().published() diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index 59c269c..bca3e13 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -18,7 +18,7 @@ class BaseBlogView(ViewUrlMixin): def get_queryset(self): language = get_language() - queryset = self.model._default_manager.active_translations(language_code=language) + queryset = self.model._default_manager.language(language_code=language) if not self.request.user.is_staff: queryset = queryset.published() return queryset.on_site() @@ -128,10 +128,7 @@ class CategoryEntriesView(BaseBlogView, ListView): @property def category(self): if not self._category: - language = get_language() - self._category = BlogCategory._default_manager.language(language).get( - translations__language_code=language, - translations__slug=self.kwargs['category']) + self._category = BlogCategory.objects.active_translations(get_language(), slug=self.kwargs['category']).last() return self._category def get_queryset(self): diff --git a/tests/__init__.py b/tests/__init__.py index d458d24..1c2b152 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,23 +2,23 @@ """ Tests for `djangocms_blog` module. """ -from django.contrib.sites.models import Site import os from cms.utils.i18n import get_language_list from cmsplugin_filer_image.models import ThumbnailOption from django.conf import settings from django.contrib.auth import get_user_model +from django.contrib.sites.models import Site from django.core.files import File as DjangoFile from django.http import SimpleCookie from django.test import TestCase, RequestFactory -from django.utils.translation import activate +from django.utils.translation import activate, override from filer.models import File, Image from PIL import Image as PilImage, ImageDraw from six import StringIO -from djangocms_blog.models import BlogCategory, Post from djangocms_helper.utils import create_user +from djangocms_blog.models import BlogCategory, Post User = get_user_model() @@ -71,13 +71,10 @@ class BaseTest(TestCase): def setUp(self): activate('en') super(BaseTest, self).setUp() - self.category_1 = BlogCategory.objects.create() - self.category_1.name = u'category 1' - self.category_1.save() - self.category_1.set_current_language('it') + self.category_1 = BlogCategory.objects.create(name=u'category 1') + self.category_1.set_current_language('it', initialize=True) self.category_1.name = u'categoria 1' self.category_1.save() - self.category_1.set_current_language('en') self.thumb_1 = ThumbnailOption.objects.create( name='base', width=100, height=100, crop=True, upscale=False ) @@ -96,19 +93,25 @@ class BaseTest(TestCase): def _get_post(self, data, post=None, lang='en', sites=None): if not post: - post = Post() - post.set_current_language(lang) - post.author = self.user - post.title = data['title'] - post.abstract = data['abstract'] - post.meta_description = data['description'] - post.meta_keywords = data['keywords'] - post.save() + post_data = { + 'author': self.user, + 'title': data['title'], + 'abstract': data['abstract'], + 'meta_description': data['description'], + 'meta_keywords': data['keywords'], + } + post = Post.objects.create(**post_data) + else: + post.set_current_language(lang) + post.title = data['title'] + post.abstract = data['abstract'] + post.meta_description = data['description'] + post.meta_keywords = data['keywords'] + post.save() post.categories.add(self.category_1) if sites: for site in sites: post.sites.add(site) - post.save() return post @classmethod @@ -116,6 +119,8 @@ class BaseTest(TestCase): User.objects.all().delete() def tearDown(self): + for post in Post.objects.all(): + post.delete() os.remove(self.filename) for f in File.objects.all(): f.delete() @@ -159,18 +164,16 @@ class BaseTest(TestCase): return request def get_posts(self, sites=None): - post1 = self._get_post(self.data['en'][0], sites=sites) - post1 = self._get_post(self.data['it'][0], post1, 'it') - post1.publish = True - post1.main_image = self.img - post1.save() - post1.set_current_language('en') - post2 = self._get_post(self.data['en'][1], sites=sites) - post2 = self._get_post(self.data['it'][1], post2, 'it') - post2.set_current_language('en') - post2.main_image = self.img - post2.save() - post2.set_current_language('en') + with override('en'): + post1 = self._get_post(self.data['en'][0], sites=sites) + post1 = self._get_post(self.data['it'][0], post1, 'it') + post1.publish = True + post1.main_image = self.img + post1.save() + post2 = self._get_post(self.data['en'][1], sites=sites) + post2 = self._get_post(self.data['it'][1], post2, 'it') + post2.main_image = self.img + post2.save() return post1, post2 diff --git a/tests/test_models.py b/tests/test_models.py index 4b003e4..9ee7527 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -5,7 +5,7 @@ from cms.utils.plugins import downcast_plugins from django.contrib.sites.models import Site from django.core.urlresolvers import reverse from django.utils.timezone import now -from django.utils.translation import get_language, activate, override +from django.utils.translation import get_language, override import parler from taggit.models import Tag diff --git a/tests/test_views.py b/tests/test_views.py index 9cbec46..69621b0 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -3,7 +3,8 @@ from django.contrib.auth.models import AnonymousUser from django.http import Http404 from django.utils.translation import activate from django.utils.timezone import now -from djangocms_blog.models import Post +from parler.utils.context import switch_language +from djangocms_blog.models import Post, BlogCategory from djangocms_blog.feeds import LatestEntriesFeed, TagFeed from djangocms_blog.sitemaps import BlogSitemap from djangocms_blog.views import (PostListView, PostDetailView, @@ -54,34 +55,33 @@ class ViewTest(BaseTest): page1, page2 = self.get_pages() post1, post2 = self.get_posts() - request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False) - activate('en') - view_obj = PostDetailView() - view_obj.request = request + with switch_language(post1, 'en'): + request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False) + view_obj = PostDetailView() + view_obj.request = request - with self.assertRaises(Http404): - view_obj.kwargs = {'slug': 'not-existing'} + with self.assertRaises(Http404): + view_obj.kwargs = {'slug': 'not-existing'} + post_obj = view_obj.get_object() + + view_obj.kwargs = {'slug': post1.slug} post_obj = view_obj.get_object() + self.assertEqual(post_obj, post1) + self.assertEqual(post_obj.language_code, 'en') - view_obj.kwargs = {'slug': post1.slug} - post_obj = view_obj.get_object() - self.assertEqual(post_obj, post1) - self.assertEqual(post_obj.language_code, 'en') + with switch_language(post1, 'it'): + request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang_code='it', edit=False) + view_obj.request = request + view_obj.kwargs = {'slug': post1.slug} + post_obj = view_obj.get_object() + self.assertEqual(post_obj, post1) + self.assertEqual(post_obj.language_code, 'it') - request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang_code='it', edit=False) - activate('it') - post1.set_current_language('it') - view_obj.request = request - view_obj.kwargs = {'slug': post1.slug} - post_obj = view_obj.get_object() - self.assertEqual(post_obj, post1) - self.assertEqual(post_obj.language_code, 'it') - - view_obj.object = post_obj - context = view_obj.get_context_data() - self.assertEqual(context['post'], post1) - self.assertEqual(context['post'].language_code, 'it') - self.assertTrue(context['meta']) + view_obj.object = post_obj + context = view_obj.get_context_data() + self.assertEqual(context['post'], post1) + self.assertEqual(context['post'].language_code, 'it') + self.assertTrue(context['meta']) def test_post_archive_view(self): page1, page2 = self.get_pages()