From 956d9d25fc1ac339cb148ec7faf80046cc14be37 Mon Sep 17 00:00:00 2001 From: Gagan Deep Date: Wed, 7 Dec 2022 00:23:38 +0530 Subject: [PATCH] [feature] Switched to autocomplete filters Co-authored-by: Federico Capoano --- README.rst | 2 ++ openwisp_ipam/admin.py | 5 +++-- openwisp_ipam/filters.py | 16 ++++++++++++++++ requirements.txt | 2 +- tests/openwisp2/settings.py | 3 ++- 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 openwisp_ipam/filters.py diff --git a/README.rst b/README.rst index bf2bcce..667dff6 100644 --- a/README.rst +++ b/README.rst @@ -481,6 +481,7 @@ The ``settings.py`` of your project should contain the following: 'openwisp_users', 'openwisp_ipam', # admin + 'admin_auto_filters', 'django.contrib.admin', # rest framework 'rest_framework', @@ -575,6 +576,7 @@ ensuring also that ``openwisp_ipam`` has been removed: 'openwisp_users', # 'myipam', <-- replace without your app-name here # admin + 'admin_auto_filters', 'django.contrib.admin', # rest framework 'rest_framework', diff --git a/openwisp_ipam/admin.py b/openwisp_ipam/admin.py index 2c7fa81..9d81dd8 100644 --- a/openwisp_ipam/admin.py +++ b/openwisp_ipam/admin.py @@ -22,6 +22,7 @@ from .api.views import HostsSet from .base.forms import IpAddressImportForm from .base.models import CsvImportException +from .filters import SubnetFilter, SubnetOrganizationFilter Subnet = swapper.load_model('openwisp_ipam', 'Subnet') IpAddress = swapper.load_model('openwisp_ipam', 'IpAddress') @@ -46,7 +47,7 @@ class SubnetAdmin( 'created', 'modified', ] - list_filter = [('organization', MultitenantOrgFilter)] + list_filter = [MultitenantOrgFilter] autocomplete_fields = ['master_subnet'] search_fields = ['subnet', 'name'] list_select_related = ['organization', 'master_subnet'] @@ -218,7 +219,7 @@ class IpAddressAdmin( form = IpAddressAdminForm change_form_template = 'admin/openwisp-ipam/ip_address/change_form.html' list_display = ['ip_address', 'subnet', 'organization', 'created', 'modified'] - list_filter = ['subnet', ('subnet__organization', MultitenantOrgFilter)] + list_filter = [SubnetOrganizationFilter, SubnetFilter] search_fields = ['ip_address'] autocomplete_fields = ['subnet'] multitenant_parent = 'subnet' diff --git a/openwisp_ipam/filters.py b/openwisp_ipam/filters.py new file mode 100644 index 0000000..57a3d79 --- /dev/null +++ b/openwisp_ipam/filters.py @@ -0,0 +1,16 @@ +from django.utils.translation import gettext_lazy as _ +from openwisp_users.multitenancy import MultitenantRelatedOrgFilter +from swapper import load_model + +Subnet = load_model('openwisp_ipam', 'Subnet') + + +class SubnetFilter(MultitenantRelatedOrgFilter): + field_name = 'subnet' + parameter_name = 'subnet_id' + title = _('subnet') + + +class SubnetOrganizationFilter(MultitenantRelatedOrgFilter): + parameter_name = 'subnet__organization' + rel_model = Subnet diff --git a/requirements.txt b/requirements.txt index 10d8f0d..8a34665 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -openwisp-users @ https://github.com/openwisp/openwisp-users/tarball/3ec78ab47395b5aa1e4f27fd9670b57b6f0a3acb +openwisp-users @ https://github.com/openwisp/openwisp-users/tarball/master openwisp-utils[rest] @ https://github.com/openwisp/openwisp-utils/tarball/master django>=3.0.0,<4.1.0 openpyxl~=3.0.9 diff --git a/tests/openwisp2/settings.py b/tests/openwisp2/settings.py index d6ab574..9afd06d 100644 --- a/tests/openwisp2/settings.py +++ b/tests/openwisp2/settings.py @@ -13,7 +13,6 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'openwisp_utils.admin_theme', 'openwisp_users.accounts', # all-auth 'django.contrib.sites', @@ -26,7 +25,9 @@ 'openwisp_users', 'openwisp_ipam', # admin + 'openwisp_utils.admin_theme', 'django.contrib.admin', + 'admin_auto_filters', # rest framework 'rest_framework', 'rest_framework.authtoken',