Added list filters

This commit is contained in:
Marc Aymerich 2016-03-09 11:16:28 +00:00
parent ddc946b021
commit 237e494751
7 changed files with 103 additions and 8 deletions

View File

@ -8,13 +8,14 @@ from orchestra.admin.utils import change_url
from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.actions import list_accounts
from orchestra.contrib.accounts.admin import SelectAccountAdminMixin from orchestra.contrib.accounts.admin import SelectAccountAdminMixin
from .filters import HasUserListFilter, HasDatabaseListFilter
from .forms import DatabaseCreationForm, DatabaseUserChangeForm, DatabaseUserCreationForm from .forms import DatabaseCreationForm, DatabaseUserChangeForm, DatabaseUserCreationForm
from .models import Database, DatabaseUser from .models import Database, DatabaseUser
class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
list_display = ('name', 'type', 'display_users', 'account_link') list_display = ('name', 'type', 'display_users', 'account_link')
list_filter = ('type',) list_filter = ('type', HasUserListFilter)
search_fields = ('name', 'account__username') search_fields = ('name', 'account__username')
change_readonly_fields = ('name', 'type') change_readonly_fields = ('name', 'type')
extra = 1 extra = 1
@ -72,7 +73,7 @@ class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
class DatabaseUserAdmin(SelectAccountAdminMixin, ChangePasswordAdminMixin, ExtendedModelAdmin): class DatabaseUserAdmin(SelectAccountAdminMixin, ChangePasswordAdminMixin, ExtendedModelAdmin):
list_display = ('username', 'type', 'display_databases', 'account_link') list_display = ('username', 'type', 'display_databases', 'account_link')
list_filter = ('type',) list_filter = ('type', HasDatabaseListFilter)
search_fields = ('username', 'account__username') search_fields = ('username', 'account__username')
form = DatabaseUserChangeForm form = DatabaseUserChangeForm
add_form = DatabaseUserCreationForm add_form = DatabaseUserCreationForm

View File

@ -0,0 +1,34 @@
from django.contrib.admin import SimpleListFilter
from django.utils.translation import ugettext_lazy as _
class HasUserListFilter(SimpleListFilter):
""" Filter addresses whether they have any db user or not """
title = _("has user")
parameter_name = 'has_user'
def lookups(self, request, model_admin):
return (
('True', _("True")),
('False', _("False")),
)
def queryset(self, request, queryset):
if self.value() == 'True':
return queryset.filter(users__isnull=False)
elif self.value() == 'False':
return queryset.filter(users__isnull=True)
return queryset
class HasDatabaseListFilter(HasUserListFilter):
""" Filter addresses whether they have any db or not """
title = _("has database")
parameter_name = 'has_database'
def queryset(self, request, queryset):
if self.value() == 'True':
return queryset.filter(databases__isnull=False)
elif self.value() == 'False':
return queryset.filter(databases__isnull=True)
return queryset

View File

@ -11,7 +11,7 @@ from orchestra.utils.html import get_on_site_link
from . import settings from . import settings
from .actions import view_zone, edit_records, set_soa from .actions import view_zone, edit_records, set_soa
from .filters import TopDomainListFilter from .filters import TopDomainListFilter, HasWebsiteFilter, HasAddressFilter
from .forms import RecordForm, RecordInlineFormSet, BatchDomainCreationAdminForm from .forms import RecordForm, RecordInlineFormSet, BatchDomainCreationAdminForm
from .models import Domain, Record from .models import Domain, Record
@ -55,7 +55,7 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin):
fields = ('name', 'account_link', 'display_websites') fields = ('name', 'account_link', 'display_websites')
readonly_fields = ('account_link', 'top_link', 'display_websites', 'implicit_records') readonly_fields = ('account_link', 'top_link', 'display_websites', 'implicit_records')
inlines = (RecordInline, DomainInline) inlines = (RecordInline, DomainInline)
list_filter = (TopDomainListFilter,) list_filter = (TopDomainListFilter, HasWebsiteFilter, HasAddressFilter)
change_readonly_fields = ('name', 'serial') change_readonly_fields = ('name', 'serial')
search_fields = ('name', 'account__username', 'records__value') search_fields = ('name', 'account__username', 'records__value')
add_form = BatchDomainCreationAdminForm add_form = BatchDomainCreationAdminForm

View File

