Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion rest_framework/authtoken/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from rest_framework.authtoken.serializers import AuthTokenSerializer
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.schemas import ManualSchema
import coreapi
import coreschema


class ObtainAuthToken(APIView):
Expand All @@ -11,6 +14,29 @@ class ObtainAuthToken(APIView):
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
renderer_classes = (renderers.JSONRenderer,)
serializer_class = AuthTokenSerializer
schema = ManualSchema(
fields=[
coreapi.Field(
name="username",
required=True,
location='form',
schema=coreschema.String(
title="Username",
description="Valid username for authentication",
),
),
coreapi.Field(
name="password",
required=True,
location='form',
schema=coreschema.String(
title="Password",
description="Valid password for authentication",
),
),
],
encoding="application/json",
)

def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data,
Expand All @@ -20,5 +46,4 @@ def post(self, request, *args, **kwargs):
token, created = Token.objects.get_or_create(user=user)
return Response({'token': token.key})


obtain_auth_token = ObtainAuthToken.as_view()
5 changes: 3 additions & 2 deletions rest_framework/schemas/inspectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class ManualSchema(ViewInspector):
Allows providing a list of coreapi.Fields,
plus an optional description.
"""
def __init__(self, fields, description=''):
def __init__(self, fields, description='', encoding=None):
"""
Parameters:

Expand All @@ -442,6 +442,7 @@ def __init__(self, fields, description=''):
assert all(isinstance(f, coreapi.Field) for f in fields), "`fields` must be a list of coreapi.Field instances"
self._fields = fields
self._description = description
self._encoding = encoding

def get_link(self, path, method, base_url):

Expand All @@ -451,7 +452,7 @@ def get_link(self, path, method, base_url):
return coreapi.Link(
url=urlparse.urljoin(base_url, path),
action=method.lower(),
encoding=None,
encoding=self._encoding,
fields=self._fields,
description=self._description
)
Expand Down