Prepare for 1.1.0 (#30)

* refactor the view inspection process to be more modular and allow recursive customization
* add operation_id argument to @swagger_auto_
* add inspections for min/max validators
* add support for URLPathVersioning and NamespaceVersioning
* integrate with djangorestframework-camel-case
* fix bugs, improve tests and documentation
This commit is contained in:
Cristi Vîjdea
2017-12-26 22:50:59 +01:00
committed by GitHub
parent 73adc49b2c
commit c89f96fcb0
52 changed files with 2167 additions and 854 deletions
+4 -3
View File
@@ -1,11 +1,12 @@
from rest_framework import serializers
from articles.models import Article
from django.utils.translation import ugettext_lazy as _
class ArticleSerializer(serializers.ModelSerializer):
references = serializers.DictField(
help_text="this is a really bad example",
help_text=_("this is a really bad example"),
child=serializers.URLField(help_text="but i needed to test these 2 fields somehow"),
read_only=True,
)
@@ -23,8 +24,8 @@ class ArticleSerializer(serializers.ModelSerializer):
'body': {'help_text': 'body serializer help_text'},
'author': {
'default': serializers.CurrentUserDefault(),
'help_text': "The ID of the user that created this article; if none is provided, "
"defaults to the currently logged in user."
'help_text': _("The ID of the user that created this article; if none is provided, "
"defaults to the currently logged in user.")
},
}
+41 -4
View File
@@ -11,11 +11,45 @@ from rest_framework.response import Response
from articles import serializers
from articles.models import Article
from drf_yasg.inspectors import SwaggerAutoSchema
from drf_yasg import openapi
from drf_yasg.app_settings import swagger_settings
from drf_yasg.inspectors import SwaggerAutoSchema, FieldInspector, CoreAPICompatInspector, NotHandled
from drf_yasg.utils import swagger_auto_schema
class NoPagingAutoSchema(SwaggerAutoSchema):
class DjangoFilterDescriptionInspector(CoreAPICompatInspector):
def get_filter_parameters(self, filter_backend):
if isinstance(filter_backend, DjangoFilterBackend):
result = super(DjangoFilterDescriptionInspector, self).get_filter_parameters(filter_backend)
for param in result:
if not param.get('description', ''):
param.description = "Filter the returned list by {field_name}".format(field_name=param.name)
return result
return NotHandled
class NoSchemaTitleInspector(FieldInspector):
def process_result(self, result, method_name, obj, **kwargs):
# remove the `title` attribute of all Schema objects
if isinstance(result, openapi.Schema.OR_REF):
# traverse any references and alter the Schema object in place
schema = openapi.resolve_ref(result, self.components)
schema.pop('title', None)
# no ``return schema`` here, because it would mean we always generate
# an inline `object` instead of a definition reference
# return back the same object that we got - i.e. a reference if we got a reference
return result
class NoTitleAutoSchema(SwaggerAutoSchema):
field_inspectors = [NoSchemaTitleInspector] + swagger_settings.DEFAULT_FIELD_INSPECTORS
class NoPagingAutoSchema(NoTitleAutoSchema):
def should_page(self):
return False
@@ -26,7 +60,8 @@ class ArticlePagination(LimitOffsetPagination):
@method_decorator(name='list', decorator=swagger_auto_schema(
operation_description="description from swagger_auto_schema via method_decorator"
operation_description="description from swagger_auto_schema via method_decorator",
filter_inspectors=[DjangoFilterDescriptionInspector]
))
class ArticleViewSet(viewsets.ModelViewSet):
"""
@@ -52,7 +87,9 @@ class ArticleViewSet(viewsets.ModelViewSet):
ordering_fields = ('date_modified', 'date_created')
ordering = ('date_created',)
@swagger_auto_schema(auto_schema=NoPagingAutoSchema)
swagger_schema = NoTitleAutoSchema
@swagger_auto_schema(auto_schema=NoPagingAutoSchema, filter_inspectors=[DjangoFilterDescriptionInspector])
@list_route(methods=['get'])
def today(self, request):
today_min = datetime.datetime.combine(datetime.date.today(), datetime.time.min)