From 82453cd98647d71a73d81083704115b2df87f299 Mon Sep 17 00:00:00 2001 From: Lee Solway <lee@digitalanvil.co.uk> Date: Tue, 5 Jul 2016 15:19:04 +0100 Subject: [PATCH] Added admin filters to support multisite and publishing --- djangocms_blog/admin.py | 48 +++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/djangocms_blog/admin.py b/djangocms_blog/admin.py index d76cc95..7ff3fd8 100755 --- a/djangocms_blog/admin.py +++ b/djangocms_blog/admin.py @@ -5,7 +5,7 @@ from copy import deepcopy from aldryn_apphooks_config.admin import BaseAppHookConfig, ModelAppHookConfig from cms.admin.placeholderadmin import FrontendEditableAdminMixin, PlaceholderAdminMixin -from cms.models import CMSPlugin +from cms.models import CMSPlugin, ValidationError from django import forms from django.apps import apps from django.conf import settings @@ -14,7 +14,7 @@ from django.contrib import admin from django.contrib.sites.models import Site from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect -from django.utils.six import callable +from django.utils.six import callable, text_type from django.utils.translation import get_language_from_request, ugettext_lazy as _ from parler.admin import TranslatableAdmin @@ -30,6 +30,26 @@ except ImportError: pass +class SiteListFilter(admin.SimpleListFilter): + title = _('site') + parameter_name = 'sites' + + def lookups(self, request, model_admin): + restricted_sites = model_admin.get_restricted_sites(request).values_list('id', flat=True) + + qs = Site.objects.all() + if restricted_sites: + qs = qs.filter(id__in=restricted_sites) + + return [(site.id, text_type(site.name)) for site in qs] + + def queryset(self, request, queryset): + try: + return queryset.filter(**self.used_parameters) + except ValidationError as e: + raise admin.options.IncorrectLookupParameters(e) + + class BlogCategoryAdmin(EnhancedModelAdminMixin, ModelAppHookConfig, TranslatableAdmin): form = CategoryAdminForm list_display = [ @@ -55,15 +75,6 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin, 'title', 'author', 'date_published', 'app_config', 'all_languages_column', 'date_published_end' ] - try: - from taggit_helpers.admin import TaggitListFilter - list_filter = ('app_config', TaggitListFilter) - except ImportError: - try: - from taggit_helpers import TaggitListFilter - list_filter = ('app_config', TaggitListFilter) - except ImportError: - list_filter = ('app_config',) search_fields = ('translations__title',) date_hierarchy = 'date_published' raw_id_fields = ['author'] @@ -94,6 +105,21 @@ class PostAdmin(PlaceholderAdminMixin, FrontendEditableAdminMixin, } _sites = None + def get_list_filter(self, request): + filters = ['app_config', 'publish', 'date_published'] + if get_setting('MULTISITE'): + filters.append(SiteListFilter) + try: + from taggit_helpers.admin import TaggitListFilter + filters.append(TaggitListFilter) + except ImportError: + try: + from taggit_helpers import TaggitListFilter + filters.append(TaggitListFilter) + except ImportError: + pass + return filters + def get_urls(self): """ Customize the modeladmin urls