Repository: openwisp/openwisp-radius (language: python)

Issue:
[feature] Switch to autocomplete filters and autocomplete organization fields
Like https://github.com/openwisp/openwisp-firmware-upgrader/issues/214 and https://github.com/openwisp/openwisp-network-topology/issues/158.
[feature] Switch to autocomplete filters and autocomplete organization fields
Like https://github.com/openwisp/openwisp-firmware-upgrader/issues/214 and https://github.com/openwisp/openwisp-network-topology/issues/158.


Discussion / hints:

Fix patch:
diff --git a/openwisp_radius/admin.py b/openwisp_radius/admin.py
index 74a40f32..5a76f433 100644
--- a/openwisp_radius/admin.py
+++ b/openwisp_radius/admin.py
@@ -74,7 +74,7 @@ class RadiusCheckAdmin(MultitenantAdminMixin, TimeStampedEditableAdmin):
     ]
     search_fields = ['username', 'value']
     list_filter = [
-        ('organization', MultitenantOrgFilter),
+        MultitenantOrgFilter,
         'created',
         'modified',
     ]
@@ -89,7 +89,7 @@ class RadiusCheckAdmin(MultitenantAdminMixin, TimeStampedEditableAdmin):
         'created',
         'modified',
     ]
-    autocomplete_fields = ['user']
+    autocomplete_fields = ('user',)
 
 
 @admin.register(RadiusReply)
@@ -104,8 +104,7 @@ class RadiusReplyAdmin(MultitenantAdminMixin, TimeStampedEditableAdmin):
         'created',
         'modified',
     ]
-    autocomplete_fields = ['user']
-    list_filter = (('organization', MultitenantOrgFilter),)
+    list_filter = (MultitenantOrgFilter,)
     fields = [
         'mode',
         'organization',
@@ -117,6 +116,7 @@ class RadiusReplyAdmin(MultitenantAdminMixin, TimeStampedEditableAdmin):
         'created',
         'modified',
     ]
+    autocomplete_fields = ('user',)
 
 
 BaseAccounting = ReadOnlyAdmin if not app_settings.EDITABLE_ACCOUNTING else ModelAdmin
@@ -143,7 +143,7 @@ class RadiusAccountingAdmin(OrganizationFirstMixin, BaseAccounting):
         'called_station_id',
         'nas_ip_address',
     ]
-    list_filter = ['start_time', 'stop_time', ('organization', MultitenantOrgFilter)]
+    list_filter = ['start_time', 'stop_time', MultitenantOrgFilter]
     ordering = ['-start_time']
 
 
@@ -177,7 +177,7 @@ class NasAdmin(MultitenantAdminMixin, TimeStampedEditableAdmin):
         'created',
         'modified',
     ]
-    list_filter = (('organization', MultitenantOrgFilter),)
+    list_filter = (MultitenantOrgFilter,)
 
 
 class RadiusGroupCheckInline(TimeReadonlyAdminMixin, StackedInline):
@@ -204,7 +204,7 @@ class RadiusGroupAdmin(OrganizationFirstMixin, TimeStampedEditableAdmin):
         'modified',
     ]
     search_fields = ['name']
-    list_filter = (('organization', MultitenantOrgFilter),)
+    list_filter = (MultitenantOrgFilter,)
     inlines = [RadiusGroupCheckInline, RadiusGroupReplyInline]
     select_related = ('organization',)
 
@@ -325,7 +325,7 @@ class RadiusPostAuthAdmin(OrganizationFirstMixin, BasePostAuth):
     list_filter = [
         'date',
         'reply',
-        ('organization', MultitenantOrgFilter),
+        MultitenantOrgFilter,
     ]
     search_fields = ['username', 'reply', 'calling_station_id', 'called_station_id']
     exclude = ['id']
@@ -358,7 +358,7 @@ class RadiusBatchAdmin(MultitenantAdminMixin, TimeStampedEditableAdmin):
     ]
     list_filter = [
         'strategy',
-        ('organization', MultitenantOrgFilter),
+        MultitenantOrgFilter,
     ]
     search_fields = ['name']
     form = RadiusBatchForm
@@ -625,7 +625,9 @@ def get_formset(self, request, obj=None, **kwargs):
 if settings.DEBUG:
 
     @admin.register(RadiusToken)
-    class RadiusTokenAdmin(ModelAdmin):
+    class RadiusTokenAdmin(MultitenantAdminMixin, TimeStampedEditableAdmin):
         list_display = ['key', 'user', 'created']
         fields = ['user', 'organization', 'can_auth']
         ordering = ('-created',)
