| 1 |
import datetime |
| 2 |
|
| 3 |
from django.utils.decorators import method_decorator |
| 4 |
from django_filters.rest_framework import DjangoFilterBackend |
| 5 |
from rest_framework import viewsets |
| 6 |
from rest_framework.filters import OrderingFilter |
| 7 |
from rest_framework.pagination import LimitOffsetPagination |
| 8 |
from rest_framework.parsers import FileUploadParser, MultiPartParser |
| 9 |
from rest_framework.response import Response |
| 10 |
|
| 11 |
from articles import serializers |
| 12 |
from articles.models import Article |
| 13 |
from drf_yasg import openapi |
| 14 |
from drf_yasg.app_settings import swagger_settings |
| 15 |
from drf_yasg.inspectors import ( |
| 16 |
DrfAPICompatInspector, |
| 17 |
FieldInspector, |
| 18 |
NotHandled, |
| 19 |
SwaggerAutoSchema, |
| 20 |
) |
| 21 |
from drf_yasg.utils import no_body, swagger_auto_schema |
| 22 |
|
| 23 |
|
| 24 |
class DjangoFilterDescriptionInspector(DrfAPICompatInspector): |
| 25 |
def get_filter_parameters(self, filter_backend): |
| 26 |
if isinstance(filter_backend, DjangoFilterBackend): |
| 27 |
result = super( |
| 28 |
DjangoFilterDescriptionInspector, self |
| 29 |
).get_filter_parameters(filter_backend) |
| 30 |
for param in result: |
| 31 |
if ( |
| 32 |
not param.get("description", "") |
| 33 |
or param.get("description") == param.name |
| 34 |
): |
| 35 |
param.description = ( |
| 36 |
"Filter the returned list by {field_name}".format( |
| 37 |
field_name=param.name |
| 38 |
) |
| 39 |
) |
| 40 |
|
| 41 |
return result |
| 42 |
|
| 43 |
return NotHandled |
| 44 |
|
| 45 |
|
| 46 |
class NoSchemaTitleInspector(FieldInspector): |
| 47 |
def process_result(self, result, method_name, obj, **kwargs): |
| 48 |
# remove the `title` attribute of all Schema objects |
| 49 |
if isinstance(result, openapi.Schema.OR_REF): |
| 50 |
# traverse any references and alter the Schema object in place |
| 51 |
schema = openapi.resolve_ref(result, self.components) |
| 52 |
schema.pop("title", None) |
| 53 |
|
| 54 |
# no ``return schema`` here, because it would mean we always generate |
| 55 |
# an inline `object` instead of a definition reference |
| 56 |
|
| 57 |
# return back the same object that we got - i.e. a reference if we got a |
| 58 |
# reference |
| 59 |
return result |
| 60 |
|
| 61 |
|
| 62 |
class NoTitleAutoSchema(SwaggerAutoSchema): |
| 63 |
field_inspectors = [ |
| 64 |
NoSchemaTitleInspector |
| 65 |
] + swagger_settings.DEFAULT_FIELD_INSPECTORS |
| 66 |
|
| 67 |
|
| 68 |
class NoPagingAutoSchema(NoTitleAutoSchema): |
| 69 |
def should_page(self): |
| 70 |
return False |
| 71 |
|
| 72 |
|
| 73 |
class ArticlePagination(LimitOffsetPagination): |
| 74 |
default_limit = 5 |
| 75 |
max_limit = 25 |
| 76 |
|
| 77 |
|
| 78 |
@method_decorator( |
| 79 |
name="list", |
| 80 |
decorator=swagger_auto_schema( |
| 81 |
operation_description=( |
| 82 |
"description from swagger_auto_schema via method_decorator" |
| 83 |
), |
| 84 |
filter_inspectors=[DjangoFilterDescriptionInspector], |
| 85 |
), |
| 86 |
) |
| 87 |
class ArticleViewSet(viewsets.ModelViewSet): |
| 88 |
""" |
| 89 |
ArticleViewSet class docstring |
| 90 |
|
| 91 |
retrieve: |
| 92 |
retrieve class docstring |
| 93 |
|
| 94 |
destroy: |
| 95 |
destroy class docstring |
| 96 |
|
| 97 |
partial_update: |
| 98 |
partial_update class docstring |
| 99 |
""" |
| 100 |
|
| 101 |
queryset = Article.objects.all() |
| 102 |
lookup_field = "slug" |
| 103 |
lookup_value_regex = r"[a-z0-9]+(?:-[a-z0-9]+)" |
| 104 |
serializer_class = serializers.ArticleSerializer |
| 105 |
|
| 106 |
pagination_class = ArticlePagination |
| 107 |
filter_backends = (DjangoFilterBackend, OrderingFilter) |
| 108 |
filterset_fields = ("title",) |
| 109 |
# django-filter 1.1 compatibility; was renamed to filterset_fields in 2.0 |
| 110 |
# TODO: remove when dropping support for Django 1.11 |
| 111 |
filter_fields = filterset_fields |
| 112 |
ordering_fields = ("date_modified", "date_created") |
| 113 |
ordering = ("date_created",) |
| 114 |
|
| 115 |
swagger_schema = NoTitleAutoSchema |
| 116 |
|
| 117 |
from rest_framework.decorators import action |
| 118 |
|
| 119 |
@swagger_auto_schema( |
| 120 |
auto_schema=NoPagingAutoSchema, |
| 121 |
filter_inspectors=[DjangoFilterDescriptionInspector], |
| 122 |
) |
| 123 |
@action(detail=False, methods=["get"]) |
| 124 |
def today(self, request): |
| 125 |
today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min) |
| 126 |
today_max = datetime.datetime.combine(datetime.date.today(), datetime.time.max) |
| 127 |
articles = ( |
| 128 |
self.get_queryset().filter(date_created__range=(today_min, today_max)).all() |
| 129 |
) |
| 130 |
serializer = self.serializer_class(articles, many=True) |
| 131 |
return Response(serializer.data) |
| 132 |
|
| 133 |
@swagger_auto_schema( |
| 134 |
method="get", operation_description="image GET description override" |
| 135 |
) |
| 136 |
@swagger_auto_schema(method="post", request_body=serializers.ImageUploadSerializer) |
| 137 |
@swagger_auto_schema( |
| 138 |
method="delete", |
| 139 |
manual_parameters=[ |
| 140 |
openapi.Parameter( |
| 141 |
name="delete_form_param", |
| 142 |
in_=openapi.IN_FORM, |
| 143 |
type=openapi.TYPE_INTEGER, |
| 144 |
description="this should not crash (form parameter on DELETE method)", |
| 145 |
) |
| 146 |
], |
| 147 |
) |
| 148 |
@action( |
| 149 |
detail=True, |
| 150 |
methods=["get", "post", "delete"], |
| 151 |
parser_classes=(MultiPartParser, FileUploadParser), |
| 152 |
) |
| 153 |
def image(self, request, slug=None): |
| 154 |
""" |
| 155 |
image method docstring |
| 156 |
""" |
| 157 |
pass |
| 158 |
|
| 159 |
@swagger_auto_schema(request_body=no_body, operation_id="no_body_test") |
| 160 |
def update(self, request, *args, **kwargs): |
| 161 |
"""update method docstring""" |
| 162 |
return super(ArticleViewSet, self).update(request, *args, **kwargs) |
| 163 |
|
| 164 |
@swagger_auto_schema( |
| 165 |
operation_description="partial_update description override", |
| 166 |
responses={404: "slug not found"}, |
| 167 |
operation_summary="partial_update summary", |
| 168 |
deprecated=True, |
| 169 |
) |
| 170 |
def partial_update(self, request, *args, **kwargs): |
| 171 |
"""partial_update method docstring""" |
| 172 |
return super(ArticleViewSet, self).partial_update(request, *args, **kwargs) |
| 173 |
|
| 174 |
def destroy(self, request, *args, **kwargs): |
| 175 |
"""destroy method docstring""" |
| 176 |
return super(ArticleViewSet, self).destroy(request, *args, **kwargs) |