From 1f44a4605dae4015e1e6dcc78a83ee8c192e1ed1 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sat, 11 Oct 2014 12:23:36 +0200 Subject: [PATCH] Refactor settings --- djangocms_blog/admin.py | 9 ++-- djangocms_blog/cms_plugins.py | 4 +- djangocms_blog/models.py | 34 +++++++------- djangocms_blog/settings.py | 83 +++++++++++++++++++++-------------- djangocms_blog/views.py | 17 ++++--- tests/test_models.py | 82 +++++++++++++++++----------------- tests/test_views.py | 2 - 7 files changed, 122 insertions(+), 109 deletions(-) diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index d4475ef..7fc10af 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -7,6 +7,7 @@ from django.conf import settings from parler.admin import TranslatableAdmin from .models import Post, BlogCategory +from .settings import get_setting class BlogCategoryAdmin(EnhancedModelAdminMixin, TranslatableAdmin): @@ -47,11 +48,10 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdmin, ] def get_fieldsets(self, request, obj=None): - from .settings import BLOG_USE_PLACEHOLDER, BLOG_MULTISITE fsets = deepcopy(self._fieldsets) - if not BLOG_USE_PLACEHOLDER: + if not get_setting('USE_PLACEHOLDER'): fsets[0][1]['fields'].append('post_text') - if BLOG_MULTISITE: + if get_setting('MULTISITE'): fsets[1][1]['fields'][0].append('sites') if request.user.is_superuser: fsets[1][1]['fields'][0].append('author') @@ -61,8 +61,7 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdmin, return {'slug': ('title',)} def save_model(self, request, obj, form, change): - from .settings import BLOG_AUTHOR_AUTO - if not obj.author_id and BLOG_AUTHOR_AUTO: + if not obj.author_id and get_setting('AUTHOR_AUTO'): obj.author = request.user super(PostAdmin, self).save_model(request, obj, form, change) diff --git a/djangocms_blog/cms_plugins.py b/djangocms_blog/cms_plugins.py index 6bb6357..91ce5c5 100644 --- a/djangocms_blog/cms_plugins.py +++ b/djangocms_blog/cms_plugins.py @@ -7,7 +7,7 @@ from cms.plugin_pool import plugin_pool from .models import AuthorEntriesPlugin, LatestPostsPlugin, Post, BlogCategory from .forms import LatestEntriesForm -from .settings import BLOG_POSTS_LIST_TRUNCWORDS_COUNT +from .settings import get_setting class BlogPlugin(CMSPluginBase): @@ -24,7 +24,7 @@ class BlogLatestEntriesPlugin(BlogPlugin): def render(self, context, instance, placeholder): context['instance'] = instance - context['TRUNCWORDS_COUNT'] = BLOG_POSTS_LIST_TRUNCWORDS_COUNT + context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT') return context diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 0e98aaa..9ca9fd6 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -16,7 +16,7 @@ from parler.models import TranslatableModel, TranslatedFields from parler.managers import TranslationManager from taggit_autosuggest.managers import TaggableManager -from . import settings +from .settings import get_setting from .managers import GenericDateTaggedManager BLOG_CURRENT_POST_IDENTIFIER = 'djangocms_post_current' @@ -88,7 +88,7 @@ class Post(ModelMeta, TranslatableModel): blank=True, null=True) enable_comments = models.BooleanField( verbose_name=_(u'Enable comments on post'), - default=settings.BLOG_ENABLE_COMMENTS + default=get_setting('ENABLE_COMMENTS') ) sites = models.ManyToManyField(Site, verbose_name=_(u'Site(s'), blank=True, null=True, help_text=_(u'Select sites in which to show the post. If none is set it will be' @@ -124,17 +124,17 @@ class Post(ModelMeta, TranslatableModel): 'keywords': 'get_keywords', 'locale': None, 'image': 'get_image_full_url', - 'object_type': settings.BLOG_TYPE, - 'og_type': settings.BLOG_FB_TYPE, - 'og_app_id': settings.BLOG_FB_APPID, - 'og_profile_id': settings.BLOG_FB_PROFILE_ID, - 'og_publisher': settings.BLOG_FB_PUBLISHER, - 'og_author_url': settings.BLOG_FB_AUTHOR_URL, - 'twitter_type': settings.BLOG_TWITTER_TYPE, - 'twitter_site': settings.BLOG_TWITTER_SITE, - 'twitter_author': settings.BLOG_TWITTER_AUTHOR, - 'gplus_type': settings.BLOG_GPLUS_TYPE, - 'gplus_author': settings.BLOG_GPLUS_AUTHOR, + 'object_type': get_setting('TYPE'), + 'og_type': get_setting('FB_TYPE'), + 'og_app_id': get_setting('FB_APPID'), + 'og_profile_id': get_setting('FB_PROFILE_ID'), + 'og_publisher': get_setting('FB_PUBLISHER'), + 'og_author_url': get_setting('FB_AUTHOR_URL'), + 'twitter_type': get_setting('TWITTER_TYPE'), + 'twitter_site': get_setting('TWITTER_SITE'), + 'twitter_author': get_setting('TWITTER_AUTHOR'), + 'gplus_type': get_setting('GPLUS_TYPE'), + 'gplus_author': get_setting('GPLUS_AUTHOR'), 'published_time': 'date_published', 'modified_time': 'date_modified', 'expiration_time': 'date_published_end', @@ -199,13 +199,13 @@ class Post(ModelMeta, TranslatableModel): if self.main_image_thumbnail_id: return self.main_image_thumbnail.as_dict else: - return settings.BLOG_IMAGE_THUMBNAIL_SIZE + return get_setting('IMAGE_THUMBNAIL_SIZE') def full_image_options(self): if self.main_image_full_id: return self.main_image_full.as_dict else: - return settings.BLOG_IMAGE_FULL_SIZE + return get_setting('IMAGE_FULL_SIZE') def get_full_url(self): return self.make_full_url(self.get_absolute_url()) @@ -213,7 +213,7 @@ class Post(ModelMeta, TranslatableModel): class LatestPostsPlugin(CMSPlugin): - latest_posts = models.IntegerField(_(u'Articles'), default=settings.BLOG_LATEST_POSTS, + latest_posts = models.IntegerField(_(u'Articles'), default=get_setting('LATEST_POSTS'), help_text=_('The number of latests articles to be displayed.')) tags = models.ManyToManyField('taggit.Tag', blank=True, help_text=_('Show only the blog articles tagged with chosen tags.')) @@ -240,7 +240,7 @@ class AuthorEntriesPlugin(CMSPlugin): limit_choices_to={'djangocms_blog_post_author__publish': True} ) latest_posts = models.IntegerField( - _(u'Articles'), default=settings.BLOG_LATEST_POSTS, + _(u'Articles'), default=get_setting('LATEST_POSTS'), help_text=_('The number of author articles to be displayed.') ) diff --git a/djangocms_blog/settings.py b/djangocms_blog/settings.py index 211fc90..a19a6b4 100644 --- a/djangocms_blog/settings.py +++ b/djangocms_blog/settings.py @@ -1,37 +1,54 @@ # -*- coding: utf-8 -*- -from django.conf import settings -from meta_mixin import settings as meta_settings -BLOG_IMAGE_THUMBNAIL_SIZE = getattr(settings, 'BLOG_IMAGE_THUMBNAIL_SIZE', { - 'size': '120x120', - 'crop': True, - 'upscale': False -}) -BLOG_IMAGE_FULL_SIZE = getattr(settings, 'BLOG_IMAGE_FULL_SIZE', { - 'size': '640x120', - 'crop': True, - 'upscale': False -}) +def get_setting(name): + from django.conf import settings + from meta_mixin import settings as meta_settings -BLOG_TAGCLOUD_MIN = getattr(settings, 'BLOG_TAGCLOUD_MIN', 1) -BLOG_TAGCLOUD_MAX = getattr(settings, 'BLOG_TAGCLOUD_MAX', 10) -BLOG_PAGINATION = getattr(settings, 'BLOG_PAGINATION', 10) -BLOG_LATEST_POSTS = getattr(settings, 'BLOG_LATEST_POSTS', 5) -BLOG_POSTS_LIST_TRUNCWORDS_COUNT = getattr(settings, 'BLOG_POSTS_LIST_TRUNCWORDS_COUNT', 100) -BLOG_TYPE = getattr(settings, 'BLOG_TYPE', 'Article') -BLOG_FB_TYPE = getattr(settings, 'BLOG_FB_TYPE', 'Article') -BLOG_FB_APPID = getattr(settings, 'BLOG_FB_APPID', meta_settings.FB_APPID) -BLOG_FB_PROFILE_ID = getattr(settings, 'BLOG_FB_PROFILE_ID', meta_settings.FB_PROFILE_ID) -BLOG_FB_PUBLISHER = getattr(settings, 'BLOG_FB_PUBLISHER', meta_settings.FB_PUBLISHER) -BLOG_FB_AUTHOR_URL = getattr(settings, 'BLOG_FB_AUTHOR_URL', 'get_author_url') -BLOG_FB_AUTHOR = getattr(settings, 'BLOG_FB_AUTHOR', 'get_author_name') -BLOG_TWITTER_TYPE = getattr(settings, 'BLOG_TWITTER_TYPE', 'Summary') -BLOG_TWITTER_SITE = getattr(settings, 'BLOG_TWITTER_SITE', meta_settings.TWITTER_SITE) -BLOG_TWITTER_AUTHOR = getattr(settings, 'BLOG_TWITTER_AUTHOR', 'get_author_twitter') -BLOG_GPLUS_TYPE = getattr(settings, 'BLOG_GPLUS_SCOPE_CATEGORY', 'Blog') -BLOG_GPLUS_AUTHOR = getattr(settings, 'BLOG_GPLUS_AUTHOR', 'get_author_gplus') -BLOG_ENABLE_COMMENTS = getattr(settings, 'BLOG_ENABLE_COMMENTS', True) -BLOG_USE_PLACEHOLDER = getattr(settings, 'BLOG_USE_PLACEHOLDER', True) -BLOG_MULTISITE = getattr(settings, 'BLOG_MULTISITE', True) -BLOG_AUTHOR_AUTO = getattr(settings, 'BLOG_AUTHOR_AUTO', True) \ No newline at end of file + default = { + 'BLOG_IMAGE_THUMBNAIL_SIZE': getattr(settings, 'BLOG_IMAGE_THUMBNAIL_SIZE', { + 'size': '120x120', + 'crop': True, + 'upscale': False + }), + + 'BLOG_IMAGE_FULL_SIZE': getattr(settings, 'BLOG_IMAGE_FULL_SIZE', { + 'size': '640x120', + 'crop': True, + 'upscale': False + }), + + 'BLOG_TAGCLOUD_MIN': getattr(settings, 'BLOG_TAGCLOUD_MIN', 1), + 'BLOG_TAGCLOUD_MAX': getattr(settings, 'BLOG_TAGCLOUD_MAX', 10), + 'BLOG_PAGINATION': getattr(settings, 'BLOG_PAGINATION', 10), + 'BLOG_LATEST_POSTS': getattr(settings, 'BLOG_LATEST_POSTS', 5), + 'BLOG_POSTS_LIST_TRUNCWORDS_COUNT': getattr(settings, + 'BLOG_POSTS_LIST_TRUNCWORDS_COUNT', + 100), + 'BLOG_TYPE': getattr(settings, 'BLOG_TYPE', 'Article'), + 'BLOG_FB_TYPE': getattr(settings, 'BLOG_FB_TYPE', 'Article'), + 'BLOG_FB_APPID': getattr(settings, 'BLOG_FB_APPID', + meta_settings.FB_APPID), + 'BLOG_FB_PROFILE_ID': getattr(settings, 'BLOG_FB_PROFILE_ID', + meta_settings.FB_PROFILE_ID), + 'BLOG_FB_PUBLISHER': getattr(settings, 'BLOG_FB_PUBLISHER', + meta_settings.FB_PUBLISHER), + 'BLOG_FB_AUTHOR_URL': getattr(settings, 'BLOG_FB_AUTHOR_URL', + 'get_author_url'), + 'BLOG_FB_AUTHOR': getattr(settings, 'BLOG_FB_AUTHOR', + 'get_author_name'), + 'BLOG_TWITTER_TYPE': getattr(settings, 'BLOG_TWITTER_TYPE', 'Summary'), + 'BLOG_TWITTER_SITE': getattr(settings, 'BLOG_TWITTER_SITE', + meta_settings.TWITTER_SITE), + 'BLOG_TWITTER_AUTHOR': getattr(settings, 'BLOG_TWITTER_AUTHOR', + 'get_author_twitter'), + 'BLOG_GPLUS_TYPE': getattr(settings, 'BLOG_GPLUS_SCOPE_CATEGORY', + 'Blog'), + 'BLOG_GPLUS_AUTHOR': getattr(settings, 'BLOG_GPLUS_AUTHOR', + 'get_author_gplus'), + 'BLOG_ENABLE_COMMENTS': getattr(settings, 'BLOG_ENABLE_COMMENTS', True), + 'BLOG_USE_PLACEHOLDER': getattr(settings, 'BLOG_USE_PLACEHOLDER', True), + 'BLOG_MULTISITE': getattr(settings, 'BLOG_MULTISITE', True), + 'BLOG_AUTHOR_AUTO': getattr(settings, 'BLOG_AUTHOR_AUTO', True), + } + return default['BLOG_%s' % name] \ No newline at end of file diff --git a/djangocms_blog/views.py b/djangocms_blog/views.py index 251ffe1..e4dd77e 100644 --- a/djangocms_blog/views.py +++ b/djangocms_blog/views.py @@ -8,8 +8,7 @@ from django.views.generic import ListView, DetailView from parler.views import ViewUrlMixin, TranslatableSlugMixin from .models import Post, BlogCategory, BLOG_CURRENT_POST_IDENTIFIER -from .settings import (BLOG_PAGINATION, BLOG_POSTS_LIST_TRUNCWORDS_COUNT, - BLOG_USE_PLACEHOLDER) +from .settings import get_setting User = get_user_model() @@ -32,12 +31,12 @@ class PostListView(BaseBlogView, ListView): model = Post context_object_name = 'post_list' template_name = 'djangocms_blog/post_list.html' - paginate_by = BLOG_PAGINATION + paginate_by = get_setting('PAGINATION') view_url_name = 'djangocms_blog:posts-latest' def get_context_data(self, **kwargs): context = super(PostListView, self).get_context_data(**kwargs) - context['TRUNCWORDS_COUNT'] = BLOG_POSTS_LIST_TRUNCWORDS_COUNT + context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT') return context @@ -51,7 +50,7 @@ class PostDetailView(TranslatableSlugMixin, BaseBlogView, DetailView): def get_context_data(self, **kwargs): context = super(PostDetailView, self).get_context_data(**kwargs) context['meta'] = self.get_object().as_meta() - context['use_placeholer'] = BLOG_USE_PLACEHOLDER + context['use_placeholer'] = get_setting('USE_PLACEHOLDER') setattr(self.request, BLOG_CURRENT_POST_IDENTIFIER, self.get_object()) return context @@ -63,7 +62,7 @@ class PostArchiveView(BaseBlogView, ListView): date_field = 'date_published' allow_empty = True allow_future = True - paginate_by = BLOG_PAGINATION + paginate_by = get_setting('PAGINATION') view_url_name = 'djangocms_blog:posts-archive' def get_queryset(self): @@ -86,7 +85,7 @@ class TaggedListView(BaseBlogView, ListView): model = Post context_object_name = 'post_list' template_name = 'djangocms_blog/post_list.html' - paginate_by = BLOG_PAGINATION + paginate_by = get_setting('PAGINATION') view_url_name = 'djangocms_blog:posts-tagged' def get_queryset(self): @@ -103,7 +102,7 @@ class AuthorEntriesView(BaseBlogView, ListView): model = Post context_object_name = 'post_list' template_name = 'djangocms_blog/post_list.html' - paginate_by = BLOG_PAGINATION + paginate_by = get_setting('PAGINATION') view_url_name = 'djangocms_blog:posts-authors' def get_queryset(self): @@ -122,7 +121,7 @@ class CategoryEntriesView(BaseBlogView, ListView): context_object_name = 'post_list' template_name = 'djangocms_blog/post_list.html' _category = None - paginate_by = BLOG_PAGINATION + paginate_by = get_setting('PAGINATION') view_url_name = 'djangocms_blog:posts-category' @property diff --git a/tests/test_models.py b/tests/test_models.py index 713438d..b8c99b2 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -13,7 +13,7 @@ import parler from taggit.models import Tag from djangocms_blog.models import Post -from djangocms_blog import settings +from djangocms_blog.settings import get_setting from . import BaseTest @@ -25,21 +25,20 @@ class AdminTest(BaseTest): request = self.get_page_request('/', self.user_staff, r'/en/blog/', edit=False) post_admin = admin.site._registry[Post] - settings.BLOG_USE_PLACEHOLDER = True - fsets = post_admin.get_fieldsets(request) - self.assertFalse('post_text' in fsets[0][1]['fields']) - settings.BLOG_USE_PLACEHOLDER = False - fsets = post_admin.get_fieldsets(request) - self.assertTrue('post_text' in fsets[0][1]['fields']) - settings.BLOG_USE_PLACEHOLDER = True + with self.settings(BLOG_USE_PLACEHOLDER=True): + fsets = post_admin.get_fieldsets(request) + self.assertFalse('post_text' in fsets[0][1]['fields']) - settings.BLOG_MULTISITE = True - fsets = post_admin.get_fieldsets(request) - self.assertTrue('sites' in fsets[1][1]['fields'][0]) - settings.BLOG_MULTISITE = False - fsets = post_admin.get_fieldsets(request) - self.assertFalse('sites' in fsets[1][1]['fields'][0]) - settings.BLOG_MULTISITE = True + with self.settings(BLOG_USE_PLACEHOLDER=False): + fsets = post_admin.get_fieldsets(request) + self.assertTrue('post_text' in fsets[0][1]['fields']) + + with self.settings(BLOG_MULTISITE=True): + fsets = post_admin.get_fieldsets(request) + self.assertTrue('sites' in fsets[1][1]['fields'][0]) + with self.settings(BLOG_MULTISITE=False): + fsets = post_admin.get_fieldsets(request) + self.assertFalse('sites' in fsets[1][1]['fields'][0]) request = self.get_page_request('/', self.user, r'/en/blog/', edit=False) fsets = post_admin.get_fieldsets(request) @@ -49,30 +48,31 @@ class AdminTest(BaseTest): page1, page2 = self.get_pages() request = self.get_page_request('/', self.user_staff, r'/en/blog/', edit=False) data = deepcopy(self.data['en'][0]) - 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, 1) - settings.BLOG_AUTHOR_AUTO = 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) - settings.BLOG_AUTHOR_AUTO = True + with self.settings(BLOG_AUTHOR_AUTO=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, 1) + + with self.settings(BLOG_AUTHOR_AUTO=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) class ModelsTest(BaseTest): @@ -84,7 +84,7 @@ class ModelsTest(BaseTest): post.save() post.set_current_language('en') meta_en = post.as_meta() - self.assertEqual(meta_en.og_type, settings.BLOG_FB_TYPE) + self.assertEqual(meta_en.og_type, get_setting('FB_TYPE')) self.assertEqual(meta_en.title, post.title) self.assertTrue(meta_en.url.endswith(post.get_absolute_url())) self.assertEqual(meta_en.description, post.meta_description) @@ -119,8 +119,8 @@ class ModelsTest(BaseTest): self.assertEqual(post.get_full_url(), 'http://example.com%s' % url_it) self.assertEqual(post.get_image_full_url(), 'http://example.com%s' % post.main_image.url) - self.assertEqual(post.thumbnail_options(), settings.BLOG_IMAGE_THUMBNAIL_SIZE) - self.assertEqual(post.full_image_options(), settings.BLOG_IMAGE_FULL_SIZE) + self.assertEqual(post.thumbnail_options(), get_setting('IMAGE_THUMBNAIL_SIZE')) + self.assertEqual(post.full_image_options(), get_setting('IMAGE_FULL_SIZE')) post.main_image_thumbnail = self.thumb_1 post.main_image_full = self.thumb_2 diff --git a/tests/test_views.py b/tests/test_views.py index 4f7da17..bd27829 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -4,8 +4,6 @@ from django.http import Http404 from django.utils.translation import activate from django.utils.timezone import now from parler.utils.context import switch_language -from cms.middleware.toolbar import ToolbarMiddleware -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,