Merge pull request #145 from nephila/feature/rebase_tests
Improve tests
This commit is contained in:
commit
78d612a387
25 changed files with 417 additions and 426 deletions
35
.editorconfig
Normal file
35
.editorconfig
Normal file
|
@ -0,0 +1,35 @@
|
|||
# http://editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
max_line_length = 80
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
[*.rst]
|
||||
max_line_length = 80
|
||||
|
||||
[*.py]
|
||||
max_line_length = 100
|
||||
|
||||
[*.{scss,html}]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
max_line_length = 120
|
||||
|
||||
[*.js]
|
||||
indent_size = 2
|
||||
max_line_length = 120
|
||||
|
||||
[*.yml]
|
||||
indent_size = 2
|
||||
|
||||
[Makefile]
|
||||
indent_style = tab
|
|
@ -1,14 +1,12 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
import sys
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from tempfile import mkdtemp
|
||||
|
||||
gettext = lambda s: s
|
||||
|
||||
HELPER_SETTINGS = dict(
|
||||
NOSE_ARGS=[
|
||||
'-s',
|
||||
],
|
||||
ROOT_URLCONF='tests.test_utils.urls',
|
||||
INSTALLED_APPS=[
|
||||
'filer',
|
||||
|
@ -87,8 +85,6 @@ HELPER_SETTINGS = dict(
|
|||
FILE_UPLOAD_TEMP_DIR=mkdtemp(),
|
||||
SITE_ID=1
|
||||
)
|
||||
if 'test' in sys.argv or len(sys.argv) == 1:
|
||||
HELPER_SETTINGS['INSTALLED_APPS'].append('django_nose')
|
||||
|
||||
try:
|
||||
import admin_enhancer # pragma: no cover # NOQA
|
||||
|
@ -99,8 +95,6 @@ except ImportError:
|
|||
|
||||
def run():
|
||||
from djangocms_helper import runner
|
||||
if 'test' in sys.argv or len(sys.argv) == 1:
|
||||
sys.argv.append('--nose-runner')
|
||||
runner.cms('djangocms_blog')
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
from cms.admin.placeholderadmin import FrontendEditableAdminMixin, PlaceholderAdminMixin
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.app_base import CMSApp
|
||||
from cms.apphook_pool import apphook_pool
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.models.pluginmodel import CMSPlugin
|
||||
from cms.plugin_base import CMSPluginBase
|
||||
from cms.plugin_pool import plugin_pool
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.toolbar_base import CMSToolbar
|
||||
from cms.toolbar_pool import toolbar_pool
|
||||
from django.core.urlresolvers import reverse
|
||||
|
@ -21,14 +23,14 @@ class BlogToolbar(CMSToolbar):
|
|||
admin_menu.add_modal_item(_('Add post'), url=url)
|
||||
|
||||
current_post = getattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, None)
|
||||
if current_post and self.request.user.has_perm('djangocms_blog.change_post'): # pragma: no cover
|
||||
if current_post and self.request.user.has_perm('djangocms_blog.change_post'): # pragma: no cover # NOQA
|
||||
admin_menu.add_modal_item(_('Edit Post'), reverse(
|
||||
'admin:djangocms_blog_post_change', args=(current_post.pk,)),
|
||||
active=True)
|
||||
|
||||
def post_template_populate(self):
|
||||
current_post = getattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, None)
|
||||
if current_post and self.request.user.has_perm('djangocms_blog.change_post'): # pragma: no cover
|
||||
if current_post and self.request.user.has_perm('djangocms_blog.change_post'): # pragma: no cover # NOQA
|
||||
# removing page meta menu, if present, to avoid confusion
|
||||
try: # pragma: no cover
|
||||
import djangocms_page_meta # NOQA
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from heapq import nlargest
|
||||
from itertools import ifilter, repeat
|
||||
from operator import itemgetter
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from django.contrib.sites.models import Site
|
||||
from django.contrib.syndication.views import Feed
|
||||
from django.core.urlresolvers import reverse
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from taggit_autosuggest.widgets import TagAutoSuggest
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
try:
|
||||
from collections import Counter
|
||||
except ImportError:
|
||||
from .compat import Counter
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import django
|
||||
from django.contrib.sites.models import Site
|
||||
|
@ -10,6 +7,11 @@ from django.db import models
|
|||
from django.utils.timezone import now
|
||||
from parler.managers import TranslatableQuerySet, TranslationManager
|
||||
|
||||
try:
|
||||
from collections import Counter
|
||||
except ImportError:
|
||||
from .compat import Counter
|
||||
|
||||
|
||||
class TaggedFilterItem(object):
|
||||
|
||||
|
@ -34,8 +36,12 @@ class TaggedFilterItem(object):
|
|||
filtro.update(item.tags.all())
|
||||
filtro = set([tag.id for tag in filtro])
|
||||
elif other_model is not None:
|
||||
filtro = 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))
|
||||
filtro = 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)
|
||||
return list(tags)
|
||||
|
@ -144,7 +150,8 @@ class GenericDateTaggedManager(TaggedFilterItem, TranslationManager):
|
|||
|
||||
def get_months(self, queryset=None):
|
||||
"""
|
||||
Get months with aggregate count (how much posts is in the month). Results are ordered by date.
|
||||
Get months with aggregate count (how much posts is in the month).
|
||||
Results are ordered by date.
|
||||
"""
|
||||
if queryset is None:
|
||||
queryset = self.get_queryset()
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.menu_bases import CMSAttachMenu
|
||||
from django.db.models.signals import post_delete, post_save
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import get_language
|
||||
from django.utils.translation import get_language, ugettext_lazy as _
|
||||
from menus.base import Modifier, NavigationNode
|
||||
from menus.menu_pool import menu_pool
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.models import CMSPlugin, PlaceholderField
|
||||
from django.conf import settings as dj_settings
|
||||
from django.core.urlresolvers import reverse
|
||||
|
@ -7,8 +9,7 @@ from django.utils import timezone
|
|||
from django.utils.encoding import force_text, python_2_unicode_compatible
|
||||
from django.utils.html import escape, strip_tags
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import get_language
|
||||
from django.utils.translation import get_language, ugettext_lazy as _
|
||||
from djangocms_text_ckeditor.fields import HTMLField
|
||||
from filer.fields.image import FilerImageField
|
||||
from meta_mixin.models import ModelMeta
|
||||
|
@ -75,52 +76,52 @@ class Post(ModelMeta, TranslatableModel):
|
|||
Blog post
|
||||
"""
|
||||
author = models.ForeignKey(dj_settings.AUTH_USER_MODEL,
|
||||
verbose_name=_(u'author'), null=True, blank=True,
|
||||
verbose_name=_('author'), null=True, blank=True,
|
||||
related_name='djangocms_blog_post_author')
|
||||
|
||||
date_created = models.DateTimeField(_(u'created'), auto_now_add=True)
|
||||
date_modified = models.DateTimeField(_(u'last modified'), auto_now=True)
|
||||
date_published = models.DateTimeField(_(u'published Since'),
|
||||
date_created = models.DateTimeField(_('created'), auto_now_add=True)
|
||||
date_modified = models.DateTimeField(_('last modified'), auto_now=True)
|
||||
date_published = models.DateTimeField(_('published Since'),
|
||||
default=timezone.now)
|
||||
date_published_end = models.DateTimeField(_(u'published Until'), null=True,
|
||||
date_published_end = models.DateTimeField(_('published Until'), null=True,
|
||||
blank=True)
|
||||
publish = models.BooleanField(_(u'publish'), default=False)
|
||||
categories = models.ManyToManyField('djangocms_blog.BlogCategory', verbose_name=_(u'category'),
|
||||
publish = models.BooleanField(_('publish'), default=False)
|
||||
categories = models.ManyToManyField('djangocms_blog.BlogCategory', verbose_name=_('category'),
|
||||
related_name='blog_posts',)
|
||||
main_image = FilerImageField(verbose_name=_(u'main image'), blank=True, null=True,
|
||||
main_image = FilerImageField(verbose_name=_('main image'), blank=True, null=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name='djangocms_blog_post_image')
|
||||
main_image_thumbnail = models.ForeignKey('cmsplugin_filer_image.ThumbnailOption',
|
||||
verbose_name=_(u'main image thumbnail'),
|
||||
verbose_name=_('main image thumbnail'),
|
||||
related_name='djangocms_blog_post_thumbnail',
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True, null=True)
|
||||
main_image_full = models.ForeignKey('cmsplugin_filer_image.ThumbnailOption',
|
||||
verbose_name=_(u'main image full'),
|
||||
verbose_name=_('main image full'),
|
||||
related_name='djangocms_blog_post_full',
|
||||
on_delete=models.SET_NULL,
|
||||
blank=True, null=True)
|
||||
enable_comments = models.BooleanField(verbose_name=_(u'enable comments on post'),
|
||||
enable_comments = models.BooleanField(verbose_name=_('enable comments on post'),
|
||||
default=get_setting('ENABLE_COMMENTS'))
|
||||
sites = models.ManyToManyField('sites.Site', verbose_name=_(u'Site(s)'), blank=True,
|
||||
sites = models.ManyToManyField('sites.Site', verbose_name=_('Site(s)'), blank=True,
|
||||
null=True,
|
||||
help_text=_(u'Select sites in which to show the post. '
|
||||
help_text=_('Select sites in which to show the post. '
|
||||
u'If none is set it will be '
|
||||
u'visible in all the configured sites.'))
|
||||
|
||||
translations = TranslatedFields(
|
||||
title=models.CharField(_(u'title'), max_length=255),
|
||||
slug=models.SlugField(_(u'slug'), blank=True, db_index=True),
|
||||
abstract=HTMLField(_(u'abstract'), blank=True, default=''),
|
||||
meta_description=models.TextField(verbose_name=_(u'post meta description'),
|
||||
title=models.CharField(_('title'), max_length=255),
|
||||
slug=models.SlugField(_('slug'), blank=True, db_index=True),
|
||||
abstract=HTMLField(_('abstract'), blank=True, default=''),
|
||||
meta_description=models.TextField(verbose_name=_('post meta description'),
|
||||
blank=True, default=''),
|
||||
meta_keywords=models.TextField(verbose_name=_(u'post meta keywords'),
|
||||
meta_keywords=models.TextField(verbose_name=_('post meta keywords'),
|
||||
blank=True, default=''),
|
||||
meta_title=models.CharField(verbose_name=_(u'post meta title'),
|
||||
help_text=_(u'used in title tag and social sharing'),
|
||||
meta_title=models.CharField(verbose_name=_('post meta title'),
|
||||
help_text=_('used in title tag and social sharing'),
|
||||
max_length=255,
|
||||
blank=True, default=''),
|
||||
post_text=HTMLField(_(u'text'), default='', blank=True),
|
||||
post_text=HTMLField(_('text'), default='', blank=True),
|
||||
meta={'unique_together': (('language_code', 'slug'),)}
|
||||
)
|
||||
content = PlaceholderField('post_content', related_name='post_content')
|
||||
|
@ -174,14 +175,9 @@ class Post(ModelMeta, TranslatableModel):
|
|||
return reverse('djangocms_blog:post-detail', kwargs=kwargs)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.slug and self.title:
|
||||
self.slug = slugify(self.title)
|
||||
super(Post, self).save(*args, **kwargs)
|
||||
main_lang = self.get_current_language()
|
||||
for lang in self.get_available_languages():
|
||||
self.set_current_language(lang)
|
||||
if not self.slug and self.title:
|
||||
self.slug = slugify(self.title)
|
||||
self.set_current_language(main_lang)
|
||||
self.save_translations()
|
||||
|
||||
def get_title(self):
|
||||
title = self.safe_translation_getter('meta_title', any_language=True)
|
||||
|
@ -245,16 +241,19 @@ class BasePostPlugin(CMSPlugin):
|
|||
|
||||
class LatestPostsPlugin(BasePostPlugin):
|
||||
|
||||
latest_posts = models.IntegerField(_(u'articles'), default=get_setting('LATEST_POSTS'),
|
||||
help_text=_(u'The number of latests articles to be displayed.'))
|
||||
tags = TaggableManager(_(u'filter by tag'), blank=True,
|
||||
help_text=_(u'Show only the blog articles tagged with chosen tags.'),
|
||||
latest_posts = models.IntegerField(_('articles'), default=get_setting('LATEST_POSTS'),
|
||||
help_text=_('The number of latests '
|
||||
u'articles to be displayed.'))
|
||||
tags = TaggableManager(_('filter by tag'), blank=True,
|
||||
help_text=_('Show only the blog articles tagged with chosen tags.'),
|
||||
related_name='djangocms_blog_latest_post')
|
||||
categories = models.ManyToManyField('djangocms_blog.BlogCategory', blank=True, verbose_name=_(u'filter by category'),
|
||||
help_text=_(u'Show only the blog articles tagged with chosen categories.'))
|
||||
categories = models.ManyToManyField('djangocms_blog.BlogCategory', blank=True,
|
||||
verbose_name=_('filter by category'),
|
||||
help_text=_('Show only the blog articles tagged '
|
||||
u'with chosen categories.'))
|
||||
|
||||
def __str__(self):
|
||||
return _(u'%s latest articles by tag') % self.latest_posts
|
||||
return _('%s latest articles by tag') % self.latest_posts
|
||||
|
||||
def copy_relations(self, oldinstance):
|
||||
for tag in oldinstance.tags.all():
|
||||
|
@ -275,12 +274,12 @@ class AuthorEntriesPlugin(BasePostPlugin):
|
|||
limit_choices_to={'djangocms_blog_post_author__publish': True}
|
||||
)
|
||||
latest_posts = models.IntegerField(
|
||||
_(u'articles'), default=get_setting('LATEST_POSTS'),
|
||||
help_text=_(u'The number of author articles to be displayed.')
|
||||
_('articles'), default=get_setting('LATEST_POSTS'),
|
||||
help_text=_('The number of author articles to be displayed.')
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return _(u'%s latest articles by author') % self.latest_posts
|
||||
return _('%s latest articles by author') % self.latest_posts
|
||||
|
||||
def copy_relations(self, oldinstance):
|
||||
self.authors = oldinstance.authors.all()
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
|
||||
def get_setting(name):
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from django.contrib.sitemaps import Sitemap
|
||||
|
||||
from ..models import Post
|
||||
|
||||
|
||||
class BlogSitemap(Sitemap):
|
||||
changefreq = "never"
|
||||
changefreq = 'never'
|
||||
priority = 0.5
|
||||
|
||||
def items(self):
|
||||
|
|
|
@ -1,19 +1,32 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
from .feeds import LatestEntriesFeed, TagFeed
|
||||
from .views import (AuthorEntriesView, CategoryEntriesView, PostArchiveView, PostDetailView,
|
||||
PostListView, TaggedListView)
|
||||
from .views import (
|
||||
AuthorEntriesView, CategoryEntriesView, PostArchiveView, PostDetailView, PostListView,
|
||||
TaggedListView,
|
||||
)
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(r'^$', PostListView.as_view(), name='posts-latest'),
|
||||
url(r'^feed/$', LatestEntriesFeed(), name='posts-latest-feed'),
|
||||
url(r'^(?P<year>\d{4})/$', PostArchiveView.as_view(), name='posts-archive'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$', PostArchiveView.as_view(), name='posts-archive'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>\w[-\w]*)/$', PostDetailView.as_view(), name='post-detail'),
|
||||
url(r'^author/(?P<username>[\w\.@+-]+)/$', AuthorEntriesView.as_view(), name='posts-author'),
|
||||
url(r'^category/(?P<category>[\w\.@+-]+)/$', CategoryEntriesView.as_view(), name='posts-category'),
|
||||
url(r'^tag/(?P<tag>[-\w]+)/$', TaggedListView.as_view(), name='posts-tagged'),
|
||||
url(r'^tag/(?P<tag>[-\w]+)/feed/$', TagFeed(), name='posts-tagged-feed'),
|
||||
url(r'^$',
|
||||
PostListView.as_view(), name='posts-latest'),
|
||||
url(r'^feed/$',
|
||||
LatestEntriesFeed(), name='posts-latest-feed'),
|
||||
url(r'^(?P<year>\d{4})/$',
|
||||
PostArchiveView.as_view(), name='posts-archive'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/$',
|
||||
PostArchiveView.as_view(), name='posts-archive'),
|
||||
url(r'^(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>\w[-\w]*)/$',
|
||||
PostDetailView.as_view(), name='post-detail'),
|
||||
url(r'^author/(?P<username>[\w\.@+-]+)/$',
|
||||
AuthorEntriesView.as_view(), name='posts-author'),
|
||||
url(r'^category/(?P<category>[\w\.@+-]+)/$',
|
||||
CategoryEntriesView.as_view(), name='posts-category'),
|
||||
url(r'^tag/(?P<tag>[-\w]+)/$',
|
||||
TaggedListView.as_view(), name='posts-tagged'),
|
||||
url(r'^tag/(?P<tag>[-\w]+)/feed/$',
|
||||
TagFeed(), name='posts-tagged-feed'),
|
||||
)
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.urlresolvers import resolve
|
||||
from django.utils.timezone import now
|
||||
|
@ -16,10 +18,10 @@ 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.all().active_translations(language_code=language)
|
||||
if not getattr(self.request, 'toolbar', False) or not self.request.toolbar.edit_mode:
|
||||
queryset = queryset.published()
|
||||
return queryset.on_site()
|
||||
return queryset
|
||||
|
||||
def render_to_response(self, context, **response_kwargs):
|
||||
response_kwargs['current_app'] = resolve(self.request.path).namespace
|
||||
|
@ -46,6 +48,12 @@ class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView):
|
|||
slug_field = 'slug'
|
||||
view_url_name = 'djangocms_blog:post-detail'
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = self.model._default_manager.all()
|
||||
if not getattr(self.request, 'toolbar', False) or not self.request.toolbar.edit_mode:
|
||||
queryset = queryset.published()
|
||||
return queryset
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
# submit object to cms to get corrent language switcher and selected category behavior
|
||||
if hasattr(self.request, 'toolbar'):
|
||||
|
@ -138,7 +146,9 @@ class CategoryEntriesView(BaseBlogView, ListView):
|
|||
@property
|
||||
def category(self):
|
||||
if not self._category:
|
||||
self._category = BlogCategory.objects.active_translations(get_language(), slug=self.kwargs['category']).latest('pk')
|
||||
self._category = BlogCategory.objects.active_translations(
|
||||
get_language(), slug=self.kwargs['category']
|
||||
).get()
|
||||
return self._category
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
|
|
11
setup.cfg
11
setup.cfg
|
@ -1,6 +1,5 @@
|
|||
[flake8]
|
||||
exclude = .git,*.egg-info,build,dist,.tox,djangocms_blog/migrations/*,djangocms_blog/south_migrations/*
|
||||
ignore = E501
|
||||
exclude = *.egg-info,.git,.settings,.tox,build,dist,docs,requirements,tmp,*migrations*,*south_migrations*,tests,data
|
||||
max-line-length = 99
|
||||
|
||||
[metadata]
|
||||
|
@ -11,4 +10,10 @@ universal = 1
|
|||
|
||||
[isort]
|
||||
line_length = 99
|
||||
skip = migrations, south_migrations
|
||||
skip = migrations, south_migrations
|
||||
combine_as_imports = true
|
||||
default_section = THIRDPARTY
|
||||
include_trailing_comma = true
|
||||
known_first_party = djangocms_blog
|
||||
multi_line_output = 5
|
||||
not_skip = __init__.py
|
|
@ -2,22 +2,14 @@
|
|||
"""
|
||||
Tests for `djangocms_blog` module.
|
||||
"""
|
||||
import os
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
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 filer.models import File, Image
|
||||
from PIL import Image as PilImage, ImageDraw
|
||||
from six import StringIO
|
||||
from djangocms_helper.base_test import BaseTestCase
|
||||
|
||||
from djangocms_helper.utils import create_user
|
||||
from djangocms_blog.models import BlogCategory, Post
|
||||
|
||||
User = get_user_model()
|
||||
|
@ -27,17 +19,23 @@ def _get_cat_pk(lang, name):
|
|||
return lambda: BlogCategory.objects.translated(lang, name=name).get().pk
|
||||
|
||||
|
||||
class BaseTest(TestCase):
|
||||
class BaseTest(BaseTestCase):
|
||||
"""
|
||||
Base class with utility function
|
||||
"""
|
||||
request_factory = None
|
||||
user = None
|
||||
languages = get_language_list()
|
||||
category_1 = None
|
||||
thumb_1 = None
|
||||
thumb_2 = None
|
||||
|
||||
_pages_data = (
|
||||
{'en': {'title': 'page one', 'template': 'page.html', 'publish': True},
|
||||
'fr': {'title': 'page un', 'publish': True},
|
||||
'it': {'title': 'pagina uno', 'publish': True}},
|
||||
{'en': {'title': 'page two', 'template': 'page.html', 'publish': True},
|
||||
'fr': {'title': 'page deux', 'publish': True},
|
||||
'it': {'title': 'pagina due', 'publish': True}},
|
||||
)
|
||||
|
||||
data = {
|
||||
'it': [
|
||||
{'title': u'Primo post', 'abstract': u'<p>prima riga</p>',
|
||||
|
@ -81,11 +79,7 @@ class BaseTest(TestCase):
|
|||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.request_factory = RequestFactory()
|
||||
cls.user = create_user('admin', 'admin@admin.com', 'admin', is_staff=True, is_superuser=True)
|
||||
cls.user_staff = create_user('staff', 'staff@admin.com', 'staff', is_staff=True)
|
||||
cls.user_normal = create_user('normal', 'normal@admin.com', 'normal')
|
||||
cls.site_1 = Site.objects.get(pk=1)
|
||||
super(BaseTest, cls).setUpClass()
|
||||
cls.site_2 = Site.objects.create(domain='http://example2.com', name='example 2')
|
||||
|
||||
def setUp(self):
|
||||
|
@ -101,15 +95,12 @@ class BaseTest(TestCase):
|
|||
self.thumb_2 = ThumbnailOption.objects.create(
|
||||
name='main', width=200, height=200, crop=False, upscale=False
|
||||
)
|
||||
img = create_image()
|
||||
self.image_name = 'test_file.jpg'
|
||||
self.filename = os.path.join(settings.FILE_UPLOAD_TEMP_DIR,
|
||||
self.image_name)
|
||||
img.save(self.filename, 'JPEG')
|
||||
file_obj = DjangoFile(open(self.filename, 'rb'), name=self.image_name)
|
||||
self.img = Image.objects.create(owner=self.user,
|
||||
original_filename=self.image_name,
|
||||
file=file_obj)
|
||||
self.img = self.create_filer_image_object()
|
||||
|
||||
def tearDown(self):
|
||||
for post in Post.objects.all():
|
||||
post.delete()
|
||||
super(BaseTest, self).tearDown()
|
||||
|
||||
def _get_category(self, data, category=None, lang='en'):
|
||||
for k, v in data.items():
|
||||
|
@ -147,67 +138,6 @@ class BaseTest(TestCase):
|
|||
post.sites.add(site)
|
||||
return post
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
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()
|
||||
|
||||
def get_pages(self):
|
||||
from cms.api import create_page, create_title
|
||||
page = create_page(u'page one', 'fullwidth.html', language='en')
|
||||
page_2 = create_page(u'page two', 'fullwidth.html', language='en')
|
||||
create_title(language='fr', title=u'page un', page=page)
|
||||
create_title(language='it', title=u'pagina uno', page=page)
|
||||
for lang in self.languages:
|
||||
page.publish(lang)
|
||||
page_2.publish('en')
|
||||
return page.get_draft_object(), page_2.get_draft_object()
|
||||
|
||||
def get_request(self, page, lang):
|
||||
request = self.request_factory.get(page.get_path(lang))
|
||||
request.current_page = page
|
||||
request.user = self.user
|
||||
request.session = {}
|
||||
request.cookies = SimpleCookie()
|
||||
request.errors = StringIO()
|
||||
return request
|
||||
|
||||
def post_request(self, page, lang, data, path=None):
|
||||
if not path:
|
||||
path = page.get_path(lang)
|
||||
request = self.request_factory.post(path, data)
|
||||
request.current_page = page
|
||||
request.user = self.user
|
||||
request.session = {}
|
||||
request.cookies = SimpleCookie()
|
||||
request.errors = StringIO()
|
||||
request._dont_enforce_csrf_checks = True
|
||||
return request
|
||||
|
||||
def get_page_request(self, page, user, path=None, edit=False, lang_code='en'):
|
||||
from cms.middleware.toolbar import ToolbarMiddleware
|
||||
path = path or page and page.get_absolute_url()
|
||||
if edit:
|
||||
path += '?edit'
|
||||
request = RequestFactory().get(path)
|
||||
request.session = {}
|
||||
request.user = user
|
||||
request.LANGUAGE_CODE = lang_code
|
||||
if edit:
|
||||
request.GET = {'edit': None}
|
||||
else:
|
||||
request.GET = {'edit_off': None}
|
||||
request.current_page = page
|
||||
mid = ToolbarMiddleware()
|
||||
mid.process_request(request)
|
||||
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')
|
||||
|
@ -219,12 +149,3 @@ class BaseTest(TestCase):
|
|||
post2.main_image = self.img
|
||||
post2.save()
|
||||
return post1, post2
|
||||
|
||||
|
||||
def create_image(mode='RGB', size=(800, 600)):
|
||||
image = PilImage.new(mode, size)
|
||||
draw = ImageDraw.Draw(image)
|
||||
x_bit, y_bit = size[0] // 10, size[1] // 10
|
||||
draw.rectangle((x_bit, y_bit * 2, x_bit * 7, y_bit * 3), 'red')
|
||||
draw.rectangle((x_bit * 2, y_bit, x_bit * 3, y_bit * 8), 'red')
|
||||
return image
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import copy
|
||||
|
||||
from django.utils.translation import activate
|
||||
from djangocms_blog.views import CategoryEntriesView, PostDetailView
|
||||
from menus.menu_pool import menu_pool
|
||||
from parler.utils.context import switch_language
|
||||
|
||||
from djangocms_blog.views import CategoryEntriesView, PostDetailView
|
||||
|
||||
from . import BaseTest
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
import parler
|
||||
|
@ -12,11 +14,12 @@ 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, override
|
||||
from djangocms_blog.models import Post
|
||||
from djangocms_blog.settings import get_setting
|
||||
from djangocms_helper.utils import CMS_30
|
||||
from taggit.models import Tag
|
||||
|
||||
from djangocms_blog.models import Post
|
||||
from djangocms_blog.settings import get_setting
|
||||
|
||||
from . import BaseTest
|
||||
|
||||
|
||||
|
@ -54,61 +57,66 @@ class AdminTest(BaseTest):
|
|||
|
||||
def test_admin_auto_author(self):
|
||||
page1, page2 = self.get_pages()
|
||||
request = self.get_page_request('/', self.user_staff, r'/en/blog/', edit=False)
|
||||
data = deepcopy(self.data['en'][0])
|
||||
|
||||
with self.settings(BLOG_AUTHOR_DEFAULT=True):
|
||||
data['date_published_0'] = now().strftime('%Y-%m-%d')
|
||||
data['date_published_1'] = now().strftime('%H:%M:%S')
|
||||
data['categories'] = self.category_1.pk
|
||||
request = self.post_request(page1, 'en', data=data)
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
post_admin.add_view(request)
|
||||
self.assertEqual(Post.objects.count(), 1)
|
||||
self.assertEqual(Post.objects.get(translations__slug='first-post').author_id,
|
||||
request.user.pk)
|
||||
with self.login_user_context(self.user):
|
||||
with self.settings(BLOG_AUTHOR_DEFAULT=True):
|
||||
data['date_published_0'] = now().strftime('%Y-%m-%d')
|
||||
data['date_published_1'] = now().strftime('%H:%M:%S')
|
||||
data['categories'] = self.category_1.pk
|
||||
request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text')
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
response = post_admin.add_view(request)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(Post.objects.count(), 1)
|
||||
self.assertEqual(Post.objects.get(translations__slug='first-post').author_id,
|
||||
request.user.pk)
|
||||
|
||||
with self.settings(BLOG_AUTHOR_DEFAULT=False):
|
||||
data = deepcopy(self.data['en'][1])
|
||||
data['date_published_0'] = now().strftime('%Y-%m-%d')
|
||||
data['date_published_1'] = now().strftime('%H:%M:%S')
|
||||
data['categories'] = self.category_1.pk
|
||||
request = self.post_request(page1, 'en', data=data)
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
post_admin.add_view(request)
|
||||
self.assertEqual(Post.objects.count(), 2)
|
||||
self.assertEqual(Post.objects.get(translations__slug='second-post').author_id, None)
|
||||
with self.settings(BLOG_AUTHOR_DEFAULT=False):
|
||||
data = deepcopy(self.data['en'][1])
|
||||
data['date_published_0'] = now().strftime('%Y-%m-%d')
|
||||
data['date_published_1'] = now().strftime('%H:%M:%S')
|
||||
data['categories'] = self.category_1.pk
|
||||
request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text')
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
response = post_admin.add_view(request)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(Post.objects.count(), 2)
|
||||
self.assertEqual(Post.objects.get(translations__slug='second-post').author_id, None)
|
||||
|
||||
with self.settings(BLOG_AUTHOR_DEFAULT='staff'):
|
||||
data = deepcopy(self.data['en'][2])
|
||||
data['date_published_0'] = now().strftime('%Y-%m-%d')
|
||||
data['date_published_1'] = now().strftime('%H:%M:%S')
|
||||
data['categories'] = self.category_1.pk
|
||||
request = self.post_request(page1, 'en', data=data)
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
post_admin.add_view(request)
|
||||
self.assertEqual(Post.objects.count(), 3)
|
||||
self.assertEqual(Post.objects.get(translations__slug='third-post').author.username, 'staff')
|
||||
with self.settings(BLOG_AUTHOR_DEFAULT='staff'):
|
||||
data = deepcopy(self.data['en'][2])
|
||||
data['date_published_0'] = now().strftime('%Y-%m-%d')
|
||||
data['date_published_1'] = now().strftime('%H:%M:%S')
|
||||
data['categories'] = self.category_1.pk
|
||||
request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text')
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
response = post_admin.add_view(request)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(Post.objects.count(), 3)
|
||||
self.assertEqual(Post.objects.get(translations__slug='third-post').author.username, 'staff')
|
||||
|
||||
def test_admin_post_text(self):
|
||||
page1, page2 = self.get_pages()
|
||||
post = self._get_post(self.data['en'][0])
|
||||
|
||||
with self.settings(BLOG_USE_PLACEHOLDER=False):
|
||||
data = {'post_text': 'ehi text'}
|
||||
request = self.post_request(page1, 'en', data=data, path='/en/?edit_fields=post_text')
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
post_admin.edit_field(request, post.pk, 'en')
|
||||
modified_post = Post.objects.get(pk=post.pk)
|
||||
self.assertEqual(modified_post.safe_translation_getter('post_text'), data['post_text'])
|
||||
with self.login_user_context(self.user):
|
||||
with self.settings(BLOG_USE_PLACEHOLDER=False):
|
||||
data = {'post_text': 'ehi text'}
|
||||
request = self.post_request(page1, 'en', user=self.user, data=data, path='/en/?edit_fields=post_text')
|
||||
msg_mid = MessageMiddleware()
|
||||
msg_mid.process_request(request)
|
||||
post_admin = admin.site._registry[Post]
|
||||
response = post_admin.edit_field(request, post.pk, 'en')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
modified_post = Post.objects.language('en').get(pk=post.pk)
|
||||
self.assertEqual(modified_post.safe_translation_getter('post_text'), data['post_text'])
|
||||
|
||||
|
||||
class ModelsTest(BaseTest):
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import re
|
||||
|
||||
from cms.api import add_plugin
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template import RequestContext
|
||||
from django.template.loader import get_template
|
||||
from django.utils.timezone import now
|
||||
from djangocms_blog.models import BlogCategory
|
||||
from taggit.models import Tag
|
||||
|
||||
from djangocms_blog.models import BlogCategory
|
||||
|
||||
from . import BaseTest
|
||||
|
||||
|
||||
|
@ -26,15 +27,9 @@ class PluginTest(BaseTest):
|
|||
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en')
|
||||
tag = Tag.objects.get(slug='tag-1')
|
||||
plugin.tags.add(tag)
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
|
||||
|
||||
context = RequestContext(request)
|
||||
try:
|
||||
template = get_template('page.html').template
|
||||
with context.bind_template(template):
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
except AttributeError:
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
context = self.get_plugin_context(page1, 'en', plugin, edit=True)
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-1') > -1)
|
||||
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1)
|
||||
self.assertTrue(rendered.find('<p>first line</p>') > -1)
|
||||
|
@ -49,14 +44,9 @@ class PluginTest(BaseTest):
|
|||
post2.categories.add(category_2)
|
||||
plugin = add_plugin(ph, 'BlogLatestEntriesPlugin', language='en')
|
||||
plugin.categories.add(category_2)
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
|
||||
context = RequestContext(request)
|
||||
try:
|
||||
template = get_template('page.html').template
|
||||
with context.bind_template(template):
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
except AttributeError:
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
|
||||
context = self.get_plugin_context(page1, 'en', plugin, edit=True)
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
self.assertTrue(rendered.find('cms_plugin-djangocms_blog-post-abstract-2') > -1)
|
||||
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-category', kwargs={'category': category_2.slug})) > -1)
|
||||
self.assertTrue(rendered.find('<p>second post first line</p>') > -1)
|
||||
|
@ -74,14 +64,9 @@ class PluginTest(BaseTest):
|
|||
ph = page1.placeholders.get(slot='content')
|
||||
plugin = add_plugin(ph, 'BlogAuthorPostsPlugin', language='en')
|
||||
plugin.authors.add(self.user)
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
|
||||
context = RequestContext(request)
|
||||
try:
|
||||
template = get_template('page.html').template
|
||||
with context.bind_template(template):
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
except AttributeError:
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
|
||||
context = self.get_plugin_context(page1, 'en', plugin, edit=True)
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-author', kwargs={'username': self.user.get_username()})) > -1)
|
||||
self.assertTrue(rendered.find('2 articles') > -1)
|
||||
|
||||
|
@ -97,14 +82,8 @@ class PluginTest(BaseTest):
|
|||
post2.save()
|
||||
ph = page1.placeholders.get(slot='content')
|
||||
plugin = add_plugin(ph, 'BlogTagsPlugin', language='en')
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
|
||||
context = RequestContext(request)
|
||||
try:
|
||||
template = get_template('page.html').template
|
||||
with context.bind_template(template):
|
||||
rendered = plugin.render_plugin(context, ph).replace('\n', '')
|
||||
except AttributeError:
|
||||
rendered = plugin.render_plugin(context, ph).replace('\n', '')
|
||||
context = self.get_plugin_context(page1, 'en', plugin, edit=True)
|
||||
rendered = plugin.render_plugin(context, ph)
|
||||
for tag in Tag.objects.all():
|
||||
self.assertTrue(rendered.find(reverse('djangocms_blog:posts-tagged', kwargs={'tag': tag.slug})) > -1)
|
||||
if tag.slug == 'test-tag':
|
||||
|
@ -123,15 +102,9 @@ class PluginTest(BaseTest):
|
|||
post2.save()
|
||||
ph = page1.placeholders.get(slot='content')
|
||||
plugin = add_plugin(ph, 'BlogCategoryPlugin', language='en')
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
|
||||
plugin_class = plugin.get_plugin_class_instance()
|
||||
context = RequestContext(request)
|
||||
try:
|
||||
template = get_template('page.html').template
|
||||
with context.bind_template(template):
|
||||
context = plugin_class.render(context, plugin, ph)
|
||||
except AttributeError:
|
||||
context = plugin_class.render(context, plugin, ph)
|
||||
context = self.get_plugin_context(page1, 'en', plugin, edit=True)
|
||||
context = plugin_class.render(context, plugin, ph)
|
||||
self.assertTrue(context['categories'])
|
||||
self.assertEqual(list(context['categories']), [self.category_1])
|
||||
|
||||
|
@ -144,20 +117,15 @@ class PluginTest(BaseTest):
|
|||
post2.save()
|
||||
ph = page1.placeholders.get(slot='content')
|
||||
plugin = add_plugin(ph, 'BlogArchivePlugin', language='en')
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=True)
|
||||
plugin_class = plugin.get_plugin_class_instance()
|
||||
context = RequestContext(request)
|
||||
try:
|
||||
template = get_template('page.html').template
|
||||
with context.bind_template(template):
|
||||
context = plugin_class.render(context, plugin, ph)
|
||||
except AttributeError:
|
||||
context = plugin_class.render(context, plugin, ph)
|
||||
|
||||
context = self.get_plugin_context(page1, 'en', plugin, edit=True)
|
||||
context = plugin_class.render(context, plugin, ph)
|
||||
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
||||
self.assertEqual(context['dates'][0]['count'], 2)
|
||||
|
||||
post2.publish = False
|
||||
post2.save()
|
||||
context = plugin_class.render(RequestContext(request, {'request': request}), plugin, ph)
|
||||
context = plugin_class.render(context, plugin, ph)
|
||||
self.assertEqual(context['dates'][0]['date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
||||
self.assertEqual(context['dates'][0]['count'], 1)
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.toolbar.items import ModalItem
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from djangocms_blog.models import BLOG_CURRENT_POST_IDENTIFIER
|
||||
|
||||
from . import BaseTest
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from cms.utils.conf import get_cms_setting
|
||||
from django.conf import settings
|
||||
from django.conf.urls import include, patterns, url
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from django.contrib.auth.models import AnonymousUser
|
||||
from django.http import Http404
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import activate
|
||||
from djangocms_blog.feeds import LatestEntriesFeed, TagFeed
|
||||
from djangocms_blog.sitemaps import BlogSitemap
|
||||
from djangocms_blog.views import (AuthorEntriesView, CategoryEntriesView, PostArchiveView,
|
||||
PostDetailView, PostListView, TaggedListView)
|
||||
from parler.tests.utils import override_parler_settings
|
||||
from parler.utils.conf import add_default_language_settings
|
||||
from parler.utils.context import switch_language
|
||||
from parler.utils.context import smart_override, switch_language
|
||||
|
||||
from djangocms_blog.feeds import LatestEntriesFeed, TagFeed
|
||||
from djangocms_blog.sitemaps import BlogSitemap
|
||||
from djangocms_blog.views import (
|
||||
AuthorEntriesView, CategoryEntriesView, PostArchiveView, PostDetailView, PostListView,
|
||||
TaggedListView,
|
||||
)
|
||||
|
||||
from . import BaseTest
|
||||
|
||||
|
@ -21,43 +25,43 @@ class ViewTest(BaseTest):
|
|||
post1, post2 = self.get_posts()
|
||||
|
||||
request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False)
|
||||
activate('en')
|
||||
view_obj = PostListView()
|
||||
view_obj.request = request
|
||||
|
||||
self.assertEqual(list(view_obj.get_queryset()), [post1])
|
||||
with smart_override('en'):
|
||||
view_obj = PostListView()
|
||||
view_obj.request = request
|
||||
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=False)
|
||||
activate('en')
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 1)
|
||||
self.assertEqual(set(qs), set([post1]))
|
||||
self.assertEqual(list(view_obj.get_queryset()), [post1])
|
||||
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
view_obj.request = request
|
||||
self.assertEqual(set(view_obj.get_queryset()), set([post1, post2]))
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=False)
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 1)
|
||||
self.assertEqual(set(qs), set([post1]))
|
||||
|
||||
view_obj.kwargs = {}
|
||||
view_obj.object_list = view_obj.get_queryset()
|
||||
view_obj.paginate_by = 1
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
response = view_obj.render_to_response(context)
|
||||
self.assertContains(response, context['post_list'][0].get_absolute_url())
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
view_obj.request = request
|
||||
self.assertEqual(set(view_obj.get_queryset()), set([post1, post2]))
|
||||
|
||||
request = self.get_page_request(page1, self.user, r'/it/blog/', lang_code='it', edit=True)
|
||||
activate('it')
|
||||
view_obj.request = request
|
||||
view_obj.object_list = view_obj.get_queryset()
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertEqual(context['post_list'][0].title, 'Secondo post')
|
||||
response = view_obj.render_to_response(context)
|
||||
self.assertContains(response, context['post_list'][0].get_absolute_url())
|
||||
view_obj.kwargs = {}
|
||||
view_obj.object_list = view_obj.get_queryset()
|
||||
view_obj.paginate_by = 1
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
response = view_obj.render_to_response(context)
|
||||
self.assertContains(response, context['post_list'][0].get_absolute_url())
|
||||
|
||||
with smart_override('it'):
|
||||
request = self.get_page_request(page1, self.user, r'/it/blog/', lang='it', edit=True)
|
||||
view_obj.request = request
|
||||
view_obj.object_list = view_obj.get_queryset()
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertEqual(context['post_list'][0].title, 'Secondo post')
|
||||
response = view_obj.render_to_response(context)
|
||||
self.assertContains(response, context['post_list'][0].get_absolute_url())
|
||||
|
||||
def test_post_list_view_fallback(self):
|
||||
page1, page2 = self.get_pages()
|
||||
|
@ -75,123 +79,124 @@ class ViewTest(BaseTest):
|
|||
}
|
||||
}
|
||||
|
||||
view_obj = PostListView()
|
||||
request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True)
|
||||
activate('fr')
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {}
|
||||
view_obj.object_list = view_obj.get_queryset()
|
||||
view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertEqual(view_obj.get_queryset().count(), 2)
|
||||
|
||||
PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK)
|
||||
with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK):
|
||||
|
||||
with smart_override('fr'):
|
||||
view_obj = PostListView()
|
||||
request = self.get_page_request(page1, self.user, r'/fr/blog/', lang_code='fr', edit=True)
|
||||
activate('fr')
|
||||
request = self.get_page_request(page1, self.user, r'/fr/blog/', lang='fr', edit=True)
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {}
|
||||
view_obj.object_list = view_obj.get_queryset()
|
||||
view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertEqual(view_obj.get_queryset().count(), 0)
|
||||
self.assertEqual(view_obj.get_queryset().count(), 2)
|
||||
|
||||
PARLER_FALLBACK = add_default_language_settings(PARLER_FALLBACK)
|
||||
with override_parler_settings(PARLER_LANGUAGES=PARLER_FALLBACK):
|
||||
|
||||
view_obj = PostListView()
|
||||
request = self.get_page_request(page1, self.user, r'/fr/blog/', lang='fr', edit=True)
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {}
|
||||
view_obj.object_list = view_obj.get_queryset()
|
||||
view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertEqual(view_obj.get_queryset().count(), 0)
|
||||
|
||||
def test_post_detail_view(self):
|
||||
page1, page2 = self.get_pages()
|
||||
post1, post2 = self.get_posts()
|
||||
|
||||
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 smart_override('en'):
|
||||
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 smart_override('it'):
|
||||
with switch_language(post1, 'it'):
|
||||
request = self.get_page_request(page1, AnonymousUser(), r'/it/blog/', lang='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')
|
||||
|
||||
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')
|
||||
|
||||
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()
|
||||
post1, post2 = self.get_posts()
|
||||
|
||||
request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False)
|
||||
activate('en')
|
||||
view_obj = PostArchiveView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'year': now().year, 'month': now().month}
|
||||
with smart_override('en'):
|
||||
request = self.get_page_request(page1, AnonymousUser(), r'/en/blog/', edit=False)
|
||||
view_obj = PostArchiveView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'year': now().year, 'month': now().month}
|
||||
|
||||
# One post only, anonymous request
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 1)
|
||||
self.assertEqual(list(qs), [post1])
|
||||
# One post only, anonymous request
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 1)
|
||||
self.assertEqual(list(qs), [post1])
|
||||
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertEqual(context['archive_date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertEqual(context['archive_date'].date(), now().replace(year=now().year, month=now().month, day=1).date())
|
||||
|
||||
def test_category_entries_view(self):
|
||||
page1, page2 = self.get_pages()
|
||||
post1, post2 = self.get_posts()
|
||||
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
activate('en')
|
||||
view_obj = CategoryEntriesView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'category': 'category-1'}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 2)
|
||||
self.assertEqual(set(qs), set([post1, post2]))
|
||||
with smart_override('en'):
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
view_obj = CategoryEntriesView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'category': 'category-1'}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 2)
|
||||
self.assertEqual(set(qs), set([post1, post2]))
|
||||
|
||||
view_obj.paginate_by = 1
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['category'])
|
||||
self.assertEqual(context['category'], self.category_1)
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
view_obj.paginate_by = 1
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['category'])
|
||||
self.assertEqual(context['category'], self.category_1)
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
|
||||
def test_author_entries_view(self):
|
||||
page1, page2 = self.get_pages()
|
||||
post1, post2 = self.get_posts()
|
||||
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
activate('en')
|
||||
view_obj = AuthorEntriesView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'username': self.user.get_username()}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 2)
|
||||
self.assertEqual(set(qs), set([post1, post2]))
|
||||
with smart_override('en'):
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
view_obj = AuthorEntriesView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'username': self.user.get_username()}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 2)
|
||||
self.assertEqual(set(qs), set([post1, post2]))
|
||||
|
||||
view_obj.paginate_by = 1
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['author'])
|
||||
self.assertEqual(context['author'], self.user)
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
view_obj.paginate_by = 1
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['author'])
|
||||
self.assertEqual(context['author'], self.user)
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
|
||||
def test_taggedlist_view(self):
|
||||
page1, page2 = self.get_pages()
|
||||
|
@ -201,23 +206,23 @@ class ViewTest(BaseTest):
|
|||
post2.tags.add('tag 6', 'tag 2', 'tag 5', 'tag 8')
|
||||
post2.save()
|
||||
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
activate('en')
|
||||
view_obj = TaggedListView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'tag': 'tag-2'}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 2)
|
||||
self.assertEqual(set(qs), set([post1, post2]))
|
||||
with smart_override('en'):
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', edit=True)
|
||||
view_obj = TaggedListView()
|
||||
view_obj.request = request
|
||||
view_obj.kwargs = {'tag': 'tag-2'}
|
||||
qs = view_obj.get_queryset()
|
||||
self.assertEqual(qs.count(), 2)
|
||||
self.assertEqual(set(qs), set([post1, post2]))
|
||||
|
||||
view_obj.paginate_by = 1
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['tagged_entries'], 'tag-2')
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
view_obj.paginate_by = 1
|
||||
view_obj.object_list = qs
|
||||
context = view_obj.get_context_data(object_list=view_obj.object_list)
|
||||
self.assertTrue(context['tagged_entries'], 'tag-2')
|
||||
self.assertTrue(context['is_paginated'])
|
||||
self.assertEqual(list(context['post_list']), [post2])
|
||||
self.assertEqual(context['paginator'].count, 2)
|
||||
self.assertEqual(context['post_list'][0].title, 'Second post')
|
||||
|
||||
def test_feed(self):
|
||||
page1, page2 = self.get_pages()
|
||||
|
@ -230,22 +235,22 @@ class ViewTest(BaseTest):
|
|||
|
||||
feed = LatestEntriesFeed()
|
||||
self.assertEqual(list(feed.items()), [post1])
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang_code='en', edit=False)
|
||||
request = self.get_page_request(page1, self.user, r'/en/blog/', lang='en', edit=False)
|
||||
xml = feed(request)
|
||||
self.assertContains(xml, post1.get_absolute_url())
|
||||
self.assertContains(xml, 'Blog articles on example.com')
|
||||
|
||||
activate('it')
|
||||
post1.set_current_language('it')
|
||||
feed = LatestEntriesFeed()
|
||||
self.assertEqual(list(feed.items()), [post1])
|
||||
request = self.get_page_request(page1, self.user, r'/it/blog/', lang_code='it', edit=False)
|
||||
xml = feed(request)
|
||||
self.assertContains(xml, post1.get_absolute_url())
|
||||
self.assertContains(xml, 'Articoli del blog su example.com')
|
||||
with smart_override('it'):
|
||||
with switch_language(post1, 'it'):
|
||||
feed = LatestEntriesFeed()
|
||||
self.assertEqual(list(feed.items()), [post1])
|
||||
request = self.get_page_request(page1, self.user, r'/it/blog/', lang='it', edit=False)
|
||||
xml = feed(request)
|
||||
self.assertContains(xml, post1.get_absolute_url())
|
||||
self.assertContains(xml, 'Articoli del blog su example.com')
|
||||
|
||||
feed = TagFeed()
|
||||
self.assertEqual(list(feed.items('tag-2')), [post1])
|
||||
feed = TagFeed()
|
||||
self.assertEqual(list(feed.items('tag-2')), [post1])
|
||||
|
||||
def test_sitemap(self):
|
||||
post1, post2 = self.get_posts()
|
||||
|
|
2
tox.ini
2
tox.ini
|
@ -17,7 +17,7 @@ deps =
|
|||
|
||||
[testenv:isort]
|
||||
deps = isort
|
||||
commands = isort -c -rc djangocms_blog tests
|
||||
commands = isort -c -rc -df djangocms_blog tests
|
||||
skip_install = true
|
||||
|
||||
[testenv:pep8]
|
||||
|
|
Loading…
Reference in a new issue