Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
cyriac committed Jun 4, 2016
2 parents fd5213e + 411eb1f commit 019a258
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 19 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ env:
- DJANGO_VERSION=1.8
- DJANGO_VERSION=1.9

cache:
- pip
- directories:
- rest_framework_docs/static/node_modules/

before_install:
- nvm install 5

install:
- cd rest_framework_docs/static/ && npm install && cd ../../
- pip install -r requirements.txt
Expand Down
11 changes: 11 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
coverage:
precision: 2
round: down
range: "70...100"

status:
project: false
patch: false
changes: false

comment: off
5 changes: 5 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ title: "Changelog"
source_filename: "changelog"
---

### Release 0.0.10

- Use get_serializer_class for Views without serlaizer_class attribute [#92](https://github.com/ekonstantinidis/django-rest-framework-docs/pull/92)


### Release 0.0.9

- Support for more types of `ROOT_URLCONF`
Expand Down
4 changes: 2 additions & 2 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ To hide the GitHub badge from the page, just override it with an empty block.
{% block jumbotron %}
<div class="jumbotron">
<h1>Project Title</h1>
<h3>Documentantion of the project 'Example'.</h3>
<h3>Documentation of the project 'Example'.</h3>
</div>
{% endblock %}

Expand Down Expand Up @@ -65,7 +65,7 @@ File location: `templates/rest_framework_docs/docs.html`
{% block jumbotron %}
<div class="jumbotron">
<h1>'Project Name' Web API</h1>
<h3>Documentantion of the 'Project Name' Web API.</h3>
<h3>Documentation of the 'Project Name' Web API.</h3>
</div>
{% endblock %}

Expand Down
2 changes: 1 addition & 1 deletion rest_framework_docs/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.9'
__version__ = '0.0.10'
33 changes: 19 additions & 14 deletions rest_framework_docs/api_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,27 @@ def __get_permissions_class__(self):

def __get_serializer_fields__(self):
fields = []
serializer = None

if hasattr(self.callback.cls, 'serializer_class') and hasattr(self.callback.cls.serializer_class, 'get_fields'):
if hasattr(self.callback.cls, 'serializer_class'):
serializer = self.callback.cls.serializer_class
if hasattr(serializer, 'get_fields'):
try:
fields = [{
"name": key,
"type": str(field.__class__.__name__),
"required": field.required
} for key, field in serializer().get_fields().items()]
except KeyError as e:
self.errors = e
fields = []

# FIXME:
# Show more attibutes of `field`?

elif hasattr(self.callback.cls, 'get_serializer_class'):
serializer = self.callback.cls.get_serializer_class(self.pattern.callback.cls())

if hasattr(serializer, 'get_fields'):
try:
fields = [{
"name": key,
"type": str(field.__class__.__name__),
"required": field.required
} for key, field in serializer().get_fields().items()]
except KeyError as e:
self.errors = e
fields = []

# FIXME:
# Show more attibutes of `field`?

return fields

Expand Down
12 changes: 10 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_index_view_with_endpoints(self):
response = self.client.get(reverse('drfdocs'))

self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["endpoints"]), 10)
self.assertEqual(len(response.context["endpoints"]), 11)

# Test the login view
self.assertEqual(response.context["endpoints"][0].name_parent, "accounts")
Expand All @@ -38,8 +38,16 @@ def test_index_view_with_endpoints(self):
self.assertEqual(response.context["endpoints"][0].fields[0]["type"], "CharField")
self.assertTrue(response.context["endpoints"][0].fields[0]["required"])

self.assertEqual(response.context["endpoints"][1].name_parent, "accounts")
self.assertEqual(response.context["endpoints"][1].allowed_methods, ['POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][1].path, "/accounts/login2/")
self.assertEqual(response.context["endpoints"][1].docstring, "A view that allows users to login providing their username and password. Without serializer_class")
self.assertEqual(len(response.context["endpoints"][1].fields), 2)
self.assertEqual(response.context["endpoints"][1].fields[0]["type"], "CharField")
self.assertTrue(response.context["endpoints"][1].fields[0]["required"])

# The view "OrganisationErroredView" (organisations/(?P<slug>[\w-]+)/errored/) should contain an error.
self.assertEqual(str(response.context["endpoints"][8].errors), "'test_value'")
self.assertEqual(str(response.context["endpoints"][9].errors), "'test_value'")

def test_index_search_with_endpoints(self):
response = self.client.get("%s?search=reset-password" % reverse("drfdocs"))
Expand Down
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

accounts_urls = [
url(r'^login/$', views.LoginView.as_view(), name="login"),
url(r'^login2/$', views.LoginWithSerilaizerClassView.as_view(), name="login2"),
url(r'^register/$', views.UserRegistrationView.as_view(), name="register"),
url(r'^reset-password/$', view=views.PasswordResetView.as_view(), name="reset-password"),
url(r'^reset-password/confirm/$', views.PasswordResetConfirmView.as_view(), name="reset-password-confirm"),
Expand Down
21 changes: 21 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ def delete(self, request, *args, **kwargs):
class OrganisationErroredView(generics.ListAPIView):

serializer_class = serializers.OrganisationErroredSerializer


class LoginWithSerilaizerClassView(APIView):
"""
A view that allows users to login providing their username and password. Without serializer_class
"""

throttle_classes = ()
permission_classes = ()
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
renderer_classes = (renderers.JSONRenderer,)

def post(self, request):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({'token': token.key})

def get_serializer_class(self):
return AuthTokenSerializer

0 comments on commit 019a258

Please sign in to comment.