@ -15,3 +15,41 @@ class TopDomainListFilter(SimpleListFilter):
def queryset(self, request, queryset): def queryset(self, request, queryset):
if self.value() == 'True': if self.value() == 'True':
return queryset.filter(top__isnull=True) return queryset.filter(top__isnull=True)
class HasWebsiteFilter(SimpleListFilter):
""" Filter addresses whether they have any websites or not """
title = _("has websites")
parameter_name = 'has_websites'
def lookups(self, request, model_admin):
return (
('True', _("True")),
('False', _("False")),
)
def queryset(self, request, queryset):
if self.value() == 'True':
return queryset.filter(websites__isnull=False)
elif self.value() == 'False':
return queryset.filter(websites__isnull=True)
return queryset
class HasAddressFilter(HasWebsiteFilter):
""" Filter addresses whether they have any addresses or not """
title = _("has addresses")
parameter_name = 'has_addresses'
def lookups(self, request, model_admin):
return (
('True', _("True")),
('False', _("False")),
)
def queryset(self, request, queryset):
if self.value() == 'True':
return queryset.filter(addresses__isnull=False)
elif self.value() == 'False':
return queryset.filter(addresses__isnull=True)
return queryset

View File

@ -4,7 +4,7 @@ from django.utils.translation import ugettext_lazy as _
class HasMailboxListFilter(SimpleListFilter): class HasMailboxListFilter(SimpleListFilter):
""" Filter addresses whether they have any mailbox or not """ """ Filter addresses whether they have any mailbox or not """
title = _("Has mailbox") title = _("has mailbox")
parameter_name = 'has_mailbox' parameter_name = 'has_mailbox'
def lookups(self, request, model_admin): def lookups(self, request, model_admin):
@ -23,7 +23,7 @@ class HasMailboxListFilter(SimpleListFilter):
class HasForwardListFilter(HasMailboxListFilter): class HasForwardListFilter(HasMailboxListFilter):
""" Filter addresses whether they have any mailbox or not """ """ Filter addresses whether they have any mailbox or not """
title = _("Has forward") title = _("has forward")
parameter_name = 'has_forward' parameter_name = 'has_forward'
def queryset(self, request, queryset): def queryset(self, request, queryset):
@ -36,7 +36,7 @@ class HasForwardListFilter(HasMailboxListFilter):
class HasAddressListFilter(HasMailboxListFilter): class HasAddressListFilter(HasMailboxListFilter):
""" Filter addresses whether they have any mailbox or not """ """ Filter addresses whether they have any mailbox or not """
title = _("Has address") title = _("has address")
parameter_name = 'has_address' parameter_name = 'has_address'
def queryset(self, request, queryset): def queryset(self, request, queryset):

View File

@ -14,6 +14,7 @@ from orchestra.forms.widgets import DynamicHelpTextSelect
from orchestra.utils.html import get_on_site_link from orchestra.utils.html import get_on_site_link
from .directives import SiteDirective from .directives import SiteDirective
from .filters import HasWebAppsListFilter
from .forms import WebsiteAdminForm, WebsiteDirectiveInlineFormSet from .forms import WebsiteAdminForm, WebsiteDirectiveInlineFormSet
from .models import Content, Website, WebsiteDirective from .models import Content, Website, WebsiteDirective
@ -56,7 +57,7 @@ class ContentInline(AccountAdminMixin, admin.TabularInline):
class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
list_display = ('name', 'display_domains', 'display_webapps', 'account_link') list_display = ('name', 'display_domains', 'display_webapps', 'account_link')
list_filter = ('protocol', 'is_active',) list_filter = ('protocol', 'is_active', HasWebAppsListFilter)
change_readonly_fields = ('name',) change_readonly_fields = ('name',)
inlines = [ContentInline, WebsiteDirectiveInline] inlines = [ContentInline, WebsiteDirectiveInline]
filter_horizontal = ['domains'] filter_horizontal = ['domains']

View File

@ -0,0 +1,21 @@
from django.contrib.admin import SimpleListFilter
from django.utils.translation import ugettext_lazy as _
class HasWebAppsListFilter(SimpleListFilter):
""" Filter addresses whether they have any webapp or not """
title = _("has webapps")
parameter_name = 'has_webapps'
def lookups(self, request, model_admin):
return (
('True', _("True")),
('False', _("False")),
)
def queryset(self, request, queryset):
if self.value() == 'True':
return queryset.filter(content__isnull=False)
elif self.value() == 'False':
return queryset.filter(content__isnull=True)
return queryset