From 85c65e0767a1f3ce5260fde7eceefb1c70eea3b3 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 14 Dec 2015 15:46:55 +0300 Subject: [PATCH 1/4] - BLOG_ADMIN_POST_FIELDSET_FILTER settings is added, doc is updated - functionality and test for filedssets filtering --- README.rst | 2 ++ djangocms_blog/admin.py | 4 ++++ djangocms_blog/settings.py | 2 ++ tests/test_models.py | 16 ++++++++++++++++ 4 files changed, 24 insertions(+) diff --git a/README.rst b/README.rst index e25f58d..77af1c8 100644 --- a/README.rst +++ b/README.rst @@ -351,6 +351,8 @@ 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``) * 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..ecbb2a3 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -10,6 +10,7 @@ from django.conf import settings from django.contrib import admin from django.contrib.auth import get_user_model from django.utils.translation import ugettext_lazy as _ +from django.utils.six import callable from parler.admin import TranslatableAdmin from .cms_appconfig import BlogConfig @@ -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 f3b2878..1798a26 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']) From a315e1c3f8a6430b815f753b2bd6cc5856c833ad Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 14 Dec 2015 15:53:21 +0300 Subject: [PATCH 2/4] fix for isort --- djangocms_blog/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index ecbb2a3..3036c51 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -9,8 +9,8 @@ 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.translation import ugettext_lazy as _ from django.utils.six import callable +from django.utils.translation import ugettext_lazy as _ from parler.admin import TranslatableAdmin from .cms_appconfig import BlogConfig From 2aa52b16bb4807e198c8f59a343aba2c453e37d5 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 15 Dec 2015 12:19:04 +0300 Subject: [PATCH 3/4] example addon in README.rst for BLOG_ADMIN_POST_FIELDSET_FILTER usage --- README.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 77af1c8..f805e8a 100644 --- a/README.rst +++ b/README.rst @@ -352,7 +352,11 @@ Global Settings * 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``) + 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; From f364cd3c12c59e8d74382c1657948d711b686918 Mon Sep 17 00:00:00 2001 From: cluster-master Date: Tue, 15 Dec 2015 13:13:32 +0300 Subject: [PATCH 4/4] Update README.rst --- README.rst | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index f805e8a..2ac21e1 100644 --- a/README.rst +++ b/README.rst @@ -352,11 +352,15 @@ Global Settings * 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 + 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;