Added convenient list filters

This commit is contained in:
Marc Aymerich 2016-03-14 10:40:11 +00:00
parent d39e742809
commit ec0c319ad4
9 changed files with 50 additions and 25 deletions

View File

@ -430,8 +430,6 @@ mkhomedir_helper or create ssh homes with bash.rc and such
# Automatically re-run backends until success? only timedout executions? # 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 # 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 # upgrade to django 1.9 and make margins wider
# lets encrypt: DNS vs HTTP challange # 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 # Warning websites with ssl options without https protocol

View File

@ -21,16 +21,10 @@ class HasMainUserListFilter(SimpleListFilter):
return queryset.filter(users__isnull=True).distinct() return queryset.filter(users__isnull=True).distinct()
class IsActiveListFilter(SimpleListFilter): class IsActiveListFilter(HasMainUserListFilter):
title = _("Is active") title = _("is active")
parameter_name = 'active' parameter_name = 'active'
def lookups(self, request, model_admin):
return (
('True', _("True")),
('False', _("False")),
)
def queryset(self, request, queryset): def queryset(self, request, queryset):
if self.value() == 'True': if self.value() == 'True':
return queryset.filter(is_active=True, account__is_active=True) return queryset.filter(is_active=True, account__is_active=True)

View File

@ -41,12 +41,6 @@ class HasAddressFilter(HasWebsiteFilter):
title = _("has addresses") title = _("has addresses")
parameter_name = 'has_addresses' parameter_name = 'has_addresses'
def lookups(self, request, model_admin):
return (
('True', _("True")),
('False', _("False")),
)
def queryset(self, request, queryset): def queryset(self, request, queryset):
if self.value() == 'True': if self.value() == 'True':
return queryset.filter(addresses__isnull=False) return queryset.filter(addresses__isnull=False)

View File

@ -35,7 +35,7 @@ def letsencrypt(modeladmin, request, queryset):
encrypt_domains.add(domain) encrypt_domains.add(domain)
website.encrypt_domains = encrypt_domains website.encrypt_domains = encrypt_domains
operations.extend(Operation.create_for_action(website, 'encrypt')) operations.extend(Operation.create_for_action(website, 'encrypt'))
modeladmin.log_change(request, request.user, _("Encrypted!")) modeladmin.log_change(request, website, _("Encrypted!"))
if not operations: if not operations:
messages.error(request, _("No backend operation has been executed.")) messages.error(request, _("No backend operation has been executed."))
else: else:

View File

@ -11,6 +11,7 @@ from orchestra.contrib.accounts.admin import SelectAccountAdminMixin
from orchestra.contrib.accounts.filters import IsActiveListFilter from orchestra.contrib.accounts.filters import IsActiveListFilter
from . import settings from . import settings
from .filters import HasCustomAddressListFilter
from .forms import ListCreationForm, ListChangeForm from .forms import ListCreationForm, ListChangeForm
from .models import List from .models import List
@ -50,7 +51,7 @@ class ListAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedModel
}), }),
) )
search_fields = ('name', 'address_name', 'address_domain__name', 'account__username') search_fields = ('name', 'address_name', 'address_domain__name', 'account__username')
list_filter = (IsActiveListFilter,) list_filter = (IsActiveListFilter, HasCustomAddressListFilter)
readonly_fields = ('account_link',) readonly_fields = ('account_link',)
change_readonly_fields = ('name',) change_readonly_fields = ('name',)
form = ListChangeForm form = ListChangeForm

View File

@ -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

View File

@ -8,6 +8,7 @@ from django.utils.translation import ugettext_lazy as _
from orchestra.admin import ExtendedModelAdmin from orchestra.admin import ExtendedModelAdmin
from orchestra.admin.utils import admin_link from orchestra.admin.utils import admin_link
from orchestra.contrib.accounts.admin import AccountAdminMixin from orchestra.contrib.accounts.admin import AccountAdminMixin
from orchestra.contrib.accounts.filters import IsActiveListFilter
from orchestra.plugins import PluginModelAdapter from orchestra.plugins import PluginModelAdapter
from orchestra.plugins.admin import SelectPluginAdminMixin from orchestra.plugins.admin import SelectPluginAdminMixin
from orchestra.utils.python import import_class 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' 'name', 'verbose_name', 'num_instances', 'has_identifier', 'has_amount', 'is_active'
) )
list_editable = ('is_active',) list_editable = ('is_active',)
list_filter = ('has_identifier', 'has_amount', 'is_active') list_filter = ('has_identifier', 'has_amount', IsActiveListFilter)
fields = ( fields = (
'verbose_name', 'name', 'description', 'has_identifier', 'has_amount', 'is_active' 'verbose_name', 'name', 'description', 'has_identifier', 'has_amount', 'is_active'
) )
@ -55,7 +56,7 @@ class MiscServiceAdmin(ExtendedModelAdmin):
class MiscellaneousAdmin(AccountAdminMixin, SelectPluginAdminMixin, admin.ModelAdmin): class MiscellaneousAdmin(AccountAdminMixin, SelectPluginAdminMixin, admin.ModelAdmin):
list_display = ( 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_filter = ('service__name', 'is_active')
list_select_related = ('service', 'account') list_select_related = ('service', 'account')

View File

@ -10,11 +10,12 @@ from orchestra.admin.actions import disable
from orchestra.admin.utils import admin_link, change_url from orchestra.admin.utils import admin_link, change_url
from orchestra.contrib.accounts.actions import list_accounts from orchestra.contrib.accounts.actions import list_accounts
from orchestra.contrib.accounts.admin import AccountAdminMixin, SelectAccountAdminMixin from orchestra.contrib.accounts.admin import AccountAdminMixin, SelectAccountAdminMixin
from orchestra.contrib.accounts.filters import IsActiveListFilter
from orchestra.forms.widgets import DynamicHelpTextSelect 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 .filters import HasWebAppsListFilter, HasDomainsFilter
from .forms import WebsiteAdminForm, WebsiteDirectiveInlineFormSet from .forms import WebsiteAdminForm, WebsiteDirectiveInlineFormSet
from .models import Content, Website, WebsiteDirective from .models import Content, Website, WebsiteDirective
@ -56,8 +57,12 @@ class ContentInline(AccountAdminMixin, admin.TabularInline):
class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
list_display = ('name', 'display_domains', 'display_webapps', 'account_link') list_display = (
list_filter = ('protocol', 'is_active', HasWebAppsListFilter) 'name', 'display_domains', 'display_webapps', 'account_link', 'display_active'
)
list_filter = (
'protocol', IsActiveListFilter, HasWebAppsListFilter, HasDomainsFilter
)
change_readonly_fields = ('name',) change_readonly_fields = ('name',)
inlines = [ContentInline, WebsiteDirectiveInline] inlines = [ContentInline, WebsiteDirectiveInline]
filter_horizontal = ['domains'] filter_horizontal = ['domains']

View File

@ -19,3 +19,16 @@ class HasWebAppsListFilter(SimpleListFilter):
elif self.value() == 'False': elif self.value() == 'False':
return queryset.filter(content__isnull=True) return queryset.filter(content__isnull=True)
return queryset 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