| 1 |
from django.contrib.auth.models import User |
| 2 |
from rest_framework import status |
| 3 |
from rest_framework.decorators import api_view |
| 4 |
from rest_framework.generics import get_object_or_404 |
| 5 |
from rest_framework.response import Response |
| 6 |
from rest_framework.views import APIView |
| 7 |
|
| 8 |
from drf_yasg import openapi |
| 9 |
from drf_yasg.utils import swagger_auto_schema |
| 10 |
from users.serializers import UserListQuerySerializer, UserSerializer |
| 11 |
|
| 12 |
|
| 13 |
class UserList(APIView): |
| 14 |
"""UserList cbv classdoc""" |
| 15 |
|
| 16 |
@swagger_auto_schema( |
| 17 |
query_serializer=UserListQuerySerializer, |
| 18 |
responses={200: UserSerializer(many=True)}, |
| 19 |
tags=["Users"], |
| 20 |
) |
| 21 |
def get(self, request): |
| 22 |
queryset = User.objects.all() |
| 23 |
serializer = UserSerializer(queryset, many=True) |
| 24 |
return Response(serializer.data) |
| 25 |
|
| 26 |
@swagger_auto_schema( |
| 27 |
operation_description="apiview post description override", |
| 28 |
request_body=openapi.Schema( |
| 29 |
type=openapi.TYPE_OBJECT, |
| 30 |
required=["username"], |
| 31 |
properties={"username": openapi.Schema(type=openapi.TYPE_STRING)}, |
| 32 |
), |
| 33 |
security=[], |
| 34 |
tags=["Users"], |
| 35 |
) |
| 36 |
def post(self, request): |
| 37 |
serializer = UserSerializer(request.data) |
| 38 |
serializer.is_valid(raise_exception=True) |
| 39 |
serializer.save() |
| 40 |
return Response(serializer.data, status=status.HTTP_201_CREATED) |
| 41 |
|
| 42 |
@swagger_auto_schema( |
| 43 |
operation_id="users_dummy", |
| 44 |
operation_description="dummy operation", |
| 45 |
tags=["Users"], |
| 46 |
) |
| 47 |
def patch(self, request): |
| 48 |
pass |
| 49 |
|
| 50 |
|
| 51 |
@swagger_auto_schema(method="put", request_body=UserSerializer, tags=["Users"]) |
| 52 |
@swagger_auto_schema( |
| 53 |
methods=["get"], |
| 54 |
manual_parameters=[ |
| 55 |
openapi.Parameter( |
| 56 |
"test", openapi.IN_QUERY, "test manual param", type=openapi.TYPE_BOOLEAN |
| 57 |
), |
| 58 |
openapi.Parameter( |
| 59 |
"test_array", |
| 60 |
openapi.IN_QUERY, |
| 61 |
"test query array arg", |
| 62 |
type=openapi.TYPE_ARRAY, |
| 63 |
items=openapi.Items(type=openapi.TYPE_STRING), |
| 64 |
required=True, |
| 65 |
collection_format="multi", |
| 66 |
), |
| 67 |
], |
| 68 |
responses={ |
| 69 |
200: openapi.Response("response description", UserSerializer), |
| 70 |
}, |
| 71 |
tags=["Users"], |
| 72 |
) |
| 73 |
@api_view(["GET", "PUT"]) |
| 74 |
def user_detail(request, pk): |
| 75 |
"""user_detail fbv docstring""" |
| 76 |
user = get_object_or_404(User.objects, pk=pk) |
| 77 |
serializer = UserSerializer(user) |
| 78 |
return Response(serializer.data) |
| 79 |
|
| 80 |
|
| 81 |
class DummyAutoSchema: |
| 82 |
def __init__(self, *args, **kwargs): |
| 83 |
pass |
| 84 |
|
| 85 |
def get_operation(self, keys): |
| 86 |
pass |
| 87 |
|
| 88 |
|
| 89 |
@swagger_auto_schema(methods=["get"], auto_schema=DummyAutoSchema) |
| 90 |
@swagger_auto_schema(methods=["PUT"], auto_schema=None) |
| 91 |
@api_view(["GET", "PUT"]) |
| 92 |
def test_view_with_dummy_schema(request, pk): |
| 93 |
return Response({}) |