Skip to content
This repository has been archived by the owner on Jan 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #570 from marcgibbons/feature/upgrade-reqs
Browse files Browse the repository at this point in the history
2.1.0: Make DRF 3.5 minimum version, use get_schema_view shortcut
  • Loading branch information
marcgibbons authored Oct 29, 2016
2 parents 19242d6 + d7f2a0a commit 0bcd8f9
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 109 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

Full documentation: http://marcgibbons.github.io/django-rest-swagger/

**Note:** you are viewing documentation for version 2, using Django REST Framework 3.4.1+ and CoreAPI. Documentation for previous versions is available [here](http://django-rest-swagger.readthedocs.io/en/stable-0.3.x/).


## Installation

Expand Down Expand Up @@ -53,7 +51,7 @@ urlpatterns = [

## Requirements
* Django 1.8+
* Django REST framework 3.4.1+
* Django REST framework 3.5.1+
* Python 2.7, 3.5


Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Swagger/OpenAPI Documentation Generator for Django REST Framework

---

**Note:** you are viewing documentation for version 2, using Django REST Framework 3.4+ and CoreAPI. Documentation for previous versions is available [here](http://django-rest-swagger.readthedocs.io/en/stable-0.3.x/).
**Note:** you are viewing documentation for version 2, using Django REST Framework 3.5+ and CoreAPI. Documentation for previous versions is available [here](http://django-rest-swagger.readthedocs.io/en/stable-0.3.x/).

---

Expand Down
3 changes: 2 additions & 1 deletion example_app/snippets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def highlight(self, request, *args, **kwargs):

def perform_create(self, serializer):
serializer.save(owner=self.request.user)



class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
This endpoint presents the users in the system.
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Django==1.10
djangorestframework==3.4.7
Django==1.10.2
djangorestframework==3.5.1
coreapi==2.0.8
openapi-codec==1.1.3
simplejson==3.8.2
openapi-codec==1.1.7
simplejson==3.9.0

# Testing
tox==2.3.1
tox==2.4.1
pylint==1.6.4
coverage==4.2
ipdb==0.10.1
Expand Down
2 changes: 1 addition & 1 deletion rest_framework_swagger/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.7'
__version__ = '2.1.0'
31 changes: 6 additions & 25 deletions rest_framework_swagger/views.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
from rest_framework import exceptions
from rest_framework.permissions import AllowAny
from rest_framework import schemas
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.response import Response
from rest_framework.schemas import SchemaGenerator
from rest_framework.views import APIView

from . import renderers


def get_swagger_view(title=None, url=None):
"""
Returns schema view which renders Swagger/OpenAPI.
(Replace with DRF get_schema_view shortcut in 3.5)
"""
class SwaggerSchemaView(APIView):
_ignore_model_permissions = True
exclude_from_schema = True
permission_classes = [AllowAny]
renderer_classes = [
return schemas.get_schema_view(
title=title,
url=url,
renderer_classes=[
CoreJSONRenderer,
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]

def get(self, request):
generator = SchemaGenerator(title=title, url=url)
schema = generator.get_schema(request=request)

if not schema:
raise exceptions.ValidationError(
'The schema generator did not return a schema Document'
)

return Response(schema)

return SwaggerSchemaView.as_view()
)
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
version=VERSION,
install_requires=[
'coreapi>=2.0.8',
'openapi-codec>=1.1.5',
'openapi-codec>=1.1.7',
'djangorestframework>=3.5.1',
'simplejson'
],
packages=['rest_framework_swagger'],
Expand Down
81 changes: 18 additions & 63 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.test import TestCase
from rest_framework.permissions import AllowAny
from rest_framework import schemas
from rest_framework.renderers import CoreJSONRenderer
from rest_framework.request import Request
from rest_framework.test import APIRequestFactory

from rest_framework_swagger import renderers
Expand All @@ -16,66 +15,22 @@ def setUp(self):
self.factory = APIRequestFactory()
self.view_class = self.sut().cls

def test_title_and_urlpassed_to_schema_generator(self):
title = 'Vandelay'
url = 'https://github.com/marcgibbons/django-rest-swagger'
view = self.sut(title=title, url=url)

with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
request = self.factory.get('/')
view(request=request)

mock.assert_called_once_with(title=title, url=url)

def test_ignore_model_permissions_true(self):
self.assertTrue(self.view_class._ignore_model_permissions)

def test_exclude_from_schema(self):
self.assertTrue(self.view_class.exclude_from_schema)

def test_renderer_classes(self):
self.assertListEqual(
[
CoreJSONRenderer,
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
],
self.view_class.renderer_classes
)

def test_permission_class(self):
self.assertListEqual(
[AllowAny],
self.view_class.permission_classes
def test_returns_get_schema_view(self):
title = 'Vandelay Industries API',
url = 'http://vandelay.industries'
renderer_classes = [
CoreJSONRenderer,
renderers.OpenAPIRenderer,
renderers.SwaggerUIRenderer
]

with patch.object(schemas, 'get_schema_view') as mock:
result = self.sut(title=title, url=url)

mock.assert_called_once_with(
title=title,
url=url,
renderer_classes=renderer_classes
)

def test_return_400_if_schema_is_none(self):
with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
mock.return_value.get_schema.return_value = None
request = self.factory.get('/')
response = self.sut()(request=request)

self.assertEqual(400, response.status_code)
self.assertEqual(
['The schema generator did not return a schema Document'],
response.data
)

def test_response_is_result_of_schema_generator(self):
expected = 'My amazing schema'
with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
mock.return_value.get_schema.return_value = expected
request = self.factory.get('/')
response = self.sut()(request=request)

self.assertEqual(200, response.status_code)
self.assertEqual(expected, response.data)

def test_schema_generator_instantiated_with_request(self):
with patch('rest_framework_swagger.views.SchemaGenerator') as mock:
request = self.factory.get('/')
self.sut()(request=request)

call_args = mock.return_value.get_schema.call_args[1]
self.assertIn('request', call_args)
self.assertIsInstance(call_args['request'], Request)
self.assertEqual(mock.return_value, result)
19 changes: 10 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
[tox]
envlist =
skipsdist=True
envlist =
latest
{py27,py35}-django{1.8,1.9,1.10}-drf{3.4}
{py27,py35}-django{18,19,110}-drf35
lint

[testenv]
commands = python runtests.py
deps =
coreapi
openapi-codec
simplejson
coverage
drf3.4: djangorestframework>=3.4.3,<3.5
django1.8: Django>=1.8,<1.9
django1.9: Django>=1.9,<1.10
django1.10: Django>=1.10,<1.11
mock
openapi-codec
simplejson
drf35: djangorestframework>=3.5.1,<3.6
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
django110: Django>=1.10,<1.11

[testenv:latest]
commands = coverage run runtests.py
pip_pre = True
deps =
deps =
Django
coreapi
openapi-codec
Expand Down

0 comments on commit 0bcd8f9

Please sign in to comment.