mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 12:17:24 +03:00
c911d54ae3
When incorrect parameters are supplied to the obtain auth token view 400 *is* the correct response.
26 lines
944 B
Python
26 lines
944 B
Python
from rest_framework.views import APIView
|
|
from rest_framework import status
|
|
from rest_framework import parsers
|
|
from rest_framework import renderers
|
|
from rest_framework.response import Response
|
|
from rest_framework.authtoken.models import Token
|
|
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
|
|
|
|
|
class ObtainAuthToken(APIView):
|
|
throttle_classes = ()
|
|
permission_classes = ()
|
|
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
|
renderer_classes = (renderers.JSONRenderer,)
|
|
model = Token
|
|
|
|
def post(self, request):
|
|
serializer = AuthTokenSerializer(data=request.DATA)
|
|
if serializer.is_valid():
|
|
token, created = Token.objects.get_or_create(user=serializer.object['user'])
|
|
return Response({'token': token.key})
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
obtain_auth_token = ObtainAuthToken.as_view()
|