+        list_filter = [MultitenantOrgFilter]
+        autocomplete_fields = ('user',)
diff --git a/setup.py b/setup.py
index ec15a030..958328cf 100644
--- a/setup.py
+++ b/setup.py
@@ -39,7 +39,7 @@
         (
             'openwisp-users '
             '@ https://github.com/openwisp/openwisp-users/tarball/'
-            '3ec78ab47395b5aa1e4f27fd9670b57b6f0a3acb'
+            'master'
         ),
         'openwisp-utils[rest,celery] @ '
         'https://github.com/openwisp/openwisp-utils/tarball/master',


Test patch:
diff --git a/openwisp_radius/tests/test_admin.py b/openwisp_radius/tests/test_admin.py
index 38f4e3d5..ddcc76ae 100644
--- a/openwisp_radius/tests/test_admin.py
+++ b/openwisp_radius/tests/test_admin.py
@@ -886,13 +886,14 @@ def test_radiuscheck_queryset(self):
             hidden=[data['rc2'].username, data['org2'].name, data['rc3'].username],
         )
 
-    def test_radiuscheck_organization_fk_queryset(self):
+    def test_radiuscheck_organization_fk_autocomplete_view(self):
         data = self._create_multitenancy_test_env()
         self._test_multitenant_admin(
-            url=reverse(f'admin:{self.app_label}_radiuscheck_add'),
+            url=self._get_autocomplete_view_path(
+                self.app_label, 'radiuscheck', 'organization'
+            ),
             visible=[data['org1'].name],
             hidden=[data['org2'].name, data['inactive']],
-            select_widget=True,
             administrator=True,
         )
 
@@ -918,13 +919,14 @@ def test_radiusreply_queryset(self):
             hidden=[data['rr2'].username, data['org2'], data['rr3'].username],
         )
 
-    def test_radiusreply_organization_fk_queryset(self):
+    def test_radiusreply_organization_fk_autocomplete_view(self):
         data = self._create_multitenancy_test_env()
         self._test_multitenant_admin(
-            url=reverse(f'admin:{self.app_label}_radiusreply_add'),
+            url=self._get_autocomplete_view_path(
+                self.app_label, 'radiusreply', 'organization'
+            ),
             visible=[data['org1'].name],
             hidden=[data['org2'].name, data['inactive']],
-            select_widget=True,
             administrator=True,
         )
 
@@ -950,13 +952,14 @@ def test_radiusgroup_queryset(self):
             hidden=[data['org2'].name, data['rg2'].name, data['rg3'].name],
         )
 
-    def test_radiusgroup_organization_fk_queryset(self):
+    def test_radiusgroup_organization_fk_autocomplete_view(self):
         data = self._create_multitenancy_test_env()
         self._test_multitenant_admin(
-            url=(reverse(f'admin:{self.app_label}_radiusgroup_add')),
+            url=self._get_autocomplete_view_path(
+                self.app_label, 'radiusgroup', 'organization'
+            ),
             visible=[data['org1'].name],
             hidden=[data['org2'].name, data['inactive']],
-            select_widget=True,
             administrator=True,
         )
 
@@ -968,13 +971,12 @@ def test_nas_queryset(self):
             hidden=[data['nas2'].name, data['org2'].name, data['nas3'].name],
         )
 
-    def test_nas_organization_fk_queryset(self):
+    def test_nas_organization_fk_autocomplete_view(self):
         data = self._create_multitenancy_test_env()
         self._test_multitenant_admin(
-            url=reverse(f'admin:{self.app_label}_nas_add'),
+            url=self._get_autocomplete_view_path(self.app_label, 'nas', 'organization'),
             visible=[data['org1'].name],
             hidden=[data['org2'].name, data['inactive']],
-            select_widget=True,
             administrator=True,
         )
 
@@ -994,13 +996,14 @@ def test_radiusbatch_queryset(self):
             hidden=[data['rb2'].name, data['org2'].name, data['rb3'].name],
         )
 
-    def test_radiusbatch_organization_fk_queryset(self):
+    def test_radiusbatch_organization_fk_autocomplete_view(self):
         data = self._create_multitenancy_test_env()
         self._test_multitenant_admin(
-            url=reverse(f'admin:{self.app_label}_radiusbatch_add'),
+            url=self._get_autocomplete_view_path(
+                self.app_label, 'radiusbatch', 'organization'
+            ),
             visible=[data['org1'].name],
             hidden=[data['org2'].name, data['inactive']],
-            select_widget=True,
             administrator=True,
         )
 
diff --git a/tests/openwisp2/settings.py b/tests/openwisp2/settings.py
index 976250f6..6d92a7e4 100644
--- a/tests/openwisp2/settings.py
+++ b/tests/openwisp2/settings.py
@@ -46,6 +46,7 @@
     'openwisp_radius',
     'openwisp_users',
     # admin
+    'admin_auto_filters',
     'django.contrib.admin',
     'private_storage',
     'drf_yasg',