From 66a5e2c3abdc31e1ae8135221f1eb55aa4ac89f4 Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sat, 11 Oct 2014 12:46:39 +0200 Subject: [PATCH] Make the default author configurable --- README.rst | 16 ++++++++++------ djangocms_blog/admin.py | 9 +++++++-- djangocms_blog/settings.py | 2 +- tests/test_models.py | 17 +++++++++++++++-- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index 69147f7..861cf36 100644 --- a/README.rst +++ b/README.rst @@ -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 ++++++++++++++++++++++++++ diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index 7fc10af..0702ec8 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -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: diff --git a/djangocms_blog/settings.py b/djangocms_blog/settings.py index a19a6b4..afb6f22 100644 --- a/djangocms_blog/settings.py +++ b/djangocms_blog/settings.py @@ -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] \ No newline at end of file diff --git a/tests/test_models.py b/tests/test_models.py index b8c99b2..b5bf560 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -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):