Advertisement
Guest User

Untitled

a guest
Aug 27th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. def put(self, request, format=None):
  2. """
  3. This text is the description for this API
  4. username -- username
  5. password -- password
  6. """
  7. username = request.DATA['username']
  8. password = request.DATA['password']
  9.  
  10. def put(self, request, format=None):
  11. """
  12. This text is the description for this API.
  13.  
  14. ---
  15. parameters:
  16. - name: username
  17. description: Foobar long description goes here
  18. required: true
  19. type: string
  20. paramType: form
  21. - name: password
  22. paramType: form
  23. required: true
  24. type: string
  25. """
  26. username = request.DATA['username']
  27. password = request.DATA['password']
  28.  
  29. def put(...):
  30. """
  31. ...
  32.  
  33. ---
  34. parameters:
  35. - name: body
  36. description: JSON object containing two strings: password and username.
  37. required: true
  38. paramType: body
  39. pytype: RequestSerializer
  40. """
  41. ...
  42.  
  43. from django_filters.rest_framework.filterset import FilterSet
  44.  
  45. class ProductFilter(FilterSet):
  46.  
  47. class Meta(object):
  48. models = models.Product
  49. fields = (
  50. 'name', 'category', 'id', )
  51.  
  52.  
  53. class PurchasedProductsList(generics.ListAPIView):
  54. """
  55. Return a list of all the products that the authenticated
  56. user has ever purchased, with optional filtering.
  57. """
  58. model = Product
  59. serializer_class = ProductSerializer
  60. filter_class = ProductFilter
  61.  
  62. def get_queryset(self):
  63. user = self.request.user
  64. return user.purchase_set.all()
  65.  
  66. class SwaggerSchemaView(APIView):
  67. permission_classes = [IsAuthenticatedOrReadOnly,]
  68. renderer_classes = [renderers.OpenAPIRenderer, renderers.SwaggerUIRenderer]
  69.  
  70. schema = coreapi.Document(
  71. title='Thingy API thing',
  72. 'range': coreapi.Link(
  73. url='/range/{start}/{end}',
  74. action='get',
  75. fields=[
  76. coreapi.Field(
  77. name='start',
  78. required=True,
  79. location='path',
  80. description='start time as an epoch',
  81. type='integer'
  82. ),
  83. coreapi.Field(
  84. name='end',
  85. required=True,
  86. location='path',
  87. description='end time as an epoch',
  88. type='integer'
  89. )
  90. ],
  91. description='show the things between the things'
  92. ),
  93. }
  94. )
  95.  
  96. urlpatterns = [
  97. url(r'^$', SwaggerSchemaView.as_view()),
  98. ...
  99. ]
  100.  
  101. # encoding: utf-8
  102. from __future__ import unicode_literals
  103. from __future__ import absolute_import
  104. import coreapi
  105.  
  106. schema = coreapi.Document(
  107. title='Business Search API',
  108. url='/api/v3/business/',
  109. content={
  110. 'search': coreapi.Link(
  111. url='/',
  112. action='get',
  113. fields=[
  114. coreapi.Field(
  115. name='what',
  116. required=True,
  117. location='query',
  118. description='Search term'
  119. ),
  120. coreapi.Field(
  121. name='where',
  122. required=True,
  123. location='query',
  124. description='Search location'
  125. ),
  126. ],
  127. description='Search business listings'
  128. )
  129. }
  130. )
  131.  
  132. # encoding: utf-8
  133. from __future__ import unicode_literals
  134. from __future__ import absolute_import
  135. from rest_framework import exceptions
  136. from rest_framework.permissions import AllowAny
  137. from rest_framework.renderers import CoreJSONRenderer
  138. from rest_framework.response import Response
  139. from rest_framework.views import APIView
  140. from rest_framework_swagger import renderers
  141. from django.utils.module_loading import import_string
  142.  
  143.  
  144. def get_swagger_view(schema_location):
  145. """
  146. Returns schema view which renders Swagger/OpenAPI.
  147. """
  148. class SwaggerSchemaView(APIView):
  149. _ignore_model_permissions = True
  150. exclude_from_schema = True
  151. permission_classes = [AllowAny]
  152. renderer_classes = [
  153. CoreJSONRenderer,
  154. renderers.OpenAPIRenderer,
  155. renderers.SwaggerUIRenderer
  156. ]
  157.  
  158. def get(self, request):
  159. schema = None
  160.  
  161. try:
  162. schema = import_string(schema_location)
  163. except:
  164. pass
  165.  
  166. if not schema:
  167. raise exceptions.ValidationError(
  168. 'The schema generator did not return a schema Document'
  169. )
  170.  
  171. return Response(schema)
  172.  
  173.  
  174. return SwaggerSchemaView.as_view()
  175.  
  176. from ..swagger import get_swagger_view
  177. from . import views
  178.  
  179. schema_view = get_swagger_view(schema_location='drf_api.business.schema.schema')
  180.  
  181. urlpatterns = [
  182. url(r'^swagger/$', schema_view),
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement