Minor code refactoring

Improve coverage
This commit is contained in:
Iacopo Spalletti 2015-09-26 23:55:07 +02:00
parent e48e92decd
commit cae4595a99
5 changed files with 89 additions and 14 deletions

View file

@ -23,7 +23,7 @@ class TaggedFilterItem(object):
o con gli stessi tag di un model o un queryset o con gli stessi tag di un model o un queryset
""" """
tags = self._taglist(other_model, 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): def _taglist(self, other_model=None, queryset=None):
""" """
@ -31,21 +31,21 @@ class TaggedFilterItem(object):
o queryset passati come argomento o queryset passati come argomento
""" """
from taggit.models import TaggedItem from taggit.models import TaggedItem
filtro = None filter = None
if queryset is not None: if queryset is not None:
filtro = set() filter = set()
for item in queryset.all(): for item in queryset.all():
filtro.update(item.tags.all()) filter.update(item.tags.all())
filtro = set([tag.id for tag in filtro]) filter = set([tag.id for tag in filter])
elif other_model is not None: elif other_model is not None:
filtro = set(TaggedItem.objects.filter( filter = set(TaggedItem.objects.filter(
content_type__model=other_model.__name__.lower() content_type__model=other_model.__name__.lower()
).values_list('tag_id', flat=True)) ).values_list('tag_id', flat=True))
tags = set(TaggedItem.objects.filter( tags = set(TaggedItem.objects.filter(
content_type__model=self.model.__name__.lower() content_type__model=self.model.__name__.lower()
).values_list('tag_id', flat=True)) ).values_list('tag_id', flat=True))
if filtro is not None: if filter is not None:
tags = tags.intersection(filtro) tags = tags.intersection(filter)
return list(tags) return list(tags)
def tag_list(self, other_model=None, queryset=None): def tag_list(self, other_model=None, queryset=None):

View file

@ -242,6 +242,7 @@ class Post(ModelMeta, TranslatableModel):
@python_2_unicode_compatible @python_2_unicode_compatible
class BasePostPlugin(CMSPlugin): class BasePostPlugin(CMSPlugin):
app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True)
class Meta: class Meta:
abstract = True abstract = True
@ -257,11 +258,10 @@ class BasePostPlugin(CMSPlugin):
return posts return posts
def __str__(self): def __str__(self):
return force_text(self.latest_posts) return _('generic blog plugin')
class LatestPostsPlugin(BasePostPlugin): class LatestPostsPlugin(BasePostPlugin):
app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True)
latest_posts = models.IntegerField(_('articles'), default=get_setting('LATEST_POSTS'), latest_posts = models.IntegerField(_('articles'), default=get_setting('LATEST_POSTS'),
help_text=_('The number of latests ' help_text=_('The number of latests '
u'articles to be displayed.')) u'articles to be displayed.'))
@ -290,7 +290,6 @@ class LatestPostsPlugin(BasePostPlugin):
class AuthorEntriesPlugin(BasePostPlugin): class AuthorEntriesPlugin(BasePostPlugin):
app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True)
authors = models.ManyToManyField( authors = models.ManyToManyField(
dj_settings.AUTH_USER_MODEL, verbose_name=_('authors'), dj_settings.AUTH_USER_MODEL, verbose_name=_('authors'),
limit_choices_to={'djangocms_blog_post_author__publish': True} limit_choices_to={'djangocms_blog_post_author__publish': True}
@ -324,7 +323,6 @@ class AuthorEntriesPlugin(BasePostPlugin):
class GenericBlogPlugin(BasePostPlugin): class GenericBlogPlugin(BasePostPlugin):
app_config = AppHookConfigField(BlogConfig, verbose_name=_('app. config'), blank=True)
def __str__(self): class Meta:
return _('generic blog plugin') abstract = False

View file

@ -5,6 +5,8 @@ import os.path
from aldryn_apphooks_config.mixins import AppConfigMixin from aldryn_apphooks_config.mixins import AppConfigMixin
from django.contrib.auth import get_user_model 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.timezone import now
from django.utils.translation import get_language from django.utils.translation import get_language
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
@ -18,6 +20,19 @@ User = get_user_model()
class BaseBlogView(AppConfigMixin, ViewUrlMixin): 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): def get_queryset(self):
language = get_language() language = get_language()
queryset = self.model._default_manager.namespace( queryset = self.model._default_manager.namespace(

View file

@ -12,6 +12,8 @@ from django.contrib.auth.models import AnonymousUser
from django.contrib.messages.middleware import MessageMiddleware from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse 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.timezone import now
from django.utils.translation import get_language, override from django.utils.translation import get_language, override
from djangocms_helper.utils import CMS_30 from djangocms_helper.utils import CMS_30
@ -145,6 +147,8 @@ class AdminTest(BaseTest):
class ModelsTest(BaseTest): class ModelsTest(BaseTest):
def test_model_attributes(self): 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]['en'])
post = self._get_post(self._post_data[0]['it'], post, 'it') post = self._get_post(self._post_data[0]['it'], post, 'it')
post.main_image = self.create_filer_image_object() post.main_image = self.create_filer_image_object()
@ -242,6 +246,7 @@ class ModelsTest(BaseTest):
post2.save() post2.save()
self.assertEqual(len(Post.objects.available()), 2) self.assertEqual(len(Post.objects.available()), 2)
self.assertEqual(len(Post.objects.published()), 1) self.assertEqual(len(Post.objects.published()), 1)
self.assertEqual(len(Post.objects.published_future()), 2)
self.assertEqual(len(Post.objects.archived()), 0) self.assertEqual(len(Post.objects.archived()), 0)
# If post is published but end publishing date is in the past # 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()), set(tags_1))
self.assertEqual(set(Post.objects.tag_cloud(published=False)), set(tags)) 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): def test_plugin_latest(self):
post1 = self._get_post(self._post_data[0]['en']) post1 = self._get_post(self._post_data[0]['en'])
self._get_post(self._post_data[1]['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}): with self.settings(**{'SITE_ID': self.site_2.pk}):
self.assertEqual(len(Post.objects.all().on_site()), 2) self.assertEqual(len(Post.objects.all().on_site()), 2)
self.assertEqual(set(list(Post.objects.all().on_site())), set([post2, post3])) 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')

View file

@ -5,6 +5,7 @@ import os.path
from aldryn_apphooks_config.utils import get_app_instance from aldryn_apphooks_config.utils import get_app_instance
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ImproperlyConfigured
from django.http import Http404 from django.http import Http404
from django.utils.timezone import now from django.utils.timezone import now
from parler.tests.utils import override_parler_settings from parler.tests.utils import override_parler_settings
@ -70,6 +71,31 @@ class ViewTest(BaseTest):
response = view_obj.render_to_response(context) response = view_obj.render_to_response(context)
self.assertContains(response, context['post_list'][0].get_absolute_url()) 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): def test_post_list_view_fallback(self):
posts = self.get_posts() posts = self.get_posts()
pages = self.get_pages() pages = self.get_pages()