Merge pull request #358 from fsbraun/develop

Add bulk actions (enhancement) (#356)
This commit is contained in:
Iacopo Spalletti 2017-11-04 12:19:51 +01:00 committed by GitHub
commit 94896db873
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 2 deletions

View file

@ -17,6 +17,7 @@ Contributors
* cluster-master
* danra
* Davide Truffo
* Fabian Braun
* frnhr
* furiousdave
* Georgiy Kutsurua

View file

@ -10,12 +10,14 @@ from django import forms
from django.apps import apps
from django.conf import settings
from django.conf.urls import url
from django.contrib import admin
from django.contrib import admin, messages
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils import timezone
from django.utils.six import callable, text_type
from django.utils.translation import get_language_from_request, ugettext_lazy as _
from django.utils.translation import get_language_from_request, ugettext_lazy as _, ungettext as __
from parler.admin import TranslatableAdmin
from .cms_appconfig import BlogConfig
@ -84,6 +86,14 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin,
raw_id_fields = ['author']
frontend_editable_fields = ('title', 'abstract', 'post_text')
enhance_exclude = ('main_image', 'tags')
actions = [
'make_published',
'make_unpublished',
'enable_comments',
'disable_comments',
]
if 'djangocms_blog.liveblog' in settings.INSTALLED_APPS:
actions += ['enable_liveblog', 'disable_liveblog']
_fieldsets = [
(None, {
'fields': [['title', 'categories', 'publish', 'app_config']]
@ -112,6 +122,95 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin,
}
_sites = None
# Bulk actions for post admin
def make_published(self, request, queryset):
""" Bulk action to mark selected posts as published. If
the date_published field is empty the current time is
saved as date_published.
queryset must not be empty (ensured by DjangoCMS).
"""
cnt1 = queryset.filter(
date_published__isnull=True,
publish=False,
).update(date_published=timezone.now(), publish=True)
cnt2 = queryset.filter(
date_published__isnull=False,
publish=False,
).update(publish=True)
messages.add_message(
request, messages.INFO,
__('%(updates)d entry published.',
'%(updates)d entries published.', cnt1+cnt2) % {
'updates': cnt1+cnt2, })
def make_unpublished(self, request, queryset):
""" Bulk action to mark selected posts as UNpublished.
queryset must not be empty (ensured by DjangoCMS).
"""
updates = queryset.filter(publish=True)\
.update(publish=False)
messages.add_message(
request, messages.INFO,
__('%(updates)d entry unpublished.',
'%(updates)d entries unpublished.', updates) % {
'updates': updates, })
def enable_comments(self, request, queryset):
""" Bulk action to enable comments for selected posts.
queryset must not be empty (ensured by DjangoCMS).
"""
updates = queryset.filter(enable_comments=False)\
.update(enable_comments=True)
messages.add_message(
request, messages.INFO,
__('Comments for %(updates)d entry enabled.',
'Comments for %(updates)d entries enabled', updates) % {
'updates': updates, })
def disable_comments(self, request, queryset):
""" Bulk action to disable comments for selected posts.
queryset must not be empty (ensured by DjangoCMS).
"""
updates = queryset.filter(enable_comments=True)\
.update(enable_comments=False)
messages.add_message(
request, messages.INFO,
__('Comments for %(updates)d entry disabled.',
'Comments for %(updates)d entries disabled.', updates) % {
'updates': updates, })
def enable_liveblog(self, request, queryset):
""" Bulk action to enable comments for selected posts.
queryset must not be empty (ensured by DjangoCMS).
"""
updates = queryset.filter(enable_liveblog=False)\
.update(enable_liveblog=True)
messages.add_message(
request, messages.INFO,
__('Liveblog for %(updates)d entry enabled.',
'Liveblog for %(updates)d entries enabled.', updates) % {
'updates': updates, })
def disable_liveblog(self, request, queryset):
""" Bulk action to disable comments for selected posts.
queryset must not be empty (ensured by DjangoCMS).
"""
updates = queryset.filter(enable_liveblog=True)\
.update(enable_liveblog=False)
messages.add_message(
request, messages.INFO,
__('Liveblog for %(updates)d entry enabled.',
'Liveblog for %(updates)d entries enabled.') % {
'updates': updates, })
# Make bulk action menu entries localizable
make_published.short_description = _("Publish selection")
make_unpublished.short_description = _("Unpublish selection")
enable_comments.short_description = _("Enable comments for selection")
disable_comments.short_description = _("Disable comments for selection ")
enable_liveblog.short_description = _("Enable liveblog for selection")
disable_liveblog.short_description = _("Disable liveblog for selection ")
def get_list_filter(self, request):
filters = ['app_config', 'publish', 'date_published']
if get_setting('MULTISITE'):