Closes #9608: Move from drf-yasg to spectacular

Co-authored-by: arthanson <worldnomad@gmail.com>
Co-authored-by: jeremystretch <jstretch@netboxlabs.com>
This commit is contained in:
Arthur Hanson
2023-03-30 11:32:59 -07:00
committed by GitHub
parent 1be626e5ee
commit ecd0c56554
35 changed files with 514 additions and 340 deletions
+5 -3
View File
@@ -1,6 +1,7 @@
from django.contrib.auth.models import Group, User
from django.contrib.contenttypes.models import ContentType
from drf_yasg.utils import swagger_serializer_method
from drf_spectacular.utils import extend_schema_field
from drf_spectacular.types import OpenApiTypes
from rest_framework import serializers
from netbox.api.fields import ContentTypeField
@@ -30,6 +31,7 @@ class NestedUserSerializer(WritableNestedSerializer):
model = User
fields = ['id', 'url', 'display', 'username']
@extend_schema_field(OpenApiTypes.STR)
def get_display(self, obj):
if full_name := obj.get_full_name():
return f"{obj.username} ({full_name})"
@@ -57,10 +59,10 @@ class NestedObjectPermissionSerializer(WritableNestedSerializer):
model = ObjectPermission
fields = ['id', 'url', 'display', 'name', 'enabled', 'object_types', 'groups', 'users', 'actions']
@swagger_serializer_method(serializer_or_field=serializers.ListField)
@extend_schema_field(serializers.ListField)
def get_groups(self, obj):
return [g.name for g in obj.groups.all()]
@swagger_serializer_method(serializer_or_field=serializers.ListField)
@extend_schema_field(serializers.ListField)
def get_users(self, obj):
return [u.username for u in obj.users.all()]
+3
View File
@@ -1,6 +1,8 @@
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.contrib.contenttypes.models import ContentType
from drf_spectacular.utils import extend_schema_field
from drf_spectacular.types import OpenApiTypes
from rest_framework import serializers
from netbox.api.fields import ContentTypeField, IPNetworkSerializer, SerializedPKRelatedField
@@ -47,6 +49,7 @@ class UserSerializer(ValidatedModelSerializer):
return user
@extend_schema_field(OpenApiTypes.STR)
def get_display(self, obj):
if full_name := obj.get_full_name():
return f"{obj.username} ({full_name})"
+8 -3
View File
@@ -1,6 +1,8 @@
from django.contrib.auth import authenticate
from django.contrib.auth.models import Group, User
from django.db.models import Count
from drf_spectacular.utils import extend_schema
from drf_spectacular.types import OpenApiTypes
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
@@ -55,9 +57,6 @@ class TokenViewSet(NetBoxModelViewSet):
Limit the non-superusers to their own Tokens.
"""
queryset = super().get_queryset()
# Workaround for schema generation (drf_yasg)
if getattr(self, 'swagger_fake_view', False):
return queryset.none()
if not self.request.user.is_authenticated:
return queryset.none()
if self.request.user.is_superuser:
@@ -71,6 +70,7 @@ class TokenProvisionView(APIView):
"""
permission_classes = []
# @extend_schema(methods=["post"], responses={201: serializers.TokenSerializer})
def post(self, request):
serializer = serializers.TokenProvisionSerializer(data=request.data)
serializer.is_valid()
@@ -93,6 +93,9 @@ class TokenProvisionView(APIView):
return Response(data, status=HTTP_201_CREATED)
def get_serializer_class(self):
return serializers.TokenSerializer
#
# ObjectPermissions
@@ -117,6 +120,7 @@ class UserConfigViewSet(ViewSet):
def get_queryset(self):
return UserConfig.objects.filter(user=self.request.user)
@extend_schema(responses={200: OpenApiTypes.OBJECT})
def list(self, request):
"""
Return the UserConfig for the currently authenticated User.
@@ -125,6 +129,7 @@ class UserConfigViewSet(ViewSet):
return Response(userconfig.data)
@extend_schema(methods=["patch"], responses={201: OpenApiTypes.OBJECT})
def patch(self, request):
"""
Update the UserConfig for the currently authenticated User.