From fff40b19de6286a2101034ec0c6609e9c537acd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petra=20C=CC=8Ci=CC=81halova=CC=81?= Date: Sat, 3 Aug 2024 11:30:57 +0200 Subject: [PATCH] set the default value for the 'order_by' query param via 'get()' method --- rbac/management/group/view.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rbac/management/group/view.py b/rbac/management/group/view.py index 44aafa039..5354385f2 100644 --- a/rbac/management/group/view.py +++ b/rbac/management/group/view.py @@ -74,6 +74,7 @@ ROLES_KEY = "roles" EXCLUDE_KEY = "exclude" ORDERING_PARAM = "order_by" +NAME_KEY = "name" PRINCIPAL_TYPE_KEY = "principal_type" PRINCIPAL_USERNAME_KEY = "principal_username" VALID_ROLE_ORDER_FIELDS = list(RoleViewSet.ordering_fields) @@ -1066,10 +1067,8 @@ def obtain_roles(self, request, group): roles = group.roles_with_access() if exclude == "false" else self.obtain_roles_with_exclusion(request, group) filtered_roles = self.filtered_roles(roles, request) annotated_roles = filtered_roles.annotate(policyCount=Count("policies", distinct=True)) - # add default order by name - order_field = "name" - if ORDERING_PARAM in request.query_params: - order_field = request.query_params.get(ORDERING_PARAM) + + order_field = request.query_params.get(ORDERING_PARAM, NAME_KEY) ordered_roles = self.order_queryset(annotated_roles, VALID_ROLE_ORDER_FIELDS, order_field) return [RoleMinimumSerializer(role).data for role in ordered_roles]