Skip to content

Commit

Permalink
[feature] Added autocomplete filters
Browse files Browse the repository at this point in the history
  • Loading branch information
pandafy committed Nov 30, 2022
1 parent e5cda5b commit 9b8d29f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions openwisp_ipam/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
from django.utils.translation import gettext_lazy as _
from openwisp_users.multitenancy import MultitenantAdminMixin, MultitenantOrgFilter
from openwisp_utils.admin import TimeReadonlyAdminMixin
from openwisp_utils.admin_theme.filters import AutocompleteFilter
from rest_framework.exceptions import PermissionDenied
from reversion.admin import VersionAdmin

from .api.utils import AuthorizeCSVOrgManaged, CsvImportAPIException
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')
Expand All @@ -46,7 +48,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']
Expand Down Expand Up @@ -211,7 +213,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 = [SubnetFilter, SubnetOrganizationFilter]
search_fields = ['ip_address']
autocomplete_fields = ['subnet']
multitenant_parent = 'subnet'
Expand Down
15 changes: 15 additions & 0 deletions openwisp_ipam/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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'


class SubnetOrganizationFilter(MultitenantRelatedOrgFilter):
field_name = 'organization'
parameter_name = 'subnet__organization'
rel_model = Subnet
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
coveralls
django-extensions>=2.0.7
openwisp-utils[qa] @ https://github.com/openwisp/openwisp-utils/tarball/master
openwisp-utils[qa] @ https://github.com/openwisp/openwisp-utils/tarball/fix-autocomplete-filter
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
openwisp-users @ https://github.com/openwisp/openwisp-users/tarball/master
openwisp-utils[rest] @ https://github.com/openwisp/openwisp-utils/tarball/master
openwisp-users @ https://github.com/openwisp/openwisp-users/tarball/issues/270-organization-autocomplete
openwisp-utils[rest] @ https://github.com/openwisp/openwisp-utils/tarball/fix-autocomplete-filter
django>=3.0.0,<4.1.0
openpyxl~=3.0.9
django-reversion~=4.0.1
3 changes: 2 additions & 1 deletion tests/openwisp2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down

0 comments on commit 9b8d29f

Please sign in to comment.