Skip to content

Instantly share code, notes, and snippets.

@lgyanf
Created June 14, 2016 10:49
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lgyanf/c1da5b1c3899105e59558ff286358ae5 to your computer and use it in GitHub Desktop.
A python decorator that adds query parameters to django-rest-framework swagger docstring.
# -*- coding: utf-8 -*-
import collections
import yaml
from rest_framework import fields
"""
Convert rest_framework.fields classes to Swagger data types according to http://swagger.io/specification/
Return 'string' by default.
"""
field_to_yaml_type = collections.defaultdict(
lambda: 'string',
{
fields.IntegerField: 'integer',
fields.FloatField: 'number',
fields.BooleanField: 'boolean',
fields.NullBooleanField: 'boolean',
}
)
def add_query_parameters(serializer_class):
def decorator(method):
def wrapper(*args, **kwargs):
return method(*args, **kwargs)
from rest_framework_swagger.introspectors import IntrospectorHelper, BaseViewIntrospector
i = BaseViewIntrospector(None, None, None, None)
parser = i.get_yaml_parser()
plain_text_doc = IntrospectorHelper.strip_yaml_from_docstring(method.__doc__)
yaml_object = parser.load_obj_from_docstring(method.__doc__) or {}
if 'parameters' in yaml_object:
if not isinstance(yaml_object['parameters'], list):
raise ValueError('paramters in YAML docstring should be a list instance')
else:
yaml_object['parameters'] = list()
serializer = serializer_class()
for field_name, field in serializer.fields.items():
parameter = {
'name': field_name,
'description': field.help_text,
'type': field_to_yaml_type[type(field)],
'required': field.required,
'paramType': 'query',
}
yaml_object['parameters'].append(parameter)
wrapper.__doc__ = '\n'.join([plain_text_doc,
'---',
yaml.dump(yaml_object, default_flow_style=False)])
return wrapper
return decorator
class ListBansSerializer(serializers.Serializer):
limit = serializers.IntegerField(default=10, help_text='query limit')
offset = serializers.IntegerField(default=0, help_text='query offset')
class ListBans(BaseBanView):
@add_query_parameters(ListBansSerializer)
def get(self, request):
"""
List all profile bans
---
response_serializer: backend_serializers.BanSerializer
"""
serializer = moderator_serializers.ListBansSerializer(data=request.query_params)
if serializer.is_valid(raise_exception=True):
# query profile bans
data = []
return APIResponse(status=status.HTTP_200_OK, data=data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment