From 631c27cd140f3bdf0c93b39a02808d39f6aeb55b Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Thu, 24 Aug 2023 17:49:13 +0200 Subject: [PATCH] add filter servers on accounts and mailboxes to send messages --- orchestra/contrib/accounts/admin.py | 3 ++- orchestra/contrib/accounts/filters.py | 17 ++++++++++++++++- orchestra/contrib/mailboxes/admin.py | 4 ++-- orchestra/contrib/mailboxes/filters.py | 18 +++++++++++++++++- orchestra/contrib/webapps/admin.py | 2 +- orchestra/contrib/websites/admin.py | 2 +- 6 files changed, 39 insertions(+), 7 deletions(-) diff --git a/orchestra/contrib/accounts/admin.py b/orchestra/contrib/accounts/admin.py index b2d6442a..0c1e81c0 100644 --- a/orchestra/contrib/accounts/admin.py +++ b/orchestra/contrib/accounts/admin.py @@ -26,12 +26,13 @@ from .actions import (list_contacts, service_report, delete_related_services, di enable_selected) from .forms import AccountCreationForm from .models import Account +from .filters import HasTipeServerFilter class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin): list_display = ('username', 'full_name', 'type', 'is_active') list_filter = ( - 'type', 'is_active', + 'type', 'is_active', HasTipeServerFilter ) add_fieldsets = ( (_("User"), { diff --git a/orchestra/contrib/accounts/filters.py b/orchestra/contrib/accounts/filters.py index ff4be0ae..bbffd8c2 100644 --- a/orchestra/contrib/accounts/filters.py +++ b/orchestra/contrib/accounts/filters.py @@ -1,7 +1,9 @@ from django.contrib.admin import SimpleListFilter from django.db.models import Q from django.utils.translation import gettext_lazy as _ - +from orchestra.contrib.orchestration.models import Server +from orchestra.contrib.websites.models import Website +from orchestra.settings import WEB_SERVERS class IsActiveListFilter(SimpleListFilter): title = _("is active") @@ -25,3 +27,16 @@ class IsActiveListFilter(SimpleListFilter): elif self.value() == 'object': return queryset.filter(is_active=False) return queryset + +class HasTipeServerFilter(SimpleListFilter): + title = _("has type server") + parameter_name = 'has_servers' + + def lookups(self, request, model_admin): + return [ (x.id, x.name) for x in Server.objects.filter(name__in=WEB_SERVERS) ] + + def queryset(self, request, queryset): + if self.value() is not None: + serverWebsites = Website.objects.filter(target_server=self.value()) + return queryset.filter(id__in=[ x.account.id for x in serverWebsites ] ) + return queryset \ No newline at end of file diff --git a/orchestra/contrib/mailboxes/admin.py b/orchestra/contrib/mailboxes/admin.py index d1094d3f..09924dd6 100644 --- a/orchestra/contrib/mailboxes/admin.py +++ b/orchestra/contrib/mailboxes/admin.py @@ -20,7 +20,7 @@ from orchestra.core import caches from . import settings from .actions import SendMailboxEmail, SendAddressEmail -from .filters import HasMailboxListFilter, HasForwardListFilter, HasAddressListFilter +from .filters import HasMailboxListFilter, HasForwardListFilter, HasAddressListFilter, HasTipeServerFilter from .forms import MailboxCreationForm, MailboxChangeForm, AddressForm from .models import Mailbox, Address, Autoresponse from .widgets import OpenCustomFilteringOnSelect @@ -40,7 +40,7 @@ class MailboxAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedMo list_display = ( 'name', 'account_link', 'display_filtering', 'display_addresses', 'display_active', ) - list_filter = (IsActiveListFilter, HasAddressListFilter, 'filtering') + list_filter = (IsActiveListFilter, HasAddressListFilter, 'filtering', HasTipeServerFilter) search_fields = ( 'account__username', 'account__short_name', 'account__full_name', 'name', 'addresses__name', 'addresses__domain__name', diff --git a/orchestra/contrib/mailboxes/filters.py b/orchestra/contrib/mailboxes/filters.py index 2c1dd603..0ef6978b 100644 --- a/orchestra/contrib/mailboxes/filters.py +++ b/orchestra/contrib/mailboxes/filters.py @@ -1,6 +1,8 @@ from django.contrib.admin import SimpleListFilter from django.utils.translation import gettext_lazy as _ - +from orchestra.contrib.orchestration.models import Server +from orchestra.contrib.websites.models import Website +from orchestra.settings import WEB_SERVERS class HasMailboxListFilter(SimpleListFilter): """ Filter addresses whether they have any mailbox or not """ @@ -45,3 +47,17 @@ class HasAddressListFilter(HasMailboxListFilter): elif self.value() == 'False': return queryset.filter(addresses__isnull=True) return queryset + + +class HasTipeServerFilter(SimpleListFilter): + title = _("has type server") + parameter_name = 'has_servers' + + def lookups(self, request, model_admin): + return [ (x.id, x.name) for x in Server.objects.filter(name__in=WEB_SERVERS) ] + + def queryset(self, request, queryset): + if self.value() is not None: + serverWebsites = Website.objects.filter(target_server=self.value()) + return queryset.filter(account__in=[ x.account.id for x in serverWebsites ] ) + return queryset \ No newline at end of file diff --git a/orchestra/contrib/webapps/admin.py b/orchestra/contrib/webapps/admin.py index 6cddeec5..afa9c8ab 100644 --- a/orchestra/contrib/webapps/admin.py +++ b/orchestra/contrib/webapps/admin.py @@ -58,7 +58,7 @@ class WebAppAdmin(SelectPluginAdminMixin, AccountAdminMixin, ExtendedModelAdmin) list_display = ( 'name', 'display_type', 'display_detail', 'display_websites', 'account_link', 'target_server', ) - list_filter = ('type', HasWebsiteListFilter, DetailListFilter) + list_filter = ('type', HasWebsiteListFilter, DetailListFilter, 'target_server') inlines = [WebAppOptionInline] readonly_fields = ('account_link',) change_readonly_fields = ('name', 'type', 'display_websites', 'display_sftpuser', 'target_server',) diff --git a/orchestra/contrib/websites/admin.py b/orchestra/contrib/websites/admin.py index 6682c4c7..f47f28b9 100644 --- a/orchestra/contrib/websites/admin.py +++ b/orchestra/contrib/websites/admin.py @@ -63,7 +63,7 @@ class WebsiteAdmin(SelectAccountAdminMixin, ExtendedModelAdmin): 'name', 'display_domains', 'display_webapps', 'account_link', 'target_server', 'display_active' ) list_filter = ( - 'protocol', IsActiveListFilter, HasWebAppsListFilter, HasDomainsFilter + 'protocol', IsActiveListFilter, HasWebAppsListFilter, HasDomainsFilter, 'target_server' ) change_readonly_fields = ('name',) inlines = (ContentInline, WebsiteDirectiveInline)