Make the default author configurable

This commit is contained in:
Iacopo Spalletti 2014-10-11 12:46:39 +02:00
parent 86714126ba
commit 66a5e2c3ab
4 changed files with 33 additions and 11 deletions

View file

@ -155,20 +155,24 @@ this gist https://gist.github.com/yakky/11336204 as a base.
Settings
--------
* BLOG_ENABLE_COMMENTS: Whether to enable comments by default on posts;
while `djangocms_blog` does not ship any comment system, this flag can be used
while ``djangocms_blog`` does not ship any comment system, this flag can be used
to control the chosen comments framework; (default: True)
* BLOG_USE_PLACEHOLDER: Post content is managed via placeholder; if `False` a
* BLOG_USE_PLACEHOLDER: Post content is managed via placeholder; if ``False`` a
simple HTMLField is used; (default: True)
* BLOG_IMAGE_THUMBNAIL_SIZE: Size of the main image when shown on the post lists;
it's a dictionary with `size`, `crop` and `upscale` keys;
(default: `{'size': '120x120', 'crop': True,'upscale': False}`)
it's a dictionary with ``size``, ``crop`` and ``upscale`` keys;
(default: ``{'size': '120x120', 'crop': True,'upscale': False}``)
* BLOG_IMAGE_FULL_SIZE: Size of the main image when shown on the post detail;
it's a dictionary with `size`, `crop` and `upscale` keys;
(default: `{'size': '640x120', 'crop': True,'upscale': False}`)
it's a dictionary with ``size``, ``crop`` and ``upscale`` keys;
(default: ``{'size': '640x120', 'crop': True,'upscale': False}``)
* BLOG_PAGINATION: Number of post per page; (default: 10)
* BLOG_LATEST_POSTS: Default number of post in the **Latest post** plugin; (default: 5)
* BLOG_POSTS_LIST_TRUNCWORDS_COUNT: Default number of words shown for abstract in the post list; (default: 100)
* BLOG_MULTISITE: Add support for multisite setup
* BLOG_AUTHOR_DEFAULT: Use a default if not specified; if set to ``True`` the
current user is set as the default author, if set to ``False`` no default
author is set, if set to a string the user with the provided username is
used; (default: True)
Social media tags settings
++++++++++++++++++++++++++

View file

@ -4,6 +4,7 @@ from cms.admin.placeholderadmin import PlaceholderAdmin, FrontendEditableAdmin
from copy import deepcopy
from django.contrib import admin
from django.conf import settings
from django.contrib.auth import get_user_model
from parler.admin import TranslatableAdmin
from .models import Post, BlogCategory
@ -61,8 +62,12 @@ class PostAdmin(EnhancedModelAdminMixin, FrontendEditableAdmin,
return {'slug': ('title',)}
def save_model(self, request, obj, form, change):
if not obj.author_id and get_setting('AUTHOR_AUTO'):
obj.author = request.user
if not obj.author_id and get_setting('AUTHOR_DEFAULT'):
if get_setting('AUTHOR_DEFAULT') is True:
user = request.user
else:
user = get_user_model().objects.get(username=get_setting('AUTHOR_DEFAULT'))
obj.author = user
super(PostAdmin, self).save_model(request, obj, form, change)
class Media:

View file

@ -49,6 +49,6 @@ def get_setting(name):
'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),
'BLOG_AUTHOR_DEFAULT': getattr(settings, 'BLOG_AUTHOR_DEFAULT', True),
}
return default['BLOG_%s' % name]

View file

@ -49,7 +49,7 @@ class AdminTest(BaseTest):
request = self.get_page_request('/', self.user_staff, r'/en/blog/', edit=False)
data = deepcopy(self.data['en'][0])
with self.settings(BLOG_AUTHOR_AUTO=True):
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
@ -61,7 +61,7 @@ class AdminTest(BaseTest):
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):
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')
@ -74,6 +74,19 @@ class AdminTest(BaseTest):
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')
class ModelsTest(BaseTest):