Merge pull request #180 from peacedata/develop

Post admin's fieldset filter function
This commit is contained in:
Iacopo Spalletti 2015-12-20 17:12:03 +01:00
commit 0921a675f6
4 changed files with 32 additions and 0 deletions

View file

@ -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;

View file

@ -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):

View file

@ -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
),

View file

@ -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'])