From ec0c319ad475d629dea08b124aec5db11a280804 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Mon, 14 Mar 2016 10:40:11 +0000 Subject: [PATCH] Added convenient list filters --- TODO.md | 4 ---- orchestra/contrib/accounts/filters.py | 10 ++-------- orchestra/contrib/domains/filters.py | 6 ------ orchestra/contrib/letsencrypt/actions.py | 2 +- orchestra/contrib/lists/admin.py | 3 ++- orchestra/contrib/lists/filters.py | 21 +++++++++++++++++++++ orchestra/contrib/miscellaneous/admin.py | 5 +++-- orchestra/contrib/websites/admin.py | 11 ++++++++--- orchestra/contrib/websites/filters.py | 13 +++++++++++++ 9 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 orchestra/contrib/lists/filters.py diff --git a/TODO.md b/TODO.md index 1e3b9e4f..a5515169 100644 --- a/TODO.md +++ b/TODO.md @@ -430,8 +430,6 @@ mkhomedir_helper or create ssh homes with bash.rc and such # Automatically re-run backends until success? only timedout executions? # TODO save serialized versions ob backendoperation.instance in order to allow backend reexecution of deleted objects -# websites active list_display -# account for account.is_active on service is_active filters like systemusers # upgrade to django 1.9 and make margins wider # lets encrypt: DNS vs HTTP challange @@ -440,5 +438,3 @@ mkhomedir_helper or create ssh homes with bash.rc and such # Warning websites with ssl options without https protocol - - diff --git a/orchestra/contrib/accounts/filters.py b/orchestra/contrib/accounts/filters.py index 49caf1c5..6b8b5541 100644 --- a/orchestra/contrib/accounts/filters.py +++ b/orchestra/contrib/accounts/filters.py @@ -21,16 +21,10 @@ class HasMainUserListFilter(SimpleListFilter): return queryset.filter(users__isnull=True).distinct() -class IsActiveListFilter(SimpleListFilter): - title = _("Is active") +class IsActiveListFilter(HasMainUserListFilter): + title = _("is active") parameter_name = 'active' - def lookups(self, request, model_admin): - return ( - ('True', _("True")), - ('False', _("False")), - ) - def queryset(self, request, queryset): if self.value() == 'True': return queryset.filter(is_active=True, account__is_active=True) diff --git a/orchestra/contrib/domains/filters.py b/orchestra/contrib/domains/filters.py index f62e0f70..589079a4 100644 --- a/orchestra/contrib/domains/filters.py +++ b/orchestra/contrib/domains/filters.py @@ -41,12 +41,6 @@ class HasAddressFilter(HasWebsiteFilter): 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) diff --git a/orchestra/contrib/letsencrypt/actions.py b/orchestra/contrib/letsencrypt/actions.py index d8c492e8..d5672da8 100644 --- a/orchestra/contrib/letsencrypt/actions.py +++ b/orchestra/contrib/letsencrypt/actions.py @@ -35,7 +35,7 @@ def letsencrypt(modeladmin, request, queryset): encrypt_domains.add(domain) website.encrypt_domains = encrypt_domains operations.extend(Operation.create_for_action(website, 'encrypt')) - modeladmin.log_change(request, request.user, _("Encrypted!")) + modeladmin.log_change(request, website, _("Encrypted!")) if not operations: messages.error(request, _("No backend operation has been executed.")) else: diff --git a/orchestra/contrib/lists/admin.py b/orchestra/contrib/lists/admin.py index 15c6ff9a..afc174cf 100644 --- a/orchestra/contrib/lists/admin.py +++ b/orchestra/contrib/lists/admin.py @@ -11,6 +11,7 @@ from orchestra.contrib.accounts.admin import SelectAccountAdminMixin from orchestra.contrib.accounts.filters import IsActiveListFilter from . import settings +from .filters import HasCustomAddressListFilter from .forms import ListCreationForm, ListChangeForm from .models import List @@ -50,7 +51,7 @@ class ListAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedModel }), ) search_fields = ('name', 'address_name', 'address_domain__name', 'account__username') - list_filter = (IsActiveListFilter,) + list_filter = (IsActiveListFilter, HasCustomAddressListFilter) readonly_fields = ('account_link',) change_readonly_fields = ('name',) form = ListChangeForm diff --git a/orchestra/contrib/lists/filters.py b/orchestra/contrib/lists/filters.py new file mode 100644 index 00000000..5271153d --- /dev/null +++ b/orchestra/contrib/lists/filters.py @@ -0,0 +1,21 @@ +from django.contrib.admin import SimpleListFilter +from django.utils.translation import ugettext_lazy as _ + + +class HasCustomAddressListFilter(SimpleListFilter): + """ Filter addresses whether they have any webapp or not """ + title = _("has custom address") + parameter_name = 'has_custom_address' + + def lookups(self, request, model_admin): + return ( + ('True', _("True")), + ('False', _("False")), + ) + + def queryset(self, request, queryset): + if self.value() == 'True': + return queryset.exclude(address_name='') + elif self.value() == 'False': + return queryset.filter(address_name='') + return queryset diff --git a/orchestra/contrib/miscellaneous/admin.py b/orchestra/contrib/miscellaneous/admin.py index 95934626..91795a2b 100644 --- a/orchestra/contrib/miscellaneous/admin.py +++ b/orchestra/contrib/miscellaneous/admin.py @@ -8,6 +8,7 @@ from django.utils.translation import ugettext_lazy as _ from orchestra.admin import ExtendedModelAdmin from orchestra.admin.utils import admin_link from orchestra.contrib.accounts.admin import AccountAdminMixin +from orchestra.contrib.accounts.filters import IsActiveListFilter from orchestra.plugins import PluginModelAdapter from orchestra.plugins.admin import SelectPluginAdminMixin from orchestra.utils.python import import_class @@ -26,7 +27,7 @@ class MiscServiceAdmin(ExtendedModelAdmin): 'name', 'verbose_name', 'num_instances', 'has_identifier', 'has_amount', 'is_active' ) list_editable = ('is_active',) - list_filter = ('has_identifier', 'has_amount', 'is_active') + list_filter = ('has_identifier', 'has_amount', IsActiveListFilter) fields = ( 'verbose_name', 'name', 'description', 'has_identifier', 'has_amount', 'is_active' ) @@ -55,7 +56,7 @@ class MiscServiceAdmin(ExtendedModelAdmin): class MiscellaneousAdmin(AccountAdminMixin, SelectPluginAdminMixin, admin.ModelAdmin): list_display = ( - '__str__', 'service_link', 'amount', 'dispaly_active', 'account_link', 'is_active' + '__str__', 'service_link', 'amount', 'account_link', 'dispaly_active' ) list_filter = ('service__name', 'is_active') list_select_related = ('service', 'account') diff --git a/orchestra/contrib/websites/admin.py b/orchestra/contrib/websites/admin.py index 59162b56..56573091 100644 --- a/orchestra/contrib/websites/admin.py +++ b/orchestra/contrib/websites/admin.py @@ -10,11 +10,12 @@ from orchestra.admin.actions import disable from orchestra.admin.utils import admin_link, change_url from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.admin import AccountAdminMixin, SelectAccountAdminMixin +from orchestra.contrib.accounts.filters import IsActiveListFilter from orchestra.forms.widgets import DynamicHelpTextSelect from orchestra.utils.html import get_on_site_link from .directives import SiteDirective -from .filters import HasWebAppsListFilter +from .filters import HasWebAppsListFilter, HasDomainsFilter from .forms import WebsiteAdminForm, WebsiteDirectiveInlineFormSet from .models import Content, Website, WebsiteDirective @@ -56,8 +57,12 @@ class ContentInline(AccountAdminMixin, admin.TabularInline): class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): - list_display = ('name', 'display_domains', 'display_webapps', 'account_link') - list_filter = ('protocol', 'is_active', HasWebAppsListFilter) + list_display = ( + 'name', 'display_domains', 'display_webapps', 'account_link', 'display_active' + ) + list_filter = ( + 'protocol', IsActiveListFilter, HasWebAppsListFilter, HasDomainsFilter + ) change_readonly_fields = ('name',) inlines = [ContentInline, WebsiteDirectiveInline] filter_horizontal = ['domains'] diff --git a/orchestra/contrib/websites/filters.py b/orchestra/contrib/websites/filters.py index 0b9a673e..5fb19f44 100644 --- a/orchestra/contrib/websites/filters.py +++ b/orchestra/contrib/websites/filters.py @@ -19,3 +19,16 @@ class HasWebAppsListFilter(SimpleListFilter): elif self.value() == 'False': return queryset.filter(content__isnull=True) return queryset + + +class HasDomainsFilter(HasWebAppsListFilter): + """ Filter addresses whether they have any domains or not """ + title = _("has domains") + parameter_name = 'has_domains' + + def queryset(self, request, queryset): + if self.value() == 'True': + return queryset.filter(domains__isnull=False) + elif self.value() == 'False': + return queryset.filter(domains__isnull=True) + return queryset