Skip to content

Commit

Permalink
Make some ui/v2 base view classes for DRY and to drive dab.rbac pagin…
Browse files Browse the repository at this point in the history
…ation (#2227)

The pagination class for dab.rbac defaults to limitoffset, whereas the new ui
is expecting page_size pagination. By defining ANSIBLE_BASE_CUSTOM_VIEW_PARENT
the rbac views will inherit from that class name and use it's pagination class.

* Add test.
* Openapi needs endpoint pieces.

No-Issue

Signed-off-by: James Tanner <[email protected]>
  • Loading branch information
jctanner authored Aug 16, 2024
1 parent ba2eb25 commit 6b8280f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
42 changes: 26 additions & 16 deletions galaxy_ng/app/api/ui_v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework import status
from rest_framework.exceptions import ValidationError
from rest_framework.decorators import action
from rest_framework import views

from ansible_base.rest_pagination.default_paginator import DefaultPaginator

Expand All @@ -23,14 +24,32 @@
from galaxy_ng.app.models.organization import Team


class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().order_by('id')
serializer_class = UserSerializer
class BaseView(views.APIView):
"""
A view to define the base properties for the views in
ansible_base.rbac. Set ANSIBLE_BASE_CUSTOM_VIEW_PARENT
to this class in settings so that the rbac endpoints
follow the defined pagination and permission classes.
"""
pagination_class = DefaultPaginator
permission_classes = [IsSuperUserOrReadOnly]

# openapi compatibility ...
def endpoint_pieces(*args, **kwargs):
return ''


class BaseViewSet(viewsets.ModelViewSet):
filter_backends = (DjangoFilterBackend,)
filterset_class = UserViewFilter
pagination_class = DefaultPaginator
permission_classes = [IsSuperUserOrReadOnly]


class UserViewSet(BaseViewSet):
queryset = User.objects.all().order_by('id')
serializer_class = UserSerializer
filterset_class = UserViewFilter

def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
Expand Down Expand Up @@ -69,23 +88,17 @@ def perform_update(self, serializer):
serializer.save()


class GroupViewSet(viewsets.ReadOnlyModelViewSet):
class GroupViewSet(BaseViewSet):
queryset = Group.objects.all().order_by('id')
serializer_class = GroupSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = GroupViewFilter
pagination_class = DefaultPaginator
permission_classes = [IsSuperUserOrReadOnly]


class OrganizationViewSet(viewsets.ModelViewSet):
class OrganizationViewSet(BaseViewSet):

queryset = Organization.objects.all().order_by('pk')
serializer_class = OrganizationSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = OrganizationFilter
pagination_class = DefaultPaginator
permission_classes = [IsSuperUserOrReadOnly]

def update(self, request, *args, **kwargs):
instance = self.get_object()
Expand All @@ -101,14 +114,11 @@ def destroy(self, request, *args, **kwargs):
return super().destroy(request, *args, **kwargs)


class TeamViewSet(viewsets.ModelViewSet):
class TeamViewSet(BaseViewSet):

queryset = Team.objects.all().order_by('id')
serializer_class = TeamSerializer
filter_backends = (DjangoFilterBackend,)
filterset_class = TeamFilter
pagination_class = DefaultPaginator
permission_classes = [IsSuperUserOrReadOnly]

def create(self, request, *args, **kwargs):

Expand Down
3 changes: 3 additions & 0 deletions galaxy_ng/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@
# RESOURCE_SERVER = {"URL": str, "SECRET_KEY": str, "VALIDATE_HTTPS": bool}

# -- ANSIBLE BASE RBAC --
# The rbac viewsets inherit from a defined base class to get their
# pagination settings
ANSIBLE_BASE_CUSTOM_VIEW_PARENT = "galaxy_ng.app.api.ui_v2.views.BaseView"
# If a role does not already exist that can give those object permissions
# then the system must create one, this is used for naming the auto-created role
ANSIBLE_BASE_ROLE_CREATOR_NAME = "{obj._meta.model_name} Creator Role"
Expand Down
11 changes: 11 additions & 0 deletions galaxy_ng/tests/integration/dab/test_dab_rbac_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest


pytestmark = pytest.mark.qa # noqa: F821


@pytest.mark.deployment_standalone
def test_dab_rbac_pagination(galaxy_client):
gc = galaxy_client("admin", ignore_cache=True)
roledefs = gc.get('_ui/v2/role_definitions/?page_size=1')
assert len(roledefs['results']) == 1

0 comments on commit 6b8280f

Please sign in to comment.