Skip to content

Commit

Permalink
Fix some openapi definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
amanning9 committed May 14, 2024
1 parent a061d19 commit 5157956
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions jasmin_services/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class ServicesViewSet(
serializer_class = serializers.ServiceSerializer
action_serializers = {
"list": serializers.ServiceListSerializer,
"roles": serializers.RoleSerializer,
}
required_scopes = ["jasmin.services.services.all"]
filterset_fields = ["category", "hidden", "ceda_managed"]
Expand All @@ -41,10 +40,9 @@ class ServicesViewSet(
required=False,
type=dt.date,
description="ISO Date on which you would like to know the active roles for a service.",
)
],
responses=serializers.RoleSerializer(many=True),
)
),
]
),
)
class RolesNestedUnderServicesViewSet(rf_viewsets.ReadOnlyModelViewSet):
"""View roles for a service."""
Expand All @@ -54,6 +52,10 @@ class RolesNestedUnderServicesViewSet(rf_viewsets.ReadOnlyModelViewSet):
lookup_field = "name"

def get_queryset(self):
# If we are generating swagger definitions, return the correct
# queryset to allow types to be infered without error.
if getattr(self, "swagger_fake_view", False):
return models.Role.objects.none()

# Add query to lookup roleholders by date.
date_string = self.request.query_params.get("on_date", False)
Expand Down Expand Up @@ -97,6 +99,26 @@ def get_queryset(self):
return queryset


@drf_spectacular.utils.extend_schema_view(
list=drf_spectacular.utils.extend_schema(
parameters=[
drf_spectacular.utils.OpenApiParameter(
name="category_name",
type=str,
location=drf_spectacular.utils.OpenApiParameter.PATH,
),
],
),
retrieve=drf_spectacular.utils.extend_schema(
parameters=[
drf_spectacular.utils.OpenApiParameter(
name="category_name",
type=str,
location=drf_spectacular.utils.OpenApiParameter.PATH,
),
]
),
)
class ServicesNestedUnderCategoriesViewSet(ServicesViewSet):
"""Viewset to allow services to be nested under categories.
Expand All @@ -109,16 +131,39 @@ class ServicesNestedUnderCategoriesViewSet(ServicesViewSet):

def get_queryset(self):
queryset = super().get_queryset()

# If we are generating swagger definitions, return the correct
# queryset to allow types to be infered without error.
if getattr(self, "swagger_fake_view", False):
return queryset.none()

return queryset.filter(category__name=self.kwargs["category_name"])


@drf_spectacular.utils.extend_schema_view(
list=drf_spectacular.utils.extend_schema(
parameters=[
drf_spectacular.utils.OpenApiParameter(
name="user_username",
required=True,
type=str,
location=drf_spectacular.utils.OpenApiParameter.PATH,
)
],
)
)
class UserServicesViewSet(rf_mixins.ListModelMixin, rf_viewsets.GenericViewSet):
"""Get the services assocated with a user."""

required_scopes = ["jasmin.services.userservices.all"]
serializer_class = serializers.ServiceListSerializer

def get_queryset(self):
# If we are generating swagger definitions, return the correct
# queryset to allow types to be infered without error.
if getattr(self, "swagger_fake_view", False):
return models.Service.objects.none()

return models.Service.objects.filter(
role__access__user__username=self.kwargs["user_username"],
role__access__grant__revoked=False,
Expand Down

0 comments on commit 5157956

Please sign in to comment.