Messages for bulk cations, bulk actions as staticmethods of PostAdmin

This commit is contained in:
Fabian Braun 2017-03-26 19:07:15 +02:00
parent c079a86582
commit 13ac239576

View file

@ -11,12 +11,15 @@ 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 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 ungettext as __
from parler.admin import TranslatableAdmin
from .cms_appconfig import BlogConfig
@ -31,59 +34,6 @@ except ImportError:
pass
# Bulk actions for post admin
def make_published(modeladmin, 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.
"""
for post in queryset:
if not post.publish:
if not post.date_published:
post.date_published = timezone.now()
post.publish = True
post.save()
def make_unpublished(modeladmin, request, queryset):
""" Bulk action to mark selected posts as UNpublished.
"""
queryset.update(publish=False)
def enable_comments(modeladmin, request, queryset):
""" Bulk action to enable comments for selected posts.
"""
queryset.update(enable_comments=True)
def disable_comments(modeladmin, request, queryset):
""" Bulk action to disable comments for selected posts.
"""
queryset.update(enable_comments=False)
def enable_liveblog(modeladmin, request, queryset):
""" Bulk action to enable comments for selected posts.
"""
queryset.update(enable_liveblog=True)
def disable_liveblog(modeladmin, request, queryset):
""" Bulk action to disable comments for selected posts.
"""
queryset.update(enable_liveblog=False)
# 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 ")
class SiteListFilter(admin.SimpleListFilter):
title = _('site')
parameter_name = 'sites'
@ -136,14 +86,6 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin,
search_fields = ('translations__title',)
date_hierarchy = 'date_published'
raw_id_fields = ['author']
actions = [
make_published,
make_unpublished,
enable_comments,
disable_comments,
]
if 'djangocms_blog.liveblog' in settings.INSTALLED_APPS:
actions += [enable_liveblog, disable_liveblog]
frontend_editable_fields = ('title', 'abstract', 'post_text')
enhance_exclude = ('main_image', 'tags')
_fieldsets = [
@ -171,6 +113,110 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin,
}
_sites = None
# Bulk actions for post admin
@staticmethod
def make_published(modeladmin, 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, })
@staticmethod
def make_unpublished(modeladmin, 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, })
@staticmethod
def enable_comments(modeladmin, 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, })
@staticmethod
def disable_comments(modeladmin, 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, })
@staticmethod
def enable_liveblog(modeladmin, 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, })
@staticmethod
def disable_liveblog(modeladmin, 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.__func__.short_description = _("Publish selection")
make_unpublished.__func__.short_description = _("Unpublish selection")
enable_comments.__func__.short_description = _("Enable comments for selection")
disable_comments.__func__.short_description = _("Disable comments for selection ")
enable_liveblog.__func__.short_description = _("Enable liveblog for selection")
disable_liveblog.__func__.short_description = _("Disable liveblog for selection ")
actions = [
make_published.__func__,
make_unpublished.__func__,
enable_comments.__func__,
disable_comments.__func__,
]
if 'djangocms_blog.liveblog' in settings.INSTALLED_APPS:
actions += [enable_liveblog.__func__, disable_liveblog.__func__]
def get_list_filter(self, request):
filters = ['app_config', 'publish', 'date_published']
if get_setting('MULTISITE'):