From 237e494751164ae24fbb07c32e73cb7bf36c6535 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Wed, 9 Mar 2016 11:16:28 +0000 Subject: [PATCH] Added list filters --- orchestra/contrib/databases/admin.py | 5 ++-- orchestra/contrib/databases/filters.py | 34 +++++++++++++++++++++++ orchestra/contrib/domains/admin.py | 4 +-- orchestra/contrib/domains/filters.py | 38 ++++++++++++++++++++++++++ orchestra/contrib/mailboxes/filters.py | 6 ++-- orchestra/contrib/websites/admin.py | 3 +- orchestra/contrib/websites/filters.py | 21 ++++++++++++++ 7 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 orchestra/contrib/databases/filters.py create mode 100644 orchestra/contrib/websites/filters.py diff --git a/orchestra/contrib/databases/admin.py b/orchestra/contrib/databases/admin.py index a0cc213d..f57ca154 100644 --- a/orchestra/contrib/databases/admin.py +++ b/orchestra/contrib/databases/admin.py @@ -8,13 +8,14 @@ from orchestra.admin.utils import change_url from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.admin import SelectAccountAdminMixin +from .filters import HasUserListFilter, HasDatabaseListFilter from .forms import DatabaseCreationForm, DatabaseUserChangeForm, DatabaseUserCreationForm from .models import Database, DatabaseUser class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): list_display = ('name', 'type', 'display_users', 'account_link') - list_filter = ('type',) + list_filter = ('type', HasUserListFilter) search_fields = ('name', 'account__username') change_readonly_fields = ('name', 'type') extra = 1 @@ -72,7 +73,7 @@ class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): class DatabaseUserAdmin(SelectAccountAdminMixin, ChangePasswordAdminMixin, ExtendedModelAdmin): list_display = ('username', 'type', 'display_databases', 'account_link') - list_filter = ('type',) + list_filter = ('type', HasDatabaseListFilter) search_fields = ('username', 'account__username') form = DatabaseUserChangeForm add_form = DatabaseUserCreationForm diff --git a/orchestra/contrib/databases/filters.py b/orchestra/contrib/databases/filters.py new file mode 100644 index 00000000..9b76957a --- /dev/null +++ b/orchestra/contrib/databases/filters.py @@ -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 diff --git a/orchestra/contrib/domains/admin.py b/orchestra/contrib/domains/admin.py index c24c62fc..8e03fb25 100644 --- a/orchestra/contrib/domains/admin.py +++ b/orchestra/contrib/domains/admin.py @@ -11,7 +11,7 @@ from orchestra.utils.html import get_on_site_link from . import settings 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 .models import Domain, Record @@ -55,7 +55,7 @@ class DomainAdmin(AccountAdminMixin, ExtendedModelAdmin): fields = ('name', 'account_link', 'display_websites') readonly_fields = ('account_link', 'top_link', 'display_websites', 'implicit_records') inlines = (RecordInline, DomainInline) - list_filter = (TopDomainListFilter,) + list_filter = (TopDomainListFilter, HasWebsiteFilter, HasAddressFilter) change_readonly_fields = ('name', 'serial') search_fields = ('name', 'account__username', 'records__value') add_form = BatchDomainCreationAdminForm diff --git a/orchestra/contrib/domains/filters.py b/orchestra/contrib/domains/filters.py index be3fba20..f62e0f70 100644 --- a/orchestra/contrib/domains/filters.py +++ b/orchestra/contrib/domains/filters.py @@ -15,3 +15,41 @@ class TopDomainListFilter(SimpleListFilter): def queryset(self, request, queryset): if self.value() == '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 diff --git a/orchestra/contrib/mailboxes/filters.py b/orchestra/contrib/mailboxes/filters.py index ff11d4b3..23c2f163 100644 --- a/orchestra/contrib/mailboxes/filters.py +++ b/orchestra/contrib/mailboxes/filters.py @@ -4,7 +4,7 @@ from django.utils.translation import ugettext_lazy as _ class HasMailboxListFilter(SimpleListFilter): """ Filter addresses whether they have any mailbox or not """ - title = _("Has mailbox") + title = _("has mailbox") parameter_name = 'has_mailbox' def lookups(self, request, model_admin): @@ -23,7 +23,7 @@ class HasMailboxListFilter(SimpleListFilter): class HasForwardListFilter(HasMailboxListFilter): """ Filter addresses whether they have any mailbox or not """ - title = _("Has forward") + title = _("has forward") parameter_name = 'has_forward' def queryset(self, request, queryset): @@ -36,7 +36,7 @@ class HasForwardListFilter(HasMailboxListFilter): class HasAddressListFilter(HasMailboxListFilter): """ Filter addresses whether they have any mailbox or not """ - title = _("Has address") + title = _("has address") parameter_name = 'has_address' def queryset(self, request, queryset): diff --git a/orchestra/contrib/websites/admin.py b/orchestra/contrib/websites/admin.py index de51de49..59162b56 100644 --- a/orchestra/contrib/websites/admin.py +++ b/orchestra/contrib/websites/admin.py @@ -14,6 +14,7 @@ from orchestra.forms.widgets import DynamicHelpTextSelect from orchestra.utils.html import get_on_site_link from .directives import SiteDirective +from .filters import HasWebAppsListFilter from .forms import WebsiteAdminForm, WebsiteDirectiveInlineFormSet from .models import Content, Website, WebsiteDirective @@ -56,7 +57,7 @@ class ContentInline(AccountAdminMixin, admin.TabularInline): class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): list_display = ('name', 'display_domains', 'display_webapps', 'account_link') - list_filter = ('protocol', 'is_active',) + list_filter = ('protocol', 'is_active', HasWebAppsListFilter) change_readonly_fields = ('name',) inlines = [ContentInline, WebsiteDirectiveInline] filter_horizontal = ['domains'] diff --git a/orchestra/contrib/websites/filters.py b/orchestra/contrib/websites/filters.py new file mode 100644 index 00000000..0b9a673e --- /dev/null +++ b/orchestra/contrib/websites/filters.py @@ -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