File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -980,6 +980,18 @@ Pagination is provided using page number pagination; default page size is
980980
981981 GET /api/v1/users/user/<user_pk>/radius-group/
982982
983+ It supports sorting by organization id and organization slug.
984+
985+ Filters
986+ """""""
987+
988+ ========================= ============================
989+ Filter Parameter Description
990+ ========================= ============================
991+ group__organization Filter organizations by id
992+ group__organization__slug Filter organizations by slug
993+ ========================= ============================
994+
983995POST
984996^^^^
985997
Original file line number Diff line number Diff line change @@ -373,8 +373,12 @@ class Meta:
373373
374374 def __init__ (self , * args , ** kwargs ):
375375 super ().__init__ (* args , ** kwargs )
376- view = self .context .get ("view" )
377- self ._user = view .get_parent_queryset ().first ()
376+ if self .context .get ("view" ) and getattr (
377+ self .context ["view" ], "get_parent_queryset" , None
378+ ):
379+ self ._user = self .context ["view" ].get_parent_queryset ().first ()
380+ else :
381+ self ._user = None
378382 if self ._user :
379383 self .fields ["group" ].queryset = self .fields ["group" ].queryset .filter (
380384 organization_id__in = self ._user .organizations_dict .keys ()
Original file line number Diff line number Diff line change @@ -983,6 +983,18 @@ def get_organization_queryset(self, qs):
983983 return qs .filter (** filter_kwargs ).distinct ()
984984
985985
986+ class RadiusUserGroupFilter (OrganizationManagedFilter , filters .FilterSet ):
987+ """
988+ Filter RADIUS groups by organizations managed by the user.
989+ """
990+
991+ organization_slug = None
992+
993+ class Meta (OrganizationManagedFilter .Meta ):
994+ model = RadiusUserGroup
995+ fields = ["group__organization" , "group__organization__slug" ]
996+
997+
986998@method_decorator (
987999 name = "get" ,
9881000 decorator = swagger_auto_schema (
@@ -1001,6 +1013,8 @@ def get_organization_queryset(self, qs):
10011013)
10021014class RadiusUserGroupListCreateView (BaseRadiusUserGroupView , ListCreateAPIView ):
10031015 pagination_class = RadiusGroupPaginator
1016+ filter_backends = [DjangoFilterBackend ]
1017+ filterset_class = RadiusUserGroupFilter
10041018
10051019
10061020radius_user_group_list = RadiusUserGroupListCreateView .as_view ()
Original file line number Diff line number Diff line change @@ -1331,6 +1331,16 @@ def test_radius_user_group_list(self):
13311331 self .assertEqual (data ["count" ], 1 )
13321332 self .assertEqual (data ["results" ][0 ]["id" ], str (rug .pk ))
13331333
1334+ with self .subTest ("Filter by organization query parameter" ):
1335+ # requesting the same organization that the existing group belongs to
1336+ response = self .client .get (url + f"?group__organization={ org1 .pk } " )
1337+ self .assertEqual (response .status_code , status .HTTP_200_OK )
1338+ self .assertEqual (response .data ["count" ], 1 )
1339+ # a different organization should yield no results
1340+ response = self .client .get (url + f"?group__organization={ org2 .pk } " )
1341+ self .assertEqual (response .status_code , status .HTTP_400_BAD_REQUEST )
1342+ self .assertIn ("group__organization" , response .data )
1343+
13341344 with self .subTest ("Create RadiusUserGroup" ):
13351345 org1_power_users = RadiusGroup .objects .get (
13361346 organization = org1 , name = "org-1-power-users"
You can’t perform that action at this time.
0 commit comments