djangocms_blog/tests/base.py

232 lines
9.4 KiB
Python
Raw Normal View History

2015-10-18 11:16:43 +00:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
from copy import deepcopy
from django.contrib.auth import get_user_model
from django.contrib.sites.models import Site
2016-02-07 12:00:25 +00:00
from django.core.cache import cache
2015-10-18 11:16:43 +00:00
from djangocms_helper.base_test import BaseTestCase
2015-10-15 20:29:32 +00:00
from haystack import connections
from haystack.constants import DEFAULT_ALIAS
2015-10-18 11:16:43 +00:00
from parler.utils.context import smart_override
from djangocms_blog.cms_appconfig import BlogConfig
2016-09-11 18:58:15 +00:00
from djangocms_blog.cms_menus import BlogCategoryMenu, BlogNavModifier
from djangocms_blog.models import BlogCategory, Post, ThumbnailOption
2015-10-18 11:16:43 +00:00
User = get_user_model()
def _get_cat_pk(lang, name):
2016-02-07 12:00:25 +00:00
return lambda: BlogCategory.objects.language(lang).translated(lang, name=name).get().pk
2015-10-18 11:16:43 +00:00
class BaseTest(BaseTestCase):
"""
Base class with utility function
"""
category_1 = None
thumb_1 = None
thumb_2 = None
_pages_data = (
{'en': {'title': 'page one', 'template': 'blog.html', 'publish': True},
'fr': {'title': 'page un', 'publish': True},
'it': {'title': 'pagina uno', 'publish': True}},
{'en': {'title': 'page two', 'template': 'blog.html', 'publish': True,
'apphook': 'BlogApp', 'apphook_namespace': 'sample_app'},
'fr': {'title': 'page deux', 'publish': True},
'it': {'title': 'pagina due', 'publish': True}},
{'en': {'title': 'page three', 'template': 'blog.html', 'publish': True,
'apphook': 'BlogApp', 'apphook_namespace': 'sample_app2'},
'fr': {'title': 'page trois', 'publish': True},
'it': {'title': 'pagina tre', 'publish': True}},
)
_post_data = (
{'en': {'title': 'First post', 'abstract': '<p>first line</p>',
'description': 'This is the description', 'keywords': 'keyword1, keyword2',
'text': 'Post text', 'app_config': 'sample_app', 'publish': True},
'it': {'title': 'Primo post', 'abstract': '<p>prima riga</p>',
'description': 'Questa è la descrizione', 'keywords': 'keyword1, keyword2',
'text': 'Testo del post'},
},
{'en': {'title': 'Second post', 'abstract': '<p>second post first line</p>',
'description': 'Second post description', 'keywords': 'keyword3, keyword4',
'text': 'Second post text', 'app_config': 'sample_app', 'publish': False},
'it': {'title': 'Secondo post', 'abstract': '<p>prima riga del secondo post</p>',
'description': 'Descrizione del secondo post', 'keywords': 'keyword3, keyword4',
'text': 'Testo del secondo post', 'app_config': 'sample_app'},
},
{'en': {'title': 'Third post', 'abstract': '<p>third post first line</p>',
'description': 'third post description', 'keywords': 'keyword5, keyword6',
'text': 'Third post text', 'app_config': 'sample_app', 'publish': False},
'it': {'title': 'Terzo post', 'abstract': '<p>prima riga del terzo post</p>',
'description': 'Descrizione del terzo post', 'keywords': 'keyword5, keyword6',
'text': 'Testo del terzo post'},
},
2016-02-07 12:00:25 +00:00
{'en': {'title': 'Different appconfig',
'abstract': '<p>Different appconfig first line</p>',
2015-10-18 11:16:43 +00:00
'description': 'Different appconfig description', 'keywords': 'keyword5, keyword6',
'text': 'Different appconfig text', 'app_config': 'sample_app2', 'publish': True},
'it': {'title': 'Altro appconfig', 'abstract': '<p>prima riga del Altro appconfig</p>',
'description': 'Descrizione Altro appconfig', 'keywords': 'keyword5, keyword6',
'text': 'Testo del Altro appconfig'},
},
)
_categories_data = (
{'en': {'name': 'Very loud', 'app_config': 'sample_app'},
'it': {'name': 'Fortissimo'},
},
{'en': {'name': 'Very very silent', 'app_config': 'sample_app'},
'it': {'name': 'Pianississimo'},
},
{'en': {'name': 'Almost', 'app_config': 'sample_app'},
'it': {'name': 'Mezzo'},
},
{'en': {'name': 'Drums', 'app_config': 'sample_app2'},
'it': {'name': 'Tamburi'},
},
{'en': {'name': 'Guitars', 'app_config': 'sample_app2'},
'it': {'name': 'Chitarre'},
},
2016-02-07 12:00:25 +00:00
{'en': {'name': 'Loud', 'parent_id': _get_cat_pk('en', 'Almost'),
'app_config': 'sample_app'},
'it': {'name': 'Forte', 'parent_id': _get_cat_pk('it', 'Mezzo')},
},
{'en': {'name': 'Silent', 'parent_id': _get_cat_pk('en', 'Almost'),
'app_config': 'sample_app'},
},
2015-10-18 11:16:43 +00:00
)
@classmethod
def setUpClass(cls):
super(BaseTest, cls).setUpClass()
2016-05-17 21:17:00 +00:00
cls.thumb_1, __ = ThumbnailOption.objects.get_or_create(
2015-10-18 11:16:43 +00:00
name='base', width=100, height=100, crop=True, upscale=False
)
2016-05-17 21:17:00 +00:00
cls.thumb_2, __ = ThumbnailOption.objects.get_or_create(
2015-10-18 11:16:43 +00:00
name='main', width=200, height=200, crop=False, upscale=False
)
2016-05-17 21:17:00 +00:00
cls.app_config_1, __ = BlogConfig.objects.get_or_create(
namespace='sample_app'
2015-10-18 11:16:43 +00:00
)
2016-05-17 21:17:00 +00:00
cls.app_config_2, __ = BlogConfig.objects.get_or_create(
namespace='sample_app2'
2015-10-18 11:16:43 +00:00
)
2016-05-17 21:17:00 +00:00
cls.app_config_1.app_title = 'app1'
cls.app_config_1.object_name = 'Blog'
2015-10-18 11:16:43 +00:00
cls.app_config_1.app_data.config.paginate_by = 1
cls.app_config_1.app_data.config.send_knock_create = True
cls.app_config_1.app_data.config.send_knock_update = True
2015-10-18 11:16:43 +00:00
cls.app_config_1.save()
2016-05-17 21:17:00 +00:00
cls.app_config_2.app_title = 'app2'
cls.app_config_2.object_name = 'Article'
2015-10-18 11:16:43 +00:00
cls.app_config_2.app_data.config.paginate_by = 2
cls.app_config_2.app_data.config.send_knock_create = True
cls.app_config_2.app_data.config.send_knock_update = True
2015-10-18 11:16:43 +00:00
cls.app_config_2.save()
cls.app_configs = {
'sample_app': cls.app_config_1,
'sample_app2': cls.app_config_2,
}
2016-05-17 21:17:00 +00:00
cls.category_1 = BlogCategory.objects.create(
name='category 1', app_config=cls.app_config_1
)
2015-10-18 11:16:43 +00:00
cls.category_1.set_current_language('it', initialize=True)
cls.category_1.name = 'categoria 1'
cls.category_1.save()
2016-05-17 21:17:00 +00:00
cls.site_2, __ = Site.objects.get_or_create(domain='http://example2.com', name='example 2')
cls.site_3, __ = Site.objects.get_or_create(domain='http://example3.com', name='example 3')
2016-02-07 12:00:25 +00:00
cache.clear()
2015-10-18 11:16:43 +00:00
@classmethod
def tearDownClass(cls):
BlogConfig.objects.all().delete()
BlogCategory.objects.all().delete()
ThumbnailOption.objects.all().delete()
2016-05-17 21:17:00 +00:00
cache.clear()
super(BaseTest, cls).tearDownClass()
def tearDown(self):
self.user.sites.clear()
for post in Post.objects.all():
post.sites.clear()
post.tags.clear()
2016-06-26 13:33:00 +00:00
post.categories.clear()
cache.clear()
2016-05-17 21:17:00 +00:00
super(BaseTest, self).tearDown()
2015-10-18 11:16:43 +00:00
2018-01-14 11:54:44 +00:00
def get_nodes(self, menu_pool, request):
try:
nodes = menu_pool.get_renderer(request).get_nodes()
except AttributeError:
nodes = menu_pool.get_nodes(request)
return nodes
2015-10-18 11:16:43 +00:00
def _get_category(self, data, category=None, lang='en'):
data = deepcopy(data)
for k, v in data.items():
if hasattr(v, '__call__'):
data[k] = v()
if not category:
with smart_override(lang):
data['app_config'] = self.app_configs[data['app_config']]
category = BlogCategory.objects.create(**data)
else:
category.set_current_language(lang, initialize=True)
for attr, val in data.items():
setattr(category, attr, val)
category.save()
return category
def _get_post(self, data, post=None, lang='en', sites=None):
if not post:
post_data = {
'author': self.user,
'title': data['title'],
'abstract': data['abstract'],
'meta_description': data['description'],
'meta_keywords': data['keywords'],
'app_config': self.app_configs[data['app_config']]
}
post = Post.objects.create(**post_data)
else:
post.create_translation(
lang,
title=data['title'],
abstract=data['abstract'],
meta_description=data['description'],
meta_keywords=data['keywords']
)
post.categories.add(self.category_1)
if sites:
for site in sites:
post.sites.add(site)
return post
def get_posts(self, sites=None):
posts = []
2016-02-07 12:00:25 +00:00
cache.clear()
2015-10-18 11:16:43 +00:00
for post in self._post_data:
post1 = self._get_post(post['en'], sites=sites)
post1 = self._get_post(post['it'], post=post1, lang='it')
post1.publish = post['en']['publish']
post1.main_image = self.create_filer_image_object()
post1.save()
posts.append(post1)
return posts
2015-10-15 20:29:32 +00:00
def get_post_index(self):
search_conn = connections[DEFAULT_ALIAS]
unified_index = search_conn.get_unified_index()
index = unified_index.get_index(Post)
return index
2016-09-11 18:58:15 +00:00
def _reset_menus(self):
cache.clear()
BlogCategoryMenu._config = {}
BlogNavModifier._config = {}