diff --git a/README.rst b/README.rst index 72e5f69..cfcacbb 100644 --- a/README.rst +++ b/README.rst @@ -363,6 +363,16 @@ Global Settings used; (default: ``True``) * BLOG_DEFAULT_PUBLISHED: If posts are marked as published by default; (default: ``False``) +* BLOG_ADMIN_POST_FIELDSET_FILTER: Callable function to change(add or filter) + fields to fieldsets for admin post edit form; (default: ``False``). Function simple example:: + + + def fieldset_filter_function(fsets, request, obj=None): + if request.user.groups.filter(name='Editor').exists(): + fsets[1][1]['fields'][0].append('author') # adding 'author' field if user is Editor + return fsets + + * BLOG_AVAILABLE_PERMALINK_STYLES: Choices of permalinks styles; * BLOG_PERMALINK_URLS: URLConf corresponding to BLOG_AVAILABLE_PERMALINK_STYLES; diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index f90ea87..3036c51 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -9,6 +9,7 @@ from django import forms from django.conf import settings from django.contrib import admin from django.contrib.auth import get_user_model +from django.utils.six import callable from django.utils.translation import ugettext_lazy as _ from parler.admin import TranslatableAdmin @@ -108,6 +109,9 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin, fsets[1][1]['fields'][0].append('sites') if request.user.is_superuser: fsets[1][1]['fields'][0].append('author') + filter_function = get_setting('ADMIN_POST_FIELDSET_FILTER') + if callable(filter_function): + fsets = filter_function(fsets, request, obj=obj) return fsets def get_prepopulated_fields(self, request, obj=None): diff --git a/djangocms_blog/settings.py b/djangocms_blog/settings.py index 7538a1c..3fb98c5 100644 --- a/djangocms_blog/settings.py +++ b/djangocms_blog/settings.py @@ -80,6 +80,8 @@ def get_setting(name): 'BLOG_MULTISITE': getattr(settings, 'BLOG_MULTISITE', True), 'BLOG_AUTHOR_DEFAULT': getattr(settings, 'BLOG_AUTHOR_DEFAULT', True), 'BLOG_DEFAULT_PUBLISHED': getattr(settings, 'BLOG_DEFAULT_PUBLISHED', False), + 'BLOG_ADMIN_POST_FIELDSET_FILTER': getattr( + settings, 'BLOG_ADMIN_POST_FIELDSET_FILTER', False), 'BLOG_AVAILABLE_PERMALINK_STYLES': getattr( settings, 'BLOG_AVAILABLE_PERMALINK_STYLES', PERMALINKS ), diff --git a/tests/test_models.py b/tests/test_models.py index 77cd4f8..fcce681 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -191,6 +191,22 @@ class AdminTest(BaseTest): self.assertEqual(Post.objects.count(), 3) self.assertEqual(Post.objects.get(translations__slug='third-post').author.username, 'staff') + def test_admin_fieldsets_filter(self): + post_admin = admin.site._registry[Post] + request = self.get_page_request('/', self.user_normal, r'/en/blog/?app_config=%s' % self.app_config_1.pk) + + fsets = post_admin.get_fieldsets(request) + self.assertFalse('author' in fsets[1][1]['fields'][0]) + + def filter_function(fs, request, obj=None): + if request.user == self.user_normal: + fs[1][1]['fields'][0].append('author') + return fs + + with self.settings(BLOG_ADMIN_POST_FIELDSET_FILTER=filter_function): + fsets = post_admin.get_fieldsets(request) + self.assertTrue('author' in fsets[1][1]['fields'][0]) + def test_admin_post_text(self): pages = self.get_pages() post = self._get_post(self._post_data[0]['en'])