| 1 |
from djangorestframework_camel_case.parser import CamelCaseJSONParser |
| 2 |
from djangorestframework_camel_case.render import CamelCaseJSONRenderer |
| 3 |
from inflection import camelize |
| 4 |
from rest_framework import generics, status |
| 5 |
from rest_framework.generics import get_object_or_404 |
| 6 |
from rest_framework.pagination import PageNumberPagination |
| 7 |
from rest_framework.parsers import FileUploadParser, FormParser |
| 8 |
|
| 9 |
from drf_yasg import openapi |
| 10 |
from drf_yasg.inspectors import SwaggerAutoSchema |
| 11 |
from drf_yasg.utils import swagger_auto_schema |
| 12 |
from snippets.models import Snippet, SnippetViewer |
| 13 |
from snippets.serializers import SnippetSerializer, SnippetViewerSerializer |
| 14 |
|
| 15 |
|
| 16 |
class CamelCaseOperationIDAutoSchema(SwaggerAutoSchema): |
| 17 |
def get_operation_id(self, operation_keys): |
| 18 |
operation_id = super(CamelCaseOperationIDAutoSchema, self).get_operation_id( |
| 19 |
operation_keys |
| 20 |
) |
| 21 |
return camelize(operation_id, uppercase_first_letter=False) |
| 22 |
|
| 23 |
|
| 24 |
class SnippetList(generics.ListCreateAPIView): |
| 25 |
"""SnippetList classdoc""" |
| 26 |
|
| 27 |
queryset = Snippet.objects.all() |
| 28 |
serializer_class = SnippetSerializer |
| 29 |
|
| 30 |
parser_classes = (FormParser, CamelCaseJSONParser, FileUploadParser) |
| 31 |
renderer_classes = (CamelCaseJSONRenderer,) |
| 32 |
swagger_schema = CamelCaseOperationIDAutoSchema |
| 33 |
|
| 34 |
def perform_create(self, serializer): |
| 35 |
serializer.save(owner=self.request.user) |
| 36 |
|
| 37 |
def post(self, request, *args, **kwargs): |
| 38 |
"""post method docstring""" |
| 39 |
return super(SnippetList, self).post(request, *args, **kwargs) |
| 40 |
|
| 41 |
@swagger_auto_schema( |
| 42 |
operation_id="snippets_delete_bulk", |
| 43 |
request_body=openapi.Schema( |
| 44 |
type=openapi.TYPE_OBJECT, |
| 45 |
properties={ |
| 46 |
"body": openapi.Schema( |
| 47 |
type=openapi.TYPE_STRING, |
| 48 |
description="this should not crash (request body on DELETE method)", |
| 49 |
) |
| 50 |
}, |
| 51 |
), |
| 52 |
) |
| 53 |
def delete(self, *args, **kwargs): |
| 54 |
"""summary from docstring |
| 55 |
|
| 56 |
description body is here, summary is not included |
| 57 |
""" |
| 58 |
pass |
| 59 |
|
| 60 |
|
| 61 |
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView): |
| 62 |
""" |
| 63 |
SnippetDetail classdoc |
| 64 |
|
| 65 |
put: |
| 66 |
put class docstring |
| 67 |
|
| 68 |
patch: |
| 69 |
patch class docstring |
| 70 |
""" |
| 71 |
|
| 72 |
queryset = Snippet.objects.all() |
| 73 |
serializer_class = SnippetSerializer |
| 74 |
pagination_class = None |
| 75 |
|
| 76 |
parser_classes = (CamelCaseJSONParser,) |
| 77 |
renderer_classes = (CamelCaseJSONRenderer,) |
| 78 |
swagger_schema = CamelCaseOperationIDAutoSchema |
| 79 |
|
| 80 |
def patch(self, request, *args, **kwargs): |
| 81 |
"""patch method docstring""" |
| 82 |
return super(SnippetDetail, self).patch(request, *args, **kwargs) |
| 83 |
|
| 84 |
@swagger_auto_schema( |
| 85 |
manual_parameters=[ |
| 86 |
openapi.Parameter( |
| 87 |
name="id", |
| 88 |
in_=openapi.IN_PATH, |
| 89 |
type=openapi.TYPE_INTEGER, |
| 90 |
description="path parameter override", |
| 91 |
required=True, |
| 92 |
), |
| 93 |
], |
| 94 |
responses={ |
| 95 |
status.HTTP_204_NO_CONTENT: openapi.Response( |
| 96 |
description="this should not crash (response object with no schema)" |
| 97 |
) |
| 98 |
}, |
| 99 |
) |
| 100 |
def delete(self, request, *args, **kwargs): |
| 101 |
"""delete method docstring""" |
| 102 |
return super(SnippetDetail, self).patch(request, *args, **kwargs) |
| 103 |
|
| 104 |
|
| 105 |
class SnippetViewerList(generics.ListAPIView): |
| 106 |
"""SnippetViewerList classdoc""" |
| 107 |
|
| 108 |
serializer_class = SnippetViewerSerializer |
| 109 |
pagination_class = PageNumberPagination |
| 110 |
|
| 111 |
parser_classes = (FormParser, CamelCaseJSONParser, FileUploadParser) |
| 112 |
renderer_classes = (CamelCaseJSONRenderer,) |
| 113 |
swagger_schema = CamelCaseOperationIDAutoSchema |
| 114 |
lookup_url_kwarg = "snippet_pk" |
| 115 |
|
| 116 |
def get_object(self): |
| 117 |
queryset = Snippet.objects.all() |
| 118 |
|
| 119 |
# Perform the lookup filtering. |
| 120 |
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field |
| 121 |
|
| 122 |
filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]} |
| 123 |
obj = get_object_or_404(queryset, **filter_kwargs) |
| 124 |
|
| 125 |
# May raise a permission denied |
| 126 |
self.check_object_permissions(self.request, obj) |
| 127 |
|
| 128 |
return obj |
| 129 |
|
| 130 |
def get_queryset(self): |
| 131 |
return SnippetViewer.objects.filter(snippet=self.get_object()) |