From cae4595a99aca19a89c99a30bee7bcdd45107c26 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sat, 26 Sep 2015 23:55:07 +0200 Subject: [PATCH] Minor code refactoring Improve coverage --- djangocms_blog/managers.py | 16 ++++++++-------- djangocms_blog/models.py | 10 ++++------ djangocms_blog/views.py | 15 +++++++++++++++ tests/test_models.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_views.py | 26 ++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 14 deletions(-) diff --git a/djangocms_blog/managers.py b/djangocms_blog/managers.py index f70a59d..644fbc6 100644 --- a/djangocms_blog/managers.py +++ b/djangocms_blog/managers.py @@ -23,7 +23,7 @@ class TaggedFilterItem(object): o con gli stessi tag di un model o un queryset """ tags = self._taglist(other_model, queryset) - return self.get_queryset().filter(taglist__in=tags) + return self.get_queryset().filter(tags__in=tags).distinct() def _taglist(self, other_model=None, queryset=None): """ @@ -31,21 +31,21 @@ class TaggedFilterItem(object): o queryset passati come argomento """ from taggit.models import TaggedItem - filtro = None + filter = None if queryset is not None: - filtro = set() + filter = set() for item in queryset.all(): - filtro.update(item.tags.all()) - filtro = set([tag.id for tag in filtro]) + filter.update(item.tags.all()) + filter = set([tag.id for tag in filter]) elif other_model is not None: - filtro = set(TaggedItem.objects.filter( + filter = set(TaggedItem.objects.filter( content_type__model=other_model.__name__.lower() ).values_list('tag_id', flat=True)) tags = set(TaggedItem.objects.filter( content_type__model=self.model.__name__.lower() ).values_list('tag_id', flat=True)) - if filtro is not None: - tags = tags.intersection(filtro) + if filter is not None: + tags = tags.intersection(filter) return list(tags) def tag_list(self, other_model=None, queryset=None): diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index d3dcd8d..0460447 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -242,6 +242,7 @@ class Post(ModelMeta, TranslatableModel): @python_2_unicode_compatible class BasePostPlugin(CMSPlugin): + app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True) class Meta: abstract = True @@ -257,11 +258,10 @@ class BasePostPlugin(CMSPlugin): return posts def __str__(self): - return force_text(self.latest_posts) + return _('generic blog plugin') class LatestPostsPlugin(BasePostPlugin): - app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True) latest_posts = models.IntegerField(_('articles'), default=get_setting('LATEST_POSTS'), help_text=_('The number of latests ' u'articles to be displayed.')) @@ -290,7 +290,6 @@ class LatestPostsPlugin(BasePostPlugin): class AuthorEntriesPlugin(BasePostPlugin): - app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True) authors = models.ManyToManyField( dj_settings.AUTH_USER_MODEL, verbose_name=_('authors'), limit_choices_to={'djangocms_blog_post_author__publish': True} @@ -324,7 +323,6 @@ class AuthorEntriesPlugin(BasePostPlugin): class GenericBlogPlugin(BasePostPlugin): - app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True) - def __str__(self): - return _('generic blog plugin') + class Meta: + abstract = False diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index d0ec7b5..70e138a 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -5,6 +5,8 @@ import os.path from aldryn_apphooks_config.mixins import AppConfigMixin from django.contrib.auth import get_user_model +from django.core.exceptions import ImproperlyConfigured +from django.core.urlresolvers import reverse from django.utils.timezone import now from django.utils.translation import get_language from django.views.generic import DetailView, ListView @@ -18,6 +20,19 @@ User = get_user_model() class BaseBlogView(AppConfigMixin, ViewUrlMixin): + def get_view_url(self): + if not self.view_url_name: + raise ImproperlyConfigured( + 'Missing `view_url_name` attribute on {0}'.format(self.__class__.__name__) + ) + + return reverse( + self.view_url_name, + args=self.args, + kwargs=self.kwargs, + current_app=self.namespace + ) + def get_queryset(self): language = get_language() queryset = self.model._default_manager.namespace( diff --git a/tests/test_models.py b/tests/test_models.py index 3004bbb..b9c59c9 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -12,6 +12,8 @@ from django.contrib.auth.models import AnonymousUser from django.contrib.messages.middleware import MessageMiddleware from django.contrib.sites.models import Site from django.core.urlresolvers import reverse +from django.utils.encoding import force_text +from django.utils.html import strip_tags from django.utils.timezone import now from django.utils.translation import get_language, override from djangocms_helper.utils import CMS_30 @@ -145,6 +147,8 @@ class AdminTest(BaseTest): class ModelsTest(BaseTest): def test_model_attributes(self): + self.get_pages() + post = self._get_post(self._post_data[0]['en']) post = self._get_post(self._post_data[0]['it'], post, 'it') post.main_image = self.create_filer_image_object() @@ -242,6 +246,7 @@ class ModelsTest(BaseTest): post2.save() self.assertEqual(len(Post.objects.available()), 2) self.assertEqual(len(Post.objects.published()), 1) + self.assertEqual(len(Post.objects.published_future()), 2) self.assertEqual(len(Post.objects.archived()), 0) # If post is published but end publishing date is in the past @@ -300,6 +305,15 @@ class ModelsTest(BaseTest): self.assertEqual(set(Post.objects.tag_cloud()), set(tags_1)) self.assertEqual(set(Post.objects.tag_cloud(published=False)), set(tags)) + tags1 = set(Post.objects.tag_list(Post)) + tags2 = set(Tag.objects.all()) + self.assertEqual(tags1, tags2) + + self.assertEqual( + list(Post.objects.tagged(queryset=Post.objects.filter(pk=post1.pk)).order_by('pk').values_list('pk')), + list(Post.objects.filter(pk__in=(post1.pk, post2.pk)).order_by('pk').values_list('pk')) + ) + def test_plugin_latest(self): post1 = self._get_post(self._post_data[0]['en']) self._get_post(self._post_data[1]['en']) @@ -384,3 +398,25 @@ class ModelsTest(BaseTest): with self.settings(**{'SITE_ID': self.site_2.pk}): self.assertEqual(len(Post.objects.all().on_site()), 2) self.assertEqual(set(list(Post.objects.all().on_site())), set([post2, post3])) + + def test_str_repr(self): + post1 = self._get_post(self._post_data[0]['en']) + post1.meta_description = '' + post1.main_image = None + post1.save() + + self.assertEqual(force_text(post1), post1.title) + self.assertEqual(post1.get_description(), strip_tags(post1.abstract)) + self.assertEqual(post1.get_image_full_url(), '') + self.assertEqual(post1.get_author(), self.user) + + self.assertEqual(force_text(post1.categories.first()), 'category 1') + + plugin = add_plugin(post1.content, 'BlogAuthorPostsPlugin', language='en', app_config=self.app_config_1) + self.assertEqual(force_text(plugin.__str__()), '5 latest articles by author') + + plugin = add_plugin(post1.content, 'BlogLatestEntriesPlugin', language='en', app_config=self.app_config_1) + self.assertEqual(force_text(plugin.__str__()), '5 latest articles by tag') + + plugin = add_plugin(post1.content, 'BlogArchivePlugin', language='en', app_config=self.app_config_1) + self.assertEqual(force_text(plugin.__str__()), 'generic blog plugin') diff --git a/tests/test_views.py b/tests/test_views.py index 818484a..5c45a32 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -5,6 +5,7 @@ import os.path from aldryn_apphooks_config.utils import get_app_instance from django.contrib.auth.models import AnonymousUser +from django.core.exceptions import ImproperlyConfigured from django.http import Http404 from django.utils.timezone import now from parler.tests.utils import override_parler_settings @@ -70,6 +71,31 @@ class ViewTest(BaseTest): response = view_obj.render_to_response(context) self.assertContains(response, context['post_list'][0].get_absolute_url()) + def test_get_view_url(self): + posts = self.get_posts() + pages = self.get_pages() + + # Test the custom version of get_view_url against the different namespaces + request = self.get_request(pages[1], 'en', AnonymousUser()) + view_obj_1 = PostListView() + view_obj_1.request = request + view_obj_1.args = () + view_obj_1.kwargs = {} + view_obj_1.namespace, view_obj_1.config = get_app_instance(request) + self.assertEqual(view_obj_1.get_view_url(), pages[1].get_absolute_url()) + + request = self.get_request(pages[2], 'en', AnonymousUser()) + view_obj_2 = PostListView() + view_obj_2.request = request + view_obj_2.args = () + view_obj_2.kwargs = {} + view_obj_2.namespace, view_obj_2.config = get_app_instance(request) + self.assertEqual(view_obj_2.get_view_url(), pages[2].get_absolute_url()) + + view_obj_2.view_url_name = None + with self.assertRaises(ImproperlyConfigured): + view_obj_2.get_view_url() + def test_post_list_view_fallback(self): posts = self.get_posts() pages = self.get_pages()